update 强制 NEST_LOOP NL 的理解,被驱动表 inner table

PURPOSE

This document suggests methods of processing update statements that contain subqueries so that the query drives off the subquery (i.e. it examines the subquery first before it looks at the table to be updated). This can have advantages when the subquery contains information that would allow indexes to be used on the updated table that would otherwise be unavailable. Note that the use of the techniques illustrated here are not restricted to updates but can be modified to affect many other queries.

DETAILS

Update with subquery not using index on updated table

Consider the following update:

UPDATE emp e

SET e.empno = e.empno

WHERE e.deptno in (SELECT d.deptno FROM dept d)

/

If there is an index on e.deptno then it is possible that this may be a good access path for emp. An index lookup can only be used if there is a value provided to lookup with (unless the whole index is scanned which is typically not cost effective). In this case a lookup can only be achieved if rows have already been retrieved from dept to drive the index lookup on emp. So to perform the index lookup on emp the query needs to access dept before it accesses emp. However it is likely that the plan chosen by default for this query will look something like:

Execution Plan

0 UPDATE STATEMENT Optimizer=CHOOSE (Cost=6 Card=1 Bytes=52)

1 0 HASH JOIN (Cost=6 Card=1 Bytes=52)

2 1 TABLE ACCESS (FULL) OF 'EMP' (Cost=1 Card=14 Bytes=546)

3 1 VIEW (Cost=4 Card=21 Bytes=273)

4 3 SORT (UNIQUE)

5 4 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=21 Bytes=273)

In other words it looks at emp first as opposed to dept and so does not use the index since the indexed column does not have a value to lookup with.

The optimizer does consider driving the table from both emp & dept but since it does the evaluation on a cost basis it may choose to do the query in the order that you do not want. So how can the optimizer be forced to use the subquery to drive the update?

With a select, an ordered hint could be used together with modifications to the from clause to achieve the required join order. However, an update does not have a from clause so an ordered hint cannot be used in the same way.

How to get it to use an index:

The query can be forced in to a Nested Loop join with an ORDERED and a USE_NL hint:

SQL> UPDATE /*+ ORDERED USE_NL(E) INDEX(E) */ emp e

SET e.empno = e.empno

WHERE e.deptno in (SELECT d.deptno FROM dept d)

/

15 rows updated.

Execution Plan

0 UPDATE STATEMENT Optimizer=CHOOSE (Cost=46 Card=1 Bytes=52)

1 0 NESTED LOOPS (Cost=46 Card=1 Bytes=52)

2 1 VIEW (Cost=4 Card=21 Bytes=273)

3 2 SORT (UNIQUE)

4 3 TABLE ACCESS (FULL) OF 'DEPT' (Cost=1 Card=21 Bytes=273)

5 1 INDEX (RANGE SCAN) OF 'E_DNO' (NON-UNIQUE)

Notice that the USE_NL hint specifies the inner table E (emp). Since the hint has indicated that emp should be the inner table, this leaves Dept as the outer table. Since dept is the outer table it is accessed first (before emp) and so values retrieved from dept can be used to lookup in the E_DNO index.

USE_NL 两个一起也是可以的。

Alternative solutions

  • Use PLSQL. Use the select from dept as the driving cursor for the update. 这种肯定量大就不是高效的。
  • It may also be possible to create a view on both tables and update the view. However there are numerous restrictions with using this method. 直接update 两张表
  • merge 考虑一下
相关推荐
Mahir082 小时前
Redis 与 MySQL 数据同步:一致性保证的完整解决方案
数据库·redis·mysql·缓存·面试·数据一致性
2301_769340672 小时前
如何在 Vuetify 中可靠捕获 Chip 关闭事件(包括键盘触发).txt
jvm·数据库·python
AC赳赳老秦2 小时前
供应链专员提效:OpenClaw自动跟踪物流信息、更新库存数据,异常自动提醒
java·大数据·服务器·数据库·人工智能·自动化·openclaw
灵犀学长3 小时前
基于 Spring ThreadPoolTaskScheduler + CronTrigger 实现的动态定时任务调度系统
java·数据库·spring
北秋,3 小时前
PostgreSQL(Postgres)数据库基础用法 + 数字型 + 字符型 完整联合注入实战
数据库·postgresql·开源
m0_596749094 小时前
JavaScript中手动实现一个new操作符的底层逻辑
jvm·数据库·python
多加点辣也没关系4 小时前
Redis 的安装(详细教程)
数据库·redis·缓存
数据库小学妹4 小时前
数据库连接池避坑指南:告别“连接超时”与“资源耗尽”,让系统跑得更快!
数据库·redis·sql·mysql·缓存·dba
dishugj5 小时前
HANA 数据库备份与恢复
数据库·oracle
前进的李工5 小时前
EXPLAIN输出格式全解析:JSON、TREE与可视化
开发语言·数据库·mysql·性能优化·explain