1、问题
一个查询未走索引,从一个视图中查询,视图定义为5张表的union all。
有明显的条件TMH12 = '1001271876'
2、排查
@?/rdbms/admin/sqltrpt 脚本生成优化建议
通过建议来看,是生成索引的建议,对列进行格式转换,TO_CHAR("TMH12"),
1- Index Finding (see explain plans section below)
The execution plan of this statement can be improved by creating one or more
indices.
Recommendation (estimated benefit: 99.48%)
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index HICANO_POS.IDX
$$ _20260001 on HICANO_POS.E_CPHPDA(TO_CHAR("TMH12")); - Consider running the Access Advisor to improve the physical schema design or creating the recommended index. create index HICANO_POS.IDX $$
_20260002 on
HICANO_POS.E_CPXSDA(TO_CHAR("TMH12"));
- Consider running the Access Advisor to improve the physical schema design
or creating the recommended index.
create index HICANO_POS.IDX
$$ _20260003 on HICANO_POS.E_JLZCPCPXXMXB("TMH8"); - Consider running the Access Advisor to improve the physical schema design or creating the recommended index. create index HICANO_POS.IDX $$
_20260004 on
HICANO_POS.E_CPTHDA(TO_CHAR("TMH12"));
我们先不急去创建这样的索引,先看看为什么进行了格式转换,从执行计划中也看到了filter进行了to_char转换,
查看表定义发现有三张表的TMH2字段是number类型的,第四张表对应的union字段是varchar类型的,
实际上是牺牲了3个表的字段转换,去适应一张表的varchar类型,要么从表涉及字段上统一,要么牺牲一张表去换三张表的性能。
3、调整
调整后由原来的30ms下降到11ms,
Elapsed: 00:00:00.30--》Elapsed: 00:00:00.10,提升2倍速度,执行计划time由25s下降到1s,cost从2049 下降到21
4、总结
字段类型转换会造成无法使用索引的情况,如果非要进行转换,可以创建带有转换类型的索引,函数索引。