您的位置:首页 > 数据库 > Oracle

oracle谓词推进测试

2015-11-04 16:19 531 查看
可合并视图,不可合并视图,谓词推进测试

 create table t3 as select * from dba_tables;

 create table t4 as select * from dba_users;

 create table t5 as select * from dba_objects;

对于没有外连接的测试

SQL> select * from (select * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner=t5.owner and v.table_name=t5.object_name;

已选择1917行。

执行计划

----------------------------------------------------------

Plan hash value: 2774782749

-------------------------------------------------------------------------------------

| Id  | Operation            | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |

-------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT     |      |  1683 |  4805K|       |  1126   (1)| 00:00:14 |

|   1 |  SORT ORDER BY       |      |  1683 |  4805K|  6744K|  1126   (1)| 00:00:14 |

|*  2 |   HASH JOIN          |      |  1683 |  4805K|       |    97   (2)| 00:00:02 |

|   3 |    TABLE ACCESS FULL | T4   |    18 | 39168 |       |     3   (0)| 00:00:01 |

|*  4 |    HASH JOIN         |      |  1683 |  1229K|       |    94   (2)| 00:00:02 |

|   5 |     TABLE ACCESS FULL| T3   |  1683 |   889K|       |    20   (0)| 00:00:01 |

|   6 |     TABLE ACCESS FULL| T5   | 21334 |  4312K|       |    73   (0)| 00:00:01 |

-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - access("T3"."OWNER"="T4"."USERNAME")

   4 - access("T3"."OWNER"="T5"."OWNER" AND

              "T3"."TABLE_NAME"="T5"."OBJECT_NAME")

Note

-----

   - dynamic sampling used for this statement (level=2)

对于左外连接的测试

 select * from (select * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner=t5.owner(+) and v.table_name=t5.object_name(+);

执行计划

----------------------------------------------------------

Plan hash value: 165444085

-------------------------------------------------------------------------------------

| Id  | Operation            | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |

-------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT     |      |  1683 |  4805K|       |  1126   (1)| 00:00:14 |

|   1 |  SORT ORDER BY       |      |  1683 |  4805K|  6744K|  1126   (1)| 00:00:14 |

|*  2 |   HASH JOIN          |      |  1683 |  4805K|       |    97   (2)| 00:00:02 |

|   3 |    TABLE ACCESS FULL | T4   |    18 | 39168 |       |     3   (0)| 00:00:01 |

|*  4 |    HASH JOIN OUTER   |      |  1683 |  1229K|       |    94   (2)| 00:00:02 |

|   5 |     TABLE ACCESS FULL| T3   |  1683 |   889K|       |    20   (0)| 00:00:01 |

|   6 |     TABLE ACCESS FULL| T5   | 21334 |  4312K|       |    73   (0)| 00:00:01 |

-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   2 - access("T3"."OWNER"="T4"."USERNAME")

   4 - access("T3"."OWNER"="T5"."OWNER"(+) AND

              "T3"."TABLE_NAME"="T5"."OBJECT_NAME"(+))

Note

-----

   - dynamic sampling used for this statement (level=2)

对于右外连接的测试

SQL> select * from (select * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner(+)=t5.owner and v.table_name(+)=t5.object_name;

已选择18515行。

执行计划

----------------------------------------------------------

Plan hash value: 3366092662

--------------------------------------------------------------------------------------

| Id  | Operation             | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |

--------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT      |      | 21334 |    59M|       |  1492   (1)| 00:00:18 |

|*  1 |  HASH JOIN RIGHT OUTER|      | 21334 |    59M|  4488K|  1492   (1)| 00:00:18 |

|   2 |   VIEW                |      |  1683 |  4465K|       |   979   (1)| 00:00:12 |

|   3 |    SORT ORDER BY      |      |  1683 |  4465K|  6744K|   979   (1)| 00:00:12 |

|*  4 |     HASH JOIN         |      |  1683 |  4465K|       |    24   (5)| 00:00:01 |

|   5 |      TABLE ACCESS FULL| T4   |    18 | 39168 |       |     3   (0)| 00:00:01 |

|   6 |      TABLE ACCESS FULL| T3   |  1683 |   889K|       |    20   (0)| 00:00:01 |

|   7 |   TABLE ACCESS FULL   | T5   | 21334 |  4312K|       |    73   (0)| 00:00:01 |

--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - access("V"."OWNER"(+)="T5"."OWNER" AND

              "V"."TABLE_NAME"(+)="T5"."OBJECT_NAME")

   4 - access("T3"."OWNER"="T4"."USERNAME")

Note

-----

   - dynamic sampling used for this statement (level=2)

看到对于右外连接的这种情况,视图没有合并

对于rownum伪劣的情况

SQL> select * from (select * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner=t5.owner and v.table_name=t5.object_name and rownum<1

0;

已选择9行。

执行计划

----------------------------------------------------------

Plan hash value: 87499378

---------------------------------------------------------------------------------------

| Id  | Operation              | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT       |      |     9 | 26316 |       |   649   (1)| 00:00:08 |

|*  1 |  COUNT STOPKEY         |      |       |       |       |            |          |

|   2 |   NESTED LOOPS         |      |     9 | 26316 |       |   649   (1)| 00:00:08 |

|   3 |    VIEW                |      |  1683 |  4465K|       |     6   (0)| 00:00:01 |

|   4 |     SORT ORDER BY      |      |  1683 |  4465K|  6744K|   979   (1)| 00:00:12 |

|*  5 |      HASH JOIN         |      |  1683 |  4465K|       |    24   (5)| 00:00:01 |

|   6 |       TABLE ACCESS FULL| T4   |    18 | 39168 |       |     3   (0)| 00:00:01 |

|   7 |       TABLE ACCESS FULL| T3   |  1683 |   889K|       |    20   (0)| 00:00:01 |

|*  8 |    TABLE ACCESS FULL   | T5   |     1 |   207 |       |    71   (0)| 00:00:01 |

---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - filter(ROWNUM<10)

   5 - access("T3"."OWNER"="T4"."USERNAME")

   8 - filter("V"."OWNER"="T5"."OWNER" AND "V"."TABLE_NAME"="T5"."OBJECT_NAME")

Note

-----

   - dynamic sampling used for this statement (level=2)

看到在不包含外连接的情况下,在有伪劣的情况下,视图也是没有合并的。

-----在视图无法合并的情况下使用push_pred,进行谓词推进

SQL> select * from (select /*+push_pred*/ * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner(+)=t5.owner and v.table_name(+)=t5.obj

ect_name and rownum<=10;

已选择10行。

执行计划

----------------------------------------------------------

Plan hash value: 857520502

---------------------------------------------------------------------------------

| Id  | Operation                | Name | Rows  | Bytes | Cost (%CPU)| Time     |

---------------------------------------------------------------------------------

|   0 | SELECT STATEMENT         |      |    10 | 29090 |   501K  (3)| 01:40:23 |

|*  1 |  COUNT STOPKEY           |      |       |       |            |          |

|   2 |   NESTED LOOPS OUTER     |      | 21334 |    59M|   501K  (3)| 01:40:23 |

|   3 |    TABLE ACCESS FULL     | T5   | 21334 |  4312K|     2   (0)| 00:00:01 |

|   4 |    VIEW PUSHED PREDICATE |      |     1 |  2702 |    24   (5)| 00:00:01 |

|*  5 |     HASH JOIN            |      |     1 |  2717 |    24   (5)| 00:00:01 |

|*  6 |      TABLE ACCESS FULL   | T4   |     1 |  2176 |     3   (0)| 00:00:01 |

|*  7 |      TABLE ACCESS FULL   | T3   |     1 |   541 |    20   (0)| 00:00:01 |

---------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - filter(ROWNUM<=10)

   5 - access("T3"."OWNER"="T4"."USERNAME")

   6 - filter("T4"."USERNAME"="T5"."OWNER")

   7 - filter("T3"."TABLE_NAME"="T5"."OBJECT_NAME" AND

              "T3"."OWNER"="T5"."OWNER")

Note

-----

   - dynamic sampling used for this statement (level=2)

SQL> select * from (select /*+push_pred*/ * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner(+)=t5.owner and v.table_name(+)=t5.obj

ect_name;

已选择18515行。

执行计划

----------------------------------------------------------

Plan hash value: 1963531859

--------------------------------------------------------------------------------

| Id  | Operation               | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------------

|   0 | SELECT STATEMENT        |      | 21334 |    59M|   501K  (3)| 01:40:24 |

|   1 |  NESTED LOOPS OUTER     |      | 21334 |    59M|   501K  (3)| 01:40:24 |

|   2 |   TABLE ACCESS FULL     | T5   | 21334 |  4312K|    73   (0)| 00:00:01 |

|   3 |   VIEW PUSHED PREDICATE |      |     1 |  2702 |    24   (5)| 00:00:01 |

|*  4 |    HASH JOIN            |      |     1 |  2717 |    24   (5)| 00:00:01 |

|*  5 |     TABLE ACCESS FULL   | T4   |     1 |  2176 |     3   (0)| 00:00:01 |

|*  6 |     TABLE ACCESS FULL   | T3   |     1 |   541 |    20   (0)| 00:00:01 |

--------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   4 - access("T3"."OWNER"="T4"."USERNAME")

   5 - filter("T4"."USERNAME"="T5"."OWNER")

   6 - filter("T3"."TABLE_NAME"="T5"."OBJECT_NAME" AND

              "T3"."OWNER"="T5"."OWNER")

Note

-----

   - dynamic sampling used for this statement (level=2)

-----在不包含外连接的情况下,即时有rownum伪劣,也是无法谓词推进的

SQL> select * from (select /*+push_pred*/ * from t3,t4 where t3.owner=t4.username order by table_name) v,t5 where v.owner=t5.owner and v.table_name=t5.object_na

me and rownum<=10;

已选择10行。

执行计划

----------------------------------------------------------

Plan hash value: 87499378

---------------------------------------------------------------------------------------

| Id  | Operation              | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |

---------------------------------------------------------------------------------------

|   0 | SELECT STATEMENT       |      |    10 | 29240 |       |   792   (1)| 00:00:10 |

|*  1 |  COUNT STOPKEY         |      |       |       |       |            |          |

|   2 |   NESTED LOOPS         |      |    11 | 32164 |       |   792   (1)| 00:00:10 |

|   3 |    VIEW                |      |  1683 |  4465K|       |     7   (0)| 00:00:01 |

|   4 |     SORT ORDER BY      |      |  1683 |  4465K|  6744K|   979   (1)| 00:00:12 |

|*  5 |      HASH JOIN         |      |  1683 |  4465K|       |    24   (5)| 00:00:01 |

|   6 |       TABLE ACCESS FULL| T4   |    18 | 39168 |       |     3   (0)| 00:00:01 |

|   7 |       TABLE ACCESS FULL| T3   |  1683 |   889K|       |    20   (0)| 00:00:01 |

|*  8 |    TABLE ACCESS FULL   | T5   |     1 |   207 |       |    71   (0)| 00:00:01 |

---------------------------------------------------------------------------------------

Predicate Information (identified by operation id):

---------------------------------------------------

   1 - filter(ROWNUM<=10)

   5 - access("T3"."OWNER"="T4"."USERNAME")

   8 - filter("V"."OWNER"="T5"."OWNER" AND "V"."TABLE_NAME"="T5"."OBJECT_NAME")

Note
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: