【发布时间】:2019-03-20 14:18:47
【问题描述】:
我一次又一次地被告知优化查询的最佳方法是删除 where 子句中的函数。函数会导致全表扫描。所以我的问题是,鉴于这三个查询,为什么具有 where 子句(图表 C)中的函数的查询执行得最好——快两倍以上?
图表 A - with 中的文本大写,因此 where 子句中没有任何功能,但仍将 where 子句放在 select 中
With Code As (
Select owner,name,Upper(text) As text,line From all_source
)
select * from Code
Where text Like '%UPPER(%'
And owner='COMMISSIONS'
图表 B - 将 with 中的文本大写并尽可能多地过滤
With Code As (
Select owner,name,Upper(text) As text,line From all_source
Where owner='COMMISSIONS'
)
select * from Code
Where text Like '%UPPER(%'
图表 C - 没有 with 子句和 where 子句中的函数
select * from all_source
Where upper(text) Like '%UPPER(%'
And owner='COMMISSIONS'
【问题讨论】:
-
我猜你在
owner上有一个索引,这三个的性能非常相似。 -
为了正确回答您的问题,我们需要知道表
all_source有哪些索引。如果没有这些信息,我们几乎无法猜测。 -
例如,如果您创建以下索引,您的查询 #3 可能会更快:
create index ix1 on all_source (owner, upper(text));。做起来看看效果如何。