首页 > Linux操作系统 > Linux操作系统 > 基于函数的索引(序)
分析表的语句
analyze table tt compute statistics
for table
for all indexed columns
for all indexes
/
所有的索引对DML语句都有影响。
在Oracle8i以前的版本中,基于函数的索引不能在内置的函数TO_DATE上创建索引、以后得到修复
6.例子,索引了一个字符列,在查询时,此列的值为数字型
select * from b1 where x=5;
SQL> create table b1 (x char(1) primary key);
表已创建。
SQL> insert into b1 values('5');
已创建 1 行。
SQL> set autotrace on exp
SQL>
;
X
-
5
已用时间: 00: 00: 00.02
执行计划
----------------------------------------------------------
Plan hash value: 2884419779
--------------------------------------------------------------------------------
-
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
-
| 0 | SELECT STATEMENT | | 1 | 3 | 2 (0)| 00:00:01
|
|* 1 | INDEX FULL SCAN | SYS_C0011267 | 1 | 3 | 2 (0)| 00:00:01
|
--------------------------------------------------------------------------------
-
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(TO_NUMBER("X")=5)
Note
这里是索引全表扫描,显然没有使用索引
改为如下查询
SQL> select * from b1 where x='5';
X
-
5
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1001603515
--------------------------------------------------------------------------------
--
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|
--------------------------------------------------------------------------------
--
| 0 | SELECT STATEMENT | | 1 | 3 | 1 (0)| 00:00:01
|
|* 1 | INDEX UNIQUE SCAN| SYS_C0011267 | 1 | 3 | 1 (0)| 00:00:01
|
--------------------------------------------------------------------------------
--
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("X"='5')
SQL>
从x=5 到x='5' 其实oracle 做了一个隐式转化 相当于 to_number('5') = 5 ,那么在索引 to_number('X') 是没有所索引的,索引执行了全表扫描
应该注意隐式转化带来的索引无效
在插入或者更新大量数据后,一定要分析表,否则oracle会选择错误的执行计划,没有最新的统计,CBO不能做出正确的判断。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7551038/viewspace-617728/ ,如需转载,请注明出处,否则将追究法律责任。