[20201116]11g連線謂詞推入push_pred問題.txt

lfree發表於2020-11-16

[20201116]11g連線謂詞推入push_pred問題.txt

--//看崔華<基於oracle的sql最佳化>,裡面提到11g連線謂詞推入問題的問題自己重複測試看看.

1.環境:
SCOTT@book> @ ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

2.測試環境建立:
--//注書中的例子好像不是scott.emp表,不過應該不影響測試.
create table emp1 as select * from emp ;
create index i_emp1_empno on emp1(empno);
create or replace view emp_view  as select emp1.empno as empno1 from emp1;

3.測試:
SCOTT@book> alter session set statistics_level = all;
Session altered.

SCOTT@book> select emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD';
     EMPNO
----------
      7902

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  b7up3a8rsctnz, child number 0
-------------------------------------
select emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD'
Plan hash value: 1061644130
-----------------------------------------------------------------------------------------------------------------------------
| Id  | Operation          | Name         | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
-----------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |              |      1 |        |       |     3 (100)|          |      1 |00:00:00.01 |       9 |
|   1 |  NESTED LOOPS      |              |      1 |      1 |    14 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       9 |
|*  2 |   TABLE ACCESS FULL| EMP          |      1 |      1 |    10 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       7 |
|*  3 |   INDEX RANGE SCAN | I_EMP1_EMPNO |      1 |      1 |     4 |     0   (0)|          |      1 |00:00:00.01 |       2 |
-----------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$F5BB74E1
   2 - SEL$F5BB74E1 / EMP@SEL$1
   3 - SEL$F5BB74E1 / EMP1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("EMP"."ENAME"='FORD')
   3 - access("EMP"."EMPNO"="EMP1"."EMPNO")
29 rows selected.

--//這裡僅僅簡單檢視合併.可以透過提示merge確定,修改如下:

SCOTT@book> select /*+ merge(emp_view) */ emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD';
     EMPNO
----------
      7902

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  35ybv5cn4vx32, child number 0
-------------------------------------
select /*+ merge(emp_view) */ emp.empno from emp,emp_view where
emp.empno=emp_view.empno1 and emp.ename='FORD'
Plan hash value: 1061644130
-----------------------------------------------------------------------------------------------------------------------------
| Id  | Operation          | Name         | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
-----------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |              |      1 |        |       |     3 (100)|          |      1 |00:00:00.01 |       9 |
|   1 |  NESTED LOOPS      |              |      1 |      1 |    14 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       9 |
|*  2 |   TABLE ACCESS FULL| EMP          |      1 |      1 |    10 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       7 |
|*  3 |   INDEX RANGE SCAN | I_EMP1_EMPNO |      1 |      1 |     4 |     0   (0)|          |      1 |00:00:00.01 |       2 |
-----------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$F5BB74E1
   2 - SEL$F5BB74E1 / EMP@SEL$1
   3 - SEL$F5BB74E1 / EMP1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("EMP"."ENAME"='FORD')
   3 - access("EMP"."EMPNO"="EMP1"."EMPNO")

4.使用提示no_merge看看:

SCOTT@book> select /*+ no_merge(emp_view)*/ emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD';
     EMPNO
----------
      7902

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  7kghy2nb3bnr9, child number 0
-------------------------------------
select /*+ no_merge(emp_view)*/ emp.empno from emp,emp_view where
emp.empno=emp_view.empno1 and emp.ename='FORD'

Plan hash value: 1565533375

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation                    | Name     | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers | Reads  |  OMem |  1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |          |      1 |        |       |     6 (100)|          |      1 |00:00:00.01 |       6 |      1 |       |       |          |
|   1 |  MERGE JOIN                  |          |      1 |      1 |    23 |     6  (17)| 00:00:01 |      1 |00:00:00.01 |       6 |      1 |       |       |          |
|*  2 |   TABLE ACCESS BY INDEX ROWID| EMP      |      1 |      1 |    10 |     2   (0)| 00:00:01 |      1 |00:00:00.01 |       4 |      1 |       |       |          |
|   3 |    INDEX FULL SCAN           | PK_EMP   |      1 |     14 |       |     1   (0)| 00:00:01 |     14 |00:00:00.01 |       2 |      1 |       |       |          |
|*  4 |   SORT JOIN                  |          |      1 |     14 |   182 |     4  (25)| 00:00:01 |      1 |00:00:00.01 |       2 |      0 |  2048 |  2048 | 2048  (0)|
|   5 |    VIEW                      | EMP_VIEW |      1 |     14 |   182 |     3   (0)| 00:00:01 |     14 |00:00:00.01 |       2 |      0 |       |       |          |
|   6 |     TABLE ACCESS FULL        | EMP1     |      1 |     14 |    56 |     3   (0)| 00:00:01 |     14 |00:00:00.01 |       2 |      0 |       |       |          |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   2 - SEL$1 / EMP@SEL$1
   3 - SEL$1 / EMP@SEL$1
   5 - SEL$2 / EMP_VIEW@SEL$1
   6 - SEL$2 / EMP1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("EMP"."ENAME"='FORD')
   4 - access("EMP"."EMPNO"="EMP_VIEW"."EMPNO1")
       filter("EMP"."EMPNO"="EMP_VIEW"."EMPNO1")
35 rows selected.
--//11g無法實現連線謂詞推入push_pred
--//透過如下提示控制如何呢?

SCOTT@book> select /*+ no_merge(emp_view) use_nl(emp_view) push_pred(emp_view) */ emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD';
     EMPNO
----------
      7902

SCOTT@book> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  dd885z1b7u4nz, child number 0
-------------------------------------
select /*+ no_merge(emp_view) use_nl(emp_view) push_pred(emp_view) */
emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and
emp.ename='FORD'
Plan hash value: 3774177413
--------------------------------------------------------------------------------------------------------------------------
| Id  | Operation           | Name     | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
--------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |          |      1 |        |       |     6 (100)|          |      1 |00:00:00.01 |      10 |
|   1 |  NESTED LOOPS       |          |      1 |      1 |    23 |     6   (0)| 00:00:01 |      1 |00:00:00.01 |      10 |
|*  2 |   TABLE ACCESS FULL | EMP      |      1 |      1 |    10 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       7 |
|*  3 |   VIEW              | EMP_VIEW |      1 |      1 |    13 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       3 |
|   4 |    TABLE ACCESS FULL| EMP1     |      1 |     14 |    56 |     3   (0)| 00:00:01 |     14 |00:00:00.01 |       3 |
--------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   2 - SEL$1 / EMP@SEL$1
   3 - SEL$2 / EMP_VIEW@SEL$1
   4 - SEL$2 / EMP1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("EMP"."ENAME"='FORD')
   3 - filter("EMP"."EMPNO"="EMP_VIEW"."EMPNO1")
32 rows selected.

--//確實像作者介紹那樣,11g這種情況無法實現謂詞推入。也許我哪裡做錯了........

4.繼續測試:
--//12c呢?
SCOTT@test01p> @ ver1
PORT_STRING                    VERSION        BANNER                                                                               CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0           12.2.0.1.0     Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production              0
--//建表以及測試相關檢視略。

SCOTT@test01p> select /*+ no_merge(emp_view) use_nl(emp_view) push_pred(emp_view) */ emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and emp.ename='FORD';
     EMPNO
----------
      7902

SCOTT@test01p> @ dpc '' ''
PLAN_TABLE_OUTPUT
-------------------------------------
SQL_ID  dd885z1b7u4nz, child number 0
-------------------------------------
select /*+ no_merge(emp_view) use_nl(emp_view) push_pred(emp_view) */
emp.empno from emp,emp_view where emp.empno=emp_view.empno1 and
emp.ename='FORD'
Plan hash value: 3352622377
----------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation               | Name         | Starts | E-Rows |E-Bytes| Cost (%CPU)| E-Time   | A-Rows |   A-Time   | Buffers |
----------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |              |      1 |        |       |     4 (100)|          |      1 |00:00:00.01 |      10 |
|   1 |  NESTED LOOPS           |              |      1 |      1 |    12 |     4   (0)| 00:00:01 |      1 |00:00:00.01 |      10 |
|*  2 |   TABLE ACCESS FULL     | EMP          |      1 |      1 |    10 |     3   (0)| 00:00:01 |      1 |00:00:00.01 |       8 |
|   3 |   VIEW PUSHED PREDICATE | EMP_VIEW     |      1 |      1 |     2 |     1   (0)| 00:00:01 |      1 |00:00:00.01 |       2 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|*  4 |    INDEX RANGE SCAN     | I_EMP1_EMPNO |      1 |      1 |     4 |     1   (0)| 00:00:01 |      1 |00:00:00.01 |       2 |
----------------------------------------------------------------------------------------------------------------------------------
Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------
   1 - SEL$1
   2 - SEL$1        / EMP@SEL$1
   3 - SEL$639F1A6F / EMP_VIEW@SEL$1
   4 - SEL$639F1A6F / EMP1@SEL$2
Predicate Information (identified by operation id):
---------------------------------------------------
   2 - filter("EMP"."ENAME"='FORD')
   4 - access("EMP1"."EMPNO"="EMP"."EMPNO")
--//很明顯不像作者書中介紹那樣,12c是可以最佳化的.
--//說明一點許多東西會變,oracle也在不斷完善自己,當然bug也越來越多。不能抱著一成不變的觀點看問題,要與時俱進^_^。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2734347/,如需轉載,請註明出處,否則將追究法律責任。

相關文章