为什么子选择的查询成本低于Oracle中的常量查询

Hus*_*110 6 sql oracle toad oracle11g

我有一个包含数百万条目的SQL表,我试图查询超过60天的条目数(Oracle 11.2.0.1.0).

对于这个实验,我使用了3个不同版本的select语句:(
成本值由TOAD for Oracle V. 9.7.2.5给出)

  1. select count(*) from fman_file
    where dateadded >= (select sysdate - 60 from dual)

    费用:65

  2. select count(*) from fman_file
    where dateadded >= sysdate - 60

    费用:1909年

  3. select count(*) from fman_file
    where dateadded >= sysdate - numtodsinterval(60,'day')

    费用:1884年

  4. select count(*) from fman_file where dateadded >= '10.10.2009'
    费用:1823年
    (2009 年10月10日只是一个例子日期!!!)

我没有为所有查询准确的时间值,但第一个确实是最快的.

所以我尝试了一些其他子选择的更多选择查询(如(从双精度中选择1000))并且它们(有时候是WAY)比具有常量值的其他子选择更快.甚至看起来这个"WHATEVER"(Bug/Feature)也在MySQL中发生.

那么有谁能告诉我为什么第一个查询(方式)比其他查询更快?

格尔茨

PS:这不是关于sydate!问题是为什么变化与(选择)比其他人更快?(稍微关注Select-Variation(1.)与Constant-Variation(4.))

Hog*_*gan 0

您是否在 >= 之后用 () 重新尝试了数字 2-4 的计算——在我看来,第一个语句是唯一一次计算该值的语句——对于所有其他语句,它在每一行重新计算。例如:

select count(*) from fman_file where dateadded >= (SELECT sysdate - 60) 

select count(*) from fman_file where dateadded >= (SELECT (sysdate - numtodsinterval(60,'day'))

select count(*) from fman_file where dateadded >= (SELECT CONVERT(datetime,'10.10.2009')) 
Run Code Online (Sandbox Code Playgroud)

注意——不知道在 Oracle 中转换为日期时间的语法——但你明白了。