Home Database Mysql Tutorial 执行计划顺序不符合一般规则

执行计划顺序不符合一般规则

Jun 07, 2016 pm 04:37 PM
oracle generally implement rule plan order

? 在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;
? ? ? ? ID NAME
———- ———————————-
? ? ? ? ?1 x1
? ? ? ? ?2 x2
2 rows selected.

SQL> SELECT a.ID,a.NAME,(SELECT b.ID FROM b WHERE a.ID=b.ID)?bid FROM a;

执行计划
———————————————————-
Plan hash value: 2657529235
————————————————————————–
| Id ?| Operation ? ? ? ? | Name | Rows ?| Bytes | Cost (%CPU)| Time ? ? |
————————————————————————–
| ? 0 | SELECT STATEMENT ?| ? ? ?| ? ? 3 | ? ?60 | ? ? 3 ? (0)| 00:00:01 |
|* ?1 | ?TABLE ACCESS FULL| B ? ?| ? ? 1 | ? ?13 | ? ? 3 ? (0)| 00:00:01 |
| ? 2 | ?TABLE ACCESS FULL| A ? ?| ? ? 3 | ? ?60 | ? ? 3 ? (0)| 00:00:01 |
————————————————————————–
Predicate Information (identified by operation id):
—————————————————
? ?1 – filter(“B”.”ID”=:B1)

? ? ??如果按照文档的的分析,显然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
————————————————————————————
| Id ?| Operation ? ? ? ? | Name | Starts | E-Rows | A-Rows | ? A-Time ? | Buffers |
————————————————————————————
| ? 0 | SELECT STATEMENT ?| ? ? ?| ? ? ?1 | ? ? ? ?| ? ? ?3 |00:00:00.01 | ? ? ? 8 |
|* ?1 | ?TABLE ACCESS FULL| B ? ?| ? ???3?| ? ? ?1 | ? ? ?2 |00:00:00.01 | ? ? ?21 |
| ? 2 | ?TABLE ACCESS FULL| A ? ?| ? ? ?1 | ? ? ?3 | ? ???3?|00:00:00.01 | ? ? ? 8 |
————————————————————————————

Predicate Information (identified by operation id):
—————————————————
? ?1 – filter(“B”.”ID”=:B1)

? ? ??其中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;
Commit complete.

SQL>??@display_cursor
SQL_ID ?caq6tcx266xnq, child number 0
————————————-
SELECT a.ID,a.NAME,(SELECT b.ID FROM b WHERE a.ID=b.ID) bid FROM a

Plan hash value: 2657529235

————————————————————————————
| Id ?| Operation ? ? ? ? | Name | Starts | E-Rows | A-Rows | ? A-Time ? | Buffers |
————————————————————————————
| ? 0 | SELECT STATEMENT ?| ? ? ?| ? ? ?1 | ? ? ? ?| ? ? ?4 |00:00:00.01 | ? ? ? 8 |
|* ?1 | ?TABLE ACCESS FULL| B ? ?| ? ???3?| ? ? ?1 | ? ? ?2?|00:00:00.01 | ? ? ?21 |
| ? 2 | ?TABLE ACCESS FULL| A ? ?| ? ? ?1 | ? ? ?4 | ? ???4?|00:00:00.01 | ? ? ? 8 |
————————————————————————————

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,在学习过程中,对不理解的问题,要随时质疑,并论证之。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

See all articles