查詢重寫對全外連線無效(二)

yangtingkun發表於2010-10-21

測試發現,Oracle不支援全外連線的查詢重寫,即使物化檢視和查詢語句完全匹配。

查詢重寫對全外連線無效:http://yangtingkun.itpub.net/post/468/507283

繼續測試對外連線物化檢視是否可以重寫。

 

 

上一篇測試發現即使查詢完全匹配,全外連線也無法利用物化檢視進行查詢重寫,那麼問題是FULL OUTER JOIN這種寫法導致的,還是外連線本身的特殊性導致的。

SQL> SET AUTOT ON
SQL> SELECT T1.ID, T2.ID
  2  FROM T1, T2
  3  WHERE T1.ID = T2.ID (+);

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
         1
         0

已選擇9行。

執行計劃
----------------------------------------------------------
Plan hash value: 1823443478

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     9 |   234 |     9  (12)| 00:00:01 |
|*  1 |  HASH JOIN OUTER   |      |     9 |   234 |     9  (12)| 00:00:01 |
|   2 |   TABLE ACCESS FULL| T1   |     9 |   117 |     4   (0)| 00:00:01 |
|   3 |   TABLE ACCESS FULL| T2   |     9 |   117 |     4   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T1"."ID"="T2"."ID"(+))

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


統計資訊
----------------------------------------------------------
          0  recursive calls
          0  db block gets
         15  consistent gets
          0  physical reads
          0  redo size
        716  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed

SQL> CREATE MATERIALIZED VIEW MV_T12
  2  ENABLE QUERY REWRITE AS
  3  SELECT T1.ID ID1, T2.ID ID2
  4  FROM T1, T2
  5  WHERE T1.ID = T2.ID (+);

實體化檢視已建立。

SQL> SELECT T1.ID, T2.ID
  2  FROM T1, T2
  3  WHERE T1.ID = T2.ID (+);

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
         1
         0

已選擇9行。


執行計劃
----------------------------------------------------------
Plan hash value: 183449623

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     9 |   234 |     3   (0)| 00:00:01 |
|   1 |  MAT_VIEW REWRITE ACCESS FULL| MV_T12 |     9 |   234 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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


統計資訊
----------------------------------------------------------
         13  recursive calls
          0  db block gets
         24  consistent gets
          1  physical reads
          0  redo size
        723  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed

對於普通外連線是可以進行查詢重寫的,檢查是否是SQL標準語法導致的問題:

SQL> SELECT T1.ID, T2.ID
  2  FROM T1 LEFT OUTER JOIN T2
  3  ON T1.ID = T2.ID;

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
         1
         0

已選擇9行。


執行計劃
----------------------------------------------------------
Plan hash value: 183449623

---------------------------------------------------------------------------------------
| Id  | Operation                    | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |        |     9 |   234 |     3   (0)| 00:00:01 |
|   1 |  MAT_VIEW REWRITE ACCESS FULL| MV_T12 |     9 |   234 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------

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


統計資訊
----------------------------------------------------------
         10  recursive calls
          0  db block gets
         24  consistent gets
          0  physical reads
          0  redo size
        723  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
          9  rows processed

和標準語法也沒有關係,檢查全外連線能否使用這個物化檢視進行查詢重寫:

SQL> SELECT T1.ID, T2.ID
  2  FROM T1 FULL OUTER JOIN T2
  3  ON T1.ID = T2.ID;

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
                    9
                   10
         1
         0

已選擇11行。


執行計劃
----------------------------------------------------------
Plan hash value: 53297166

----------------------------------------------------------------------------------
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |          |     9 |   234 |     9  (12)| 00:00:01 |
|   1 |  VIEW                 | VW_FOJ_0 |     9 |   234 |     9  (12)| 00:00:01 |
|*  2 |   HASH JOIN FULL OUTER|          |     9 |   234 |     9  (12)| 00:00:01 |
|   3 |    TABLE ACCESS FULL  | T1       |     9 |   117 |     4   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL  | T2       |     9 |   117 |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1"."ID"="T2"."ID")

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


統計資訊
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        733  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         11  rows processed

SQL> SELECT /*+ REWRITE (MV_T12) */ T1.ID, T2.ID
  2  FROM T1 FULL OUTER JOIN T2
  3  ON T1.ID = T2.ID;

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
                    9
                   10
         1
         0

已選擇11行。


執行計劃
----------------------------------------------------------
Plan hash value: 53297166

----------------------------------------------------------------------------------
| Id  | Operation             | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------
|   0 | SELECT STATEMENT      |          |     9 |   234 |     9  (12)| 00:00:01 |
|   1 |  VIEW                 | VW_FOJ_0 |     9 |   234 |     9  (12)| 00:00:01 |
|*  2 |   HASH JOIN FULL OUTER|          |     9 |   234 |     9  (12)| 00:00:01 |
|   3 |    TABLE ACCESS FULL  | T1       |     9 |   117 |     4   (0)| 00:00:01 |
|   4 |    TABLE ACCESS FULL  | T2       |     9 |   117 |     4   (0)| 00:00:01 |
----------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T1"."ID"="T2"."ID")

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


統計資訊
----------------------------------------------------------
          7  recursive calls
          0  db block gets
         31  consistent gets
          0  physical reads
          0  redo size
        733  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         11  rows processed

SQL> SELECT /*+ NO_NATIVE_FULL_OUTER_JOIN */ T1.ID, T2.ID
  2  FROM T1 FULL OUTER JOIN T2
  3  ON T1.ID = T2.ID;

        ID         ID
---------- ----------
         2          2
         3          3
         4          4
         5          5
         6          6
         7          7
         8          8
         1
         0
                   10
                    9

已選擇11行。


執行計劃
----------------------------------------------------------
Plan hash value: 665903394

-----------------------------------------------------------------------------------------
| Id  | Operation                      | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT               |        |    18 |   468 |    12   (9)| 00:00:01 |
|   1 |  VIEW                          |        |    18 |   468 |    12   (9)| 00:00:01 |
|   2 |   UNION-ALL                    |        |       |       |            |          |
|   3 |    MAT_VIEW REWRITE ACCESS FULL| MV_T12 |     9 |   234 |     3   (0)| 00:00:01 |
|*  4 |    HASH JOIN ANTI              |        |     9 |   234 |     9  (12)| 00:00:01 |
|   5 |     TABLE ACCESS FULL          | T2     |     9 |   117 |     4   (0)| 00:00:01 |
|   6 |     TABLE ACCESS FULL          | T1     |     9 |   117 |     4   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   4 - access("T1"."ID"="T2"."ID")

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


統計資訊
----------------------------------------------------------
         22  recursive calls
          0  db block gets
         70  consistent gets
          0  physical reads
          0  redo size
        733  bytes sent via SQL*Net to client
        520  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
         11  rows processed

直接利用REWRITE提示,Oracle不會利用物化檢視進行重新,但是如果利用NO_NATIVE_FULL_OUTER_JOIN提示,那麼Oracle會利用建立的物化檢視進行查詢重寫。

看來問題和metalink文件Bug 7032300描述的現象基本一致,看來這個bug11.2中仍然沒有完全解決。

 

 

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

相關文章