巨大的性能差异:使用 sysdate 与使用预格式化日期
为什么这两个查询之间的性能差异如此之大?
-- (89 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < sysdate - 5
与
-- (0.6 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < TO_DATE('05/27/2011 03:13:00', 'MM/DD/YYYY HH24:MI:SS') -- 5 days ago
无论索引如何,似乎 to_date 和 sysdate 都只返回“某个日期值”。
注意:该表上存在一个复合索引,包括 eqpid 和其他 2 列。 mydate 还存在一个索引。两者都是b树。大约有 2900 万行。
为什么优化器会为这些选择如此明显不同(并且在一种情况下非常糟糕)的计划?
Why is there such a huge performance difference between these two queries?
-- (89 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < sysdate - 5
vs.
-- (0.6 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < TO_DATE('05/27/2011 03:13:00', 'MM/DD/YYYY HH24:MI:SS') -- 5 days ago
Regardless of indexes, it seems that both to_date and sysdate just return "some date value".
Notes: A composite index exists on this table including eqpid and 2 other columns.
An index also exists for mydate. Both are b-tree. There are about 29 million rows.
Why would the optimizer choose such an obviously different (and in one case, awful) plan for these?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Jonathan Lewis 撰写了有关 9i 中
sysdate
问题的文章;看看“令人惊讶的 sysdate”部分 此处。本质上,sysdate
上的算术似乎让优化器感到困惑,因此在这种情况下,它认为mydate
上的索引更具选择性。但这似乎是一个相当极端的例子。 (最初是从一个不太相关的“问汤姆”帖子中指出这个方向的)。Jonathan Lewis has written about issues with
sysdate
in 9i; have a look at the 'surprising sysdate' section here, for example. Essentially the arithmetic onsysdate
seems to confuse the optimizer, so in this case it thinks the index onmydate
is more selective. This seems like quite an extreme example though. (Originally pointed in this direction from a not-really-related Ask Tom post).我不了解 Oracle,但在 Postgresql 中,忽略索引的一个可能原因是类型不匹配。也许直接使用
- 5
会使 Oracle 认为 rhs 是数字。您可以在sysdate - 5
上进行迄今为止的转换(或者任何 mydate 的精确类型)吗?I don't know Oracle, but in Postgresql a possible source of ignoring an index is mismatched types. Maybe doing the straight
- 5
makes Oracle think the rhs is numeric. Can you do a cast to date (or whatever is the exact type of mydate) onsysdate - 5
?