列索引未反映在解释计划中带有“ IN”语句的谓词

时间:2018-11-20 14:11:59

标签: database oracle oracle11g

我有一个表,其列名称为 IDENTIFIER ,并且表( TAB1 )对此列具有索引。每当我尝试使用具有单个值的简单where子句查询单个数据时,解释计划就会显示它正在利用该特定列上的现有索引。

但是,只要我在另一个表中都有值列表,就说一个临时表( TEMP_IDENTIFIER ),其中要列出所有要查询的标识符,并且当我使用 IN子句,我可以看到解释计划没有考虑索引,而是对表执行了全表扫描

理想情况下,我希望第二个查询也利用现有索引

请同时找到两个查询并说明计划


查询1

explain plan for
select * from schemaowner.TAB1 
where IDENTIFIER = 'A'; 

说明计划

Plan hash value: 4172144893

------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name             | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                  |    51 | 12750 |    11   (0)| 00:00:01 |
|   1 |  TABLE ACCESS BY INDEX ROWID| TAB1             |    51 | 12750 |    11   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN          | COL_INDEX        |    51 |       |     4   (0)| 00:00:01 |
------------------------------------------------------------------------------------------------

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

   2 - access("IDENTIFIER"='A')

查询2

explain plan for
select * from schemaowner.TAB1 
where IDENTIFIER in (select IDENTIFIER from SCHEMAOWNER.temp_IDENTIFIER);

说明计划:

Plan hash value: 935676029

-------------------------------------------------------------------------------------------------
| Id  | Operation            | Name             | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT     |                  |  3135K|   822M|       | 74751   (1)| 00:14:58 |
|*  1 |  HASH JOIN RIGHT SEMI|                  |  3135K|   822M|  2216K| 74751   (1)| 00:14:58 |
|   2 |   TABLE ACCESS FULL  | TEMP_IDENTIFIER  | 61115 |  1492K|       |    85   (2)| 00:00:02 |
|   3 |   TABLE ACCESS FULL  | TAB1             |  3745K|   893M|       | 28028   (2)| 00:05:37 |
-------------------------------------------------------------------------------------------------

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

   1 - access("IDENTIFIER"="IDENTIFIER")

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

1 个答案:

答案 0 :(得分:0)

那就是优化器的美。已经弄清楚(或花费)SEMI连接是最有效的方法:)