gpt4 book ai didi

sql-server - 比较日期 < 或 DateDiff 哪个更好?

转载 作者:行者123 更新时间:2023-12-03 16:43:26 31 4
gpt4 key购买 nike

哪个被认为更好?

select *
from TableA
where productDate < '12/9/2013'

或者
select *
from TableA
where DATEDIFF(day, productDate, '12/9/2013') > 0

在浏览文章时,我读到在 where 子句中使用 Date 函数(例如:datediff、dateadd)会对性能产生负面影响。这是真的?

最佳答案

最好的可能是:

SELECT <column list> -- not * (1)
FROM dbo.TableA -- please always specify schema (2)
WHERE productDate < '20131209'; -- always use a semi-colon (3)
-- and always use a non-regional, unambiguous date format (4)

这将是最好的原因是因为它为优化器提供了在 productDate 上使用索引的最佳机会。柱子。即使今天不存在索引,明天也可能有人会添加一个。应用函数,如 DATEDIFF()到列将始终使表达式不可搜索,这意味着它将始终必须使用效率较低的扫描(假设这是唯一的搜索谓词)。

至于内联评论:
  • Bad habits to kick : using SELECT * / omitting the column list
  • Bad habits to kick : avoiding the schema prefix
  • Ladies and gentlemen, start your semi-colons!
  • Bad habits to kick : mis-handling date / range queries
  • 关于sql-server - 比较日期 < 或 DateDiff 哪个更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20476637/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com