Oracle db中出现意外的查询结果

时间:2018-06-06 09:18:04

标签: sql oracle

我们有Oracle 12.2.0.1.0数据库。我们创建一个这样的简单表:

CREATE TABLE TABLE1 (DATE1 TIMESTAMP (6));
INSERT INTO TABLE1 VALUES (TIMESTAMP'2018-05-30 00:00:00');
INSERT INTO TABLE1 VALUES (TIMESTAMP'2018-05-30 00:00:00');

当我们使用以下两个select语句进行查询时,我们会得到不同的结果。第一个按预期返回两行,而第二个不按预期返回。

SELECT T1.*, NVL(T2.DATE1, TIMESTAMP'1900-01-01 00:00:00')
FROM TABLE1 T1
LEFT JOIN TABLE1 T2
ON 1 = 0
WHERE T1.DATE1 > NVL(T2.DATE1, TIMESTAMP'1900-01-01 00:00:00');

SELECT T1.*, NVL(T2.DATE1, TIMESTAMP'1900-01-01 00:00:00')
FROM TABLE1 T1
LEFT JOIN TABLE1 T2
ON T1.DATE1 || '---' = '-'
WHERE T1.DATE1 > NVL(T2.DATE1, TIMESTAMP'1900-01-01 00:00:00');

T1和T2是相同的TABLE1。我们正在加入它。

请告知原因。感谢。

1 个答案:

答案 0 :(得分:0)

似乎优化器混淆了这么多级别的混淆连接条件。

第一个查询产生以下执行计划:

SQL_ID  9k6g3m0xs31w7, child number 1
-------------------------------------
select t1.*, nvl(t2.date1, timestamp'1900-01-01 00:00:00') from table1 
t1   left join table1 t2 on 1 = 0 where t1.date1 > nvl(t2.date1, 
timestamp'1900-01-01 00:00:00')

Plan hash value: 963482612

-----------------------------------------------------------------------------------------------------------
| Id  | Operation         | Name   | Starts | E-Rows |E-Bytes| Cost (%CPU)| A-Rows |   A-Time   | Buffers |
-----------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |        |      1 |        |       |     3 (100)|      2 |00:00:00.01 |       7 |
|*  1 |  TABLE ACCESS FULL| TABLE1 |      1 |      2 |    26 |     3   (0)|      2 |00:00:00.01 |       7 |
-----------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$F7AF7B7D / T1@SEL$1

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

   1 - filter("T1"."DATE1">TIMESTAMP' 1900-01-01 00:00:00.000000000')

因此,规划器正确地看到自连接是不必要的,并且用连接表本身上的条件替换连接表上的NVL()条件。

显然这是"取代"条件在12.2中无法正常工作。

第二个查询产生以下计划:

SQL_ID  3twykk3kcyyxy, child number 1
-------------------------------------
select t1.*, nvl(t2.date1, timestamp'1900-01-01 00:00:00') from table1 
t1 left join table1 t2   on t1.date1 || '---' = '-' where t1.date1 > 
nvl(t2.date1, timestamp'1900-01-01 00:00:00')

Plan hash value: 736255932

----------------------------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation              | Name            | Starts | E-Rows |E-Bytes| Cost (%CPU)| A-Rows |   A-Time   | Buffers |  OMem |  1Mem | Used-Mem |
----------------------------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |                 |      1 |        |       |     8 (100)|      0 |00:00:00.01 |       7 |       |       |          |
|*  1 |  FILTER                |                 |      1 |        |       |            |      0 |00:00:00.01 |       7 |       |       |          |
|   2 |   MERGE JOIN OUTER     |                 |      1 |      1 |    26 |     8  (25)|      2 |00:00:00.01 |       7 |       |       |          |
|   3 |    SORT JOIN           |                 |      1 |      2 |    26 |     4  (25)|      2 |00:00:00.01 |       7 |  2048 |  2048 | 2048  (0)|
|   4 |     TABLE ACCESS FULL  | TABLE1          |      1 |      2 |    26 |     3   (0)|      2 |00:00:00.01 |       7 |       |       |          |
|*  5 |    SORT JOIN           |                 |      2 |      2 |    26 |     4  (25)|      0 |00:00:00.01 |       0 |  1024 |  1024 |          |
|   6 |     VIEW               | VW_LAT_C83A7ED5 |      2 |      2 |    26 |     3   (0)|      0 |00:00:00.01 |       0 |       |       |          |
|*  7 |      FILTER            |                 |      2 |        |       |            |      0 |00:00:00.01 |       0 |       |       |          |
|   8 |       TABLE ACCESS FULL| TABLE1          |      0 |      2 |    26 |     3   (0)|      0 |00:00:00.01 |       0 |       |       |          |
----------------------------------------------------------------------------------------------------------------------------------------------------

Query Block Name / Object Alias (identified by operation id):
-------------------------------------------------------------

   1 - SEL$F7AF7B7D
   4 - SEL$F7AF7B7D / T1@SEL$1
   6 - SEL$BCD4421C / VW_LAT_AE9E49E8@SEL$AE9E49E8
   7 - SEL$BCD4421C
   8 - SEL$BCD4421C / T2@SEL$1

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

   1 - filter("T1"."DATE1">NVL("ITEM_1",TIMESTAMP' 1900-01-01 00:00:00.000000000'))
   5 - access(INTERNAL_FUNCTION("T1"."DATE1")>NVL("ITEM_1",TIMESTAMP' 1900-01-01 00:00:00.000000000'))
   7 - filter(INTERNAL_FUNCTION("T1"."DATE1")||'---'='-')

因此,优化器用一些ITEM1占位符替换了对表列的引用 - 并且步骤access(INTERNAL_FUNCTION("T1"."DATE1")>NVL("ITEM_1",TIMESTAMP' 1900-01-01 00:00:00.000000000'))使事情变得混乱。

对于12.1,计划本质上是相同的,唯一的区别是谓词中缺少access()部分,所以我猜这个替换在12.2中有些错误(确切地说,我的版本是:12.2。 0.1.0)