执行计划顺序不符合一般规则
? 在Oracle performance tuning guide中,对执行计划顺序的描述是最右最上最先执行,然后父步骤执行,也就是最右边的步骤最先执行,如果同等级,那么最上边的最先执行,然后执行其父步骤(文档原文:The execution order in EXPLAIN PLAN output begins with
? 在Oracle performance tuning guide中,对执行计划顺序的描述是最右最上最先执行,然后父步骤执行,也就是最右边的步骤最先执行,如果同等级,那么最上边的最先执行,然后执行其父步骤(文档原文:The execution order in EXPLAIN PLAN output begins with the line that is the?furthest indented to the right. The next step is the parent of that line. If two lines?are indented equally, then the top line is normally executed first)。
? ? ? 在实际应用中,这个规则不是完全正确的。ORACLE的SQL内部步骤的执行顺序与其计划中的展现,会有一定的差别,如果不仔细分析,而且一味相信文档,那么可能会感觉很迷惑。比如在标量子查询中(scalary subquery),执行计划的显示会非常让人困惑,如:
SQL> select * from a; ? ? ? ? ID NAME ———- ———————————- ? ? ? ? ?1 a ? ? ? ? ?2 b ? ? ? ? ?3 c 3 rows selected. SQL> select * from b; SQL> SELECT a.ID,a.NAME,(SELECT b.ID FROM b WHERE a.ID=b.ID)?bid FROM a; 执行计划 |
? ? ??如果按照文档的的分析,显然ID=2的与ID=1的是同等级的,ID=1的在ID=2的上面,那么最后执行计划的顺序应该是1—->2—–>0,但是分析下,显然不是这样的顺序,肯定是必须获得a.id之后,才能用a.id去查找B。通过谓词中的”B”.”ID”=:B1可以看出来,:B1,类似于绑定变量,这里就2张表,而且根据SQL查询,肯定来源于A.ID。所以对于标量子查询的计划,应该是2—->1—–>0,而且2与1的操作是类似于NESTED LOOPS(与其不同的是,标量子查询的驱动表是inner table)的操作,每1个A的行,都会执行一次B,当然,ORACLE内部肯定是有优化的,这种优化就是会缓存已经匹配的A.ID值,遇到相同的,不会重复扫描B。可以通过DBMS_XPLAN.DISPLAY_CURSOR详细看看如何执行的:
SQL> @display_cursor SQL_ID ?caq6tcx266xnq, child number 1 ————————————- SELECT a.ID,a.NAME,(SELECT b.ID FROM b WHERE a.ID=b.ID) bid FROM a Plan hash value: 2657529235 Predicate Information (identified by operation id): |
? ? ??其中A共3行,访问B 3次,返回B 2行,因为有一行不匹配,由A的行驱动访问B。因为这里A.ID无重复值,下面插入一行id=1的,因为id=1已经在A表中存在,因此,标量子查询有缓存,所以对B的扫描还是3次,而不是4次,如下:
SQL> INSERT INTO a VALUES(1,’d'); 1 row created. SQL> COMMIT; SQL>??@display_cursor Plan hash value: 2657529235 ———————————————————————————— Predicate Information (identified by operation id): ? ?1 – filter(“B”.”ID”=:B1) |
? ? ?从计划中可以看到,虽然A是4行,但是因为DISTINCT A.ID是3行,所以还是扫描B 3次,其中ID=1的访问一次即缓存结果,通过A-ROWS可以看到B还是返回2行,而不是3行。所以不要看到标量子查询就认为效率不行,标量子查询和FILTER类似,如果能够对标量子查询走索引扫描,甚至UNIQUE INDEX SCAN,如果主表查询的行重复值特别多,效率还是很高的,标量子查询在一定程度上,消除了JOIN,经常在查询这种对应某表的行,需要匹配另一表的某个列值,比JOIN效率高(当然,既然类似于NESTED LOOPS了,结果集肯定不会很大,不然效率会差,这个tom的高效设计上有详细的讲解)。
? ?
? ? 本文主要就是讲解下,执行计划反应了SQL的执行顺序,但是如果通过执行计划准确知道SQL中的执行顺序,并不是只要了解文档中说的规则就可以了,在实际应用中,可能会碰到这样那样的问题,文档当然很少有错误,但是文档大多说的都是普遍的规则,SO,在学习过程中,对不理解的问题,要随时质疑,并论证之。
原文地址:执行计划顺序不符合一般规则, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.

The method of replacing strings in Oracle is to use the REPLACE function. The syntax of this function is: REPLACE(string, search_string, replace_string). Usage steps: 1. Identify the substring to be replaced; 2. Determine the new string to replace the substring; 3. Use the REPLACE function to replace. Advanced usage includes: multiple replacements, case sensitivity, special character replacement, etc.
