哪个被认为更好?
select *
from TableA
where productDate < '12/9/2013'
or
select *
from TableA
where DATEDIFF(day, productDate, '12/9/2013') > 0
在浏览文章时,我发现在 where 子句中使用日期函数(例如: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()
到专栏将always使表达式不可控制,这意味着它将always必须使用效率较低的扫描(假设这是唯一的搜索谓词)。
至于内嵌评论:
改掉的坏习惯:使用 SELECT * / 省略列列表 https://sqlblog.org/2009/10/10/bad-habits-to-kick-using-select-omitting-the-column-list
要改掉的坏习惯:避免使用 schema 前缀 https://sqlblog.org/2009/10/11/bad-habits-to-kick-avoiding-the-schema-prefix
女士们先生们,请开始使用分号! https://sqlblog.org/2009/09/03/ladies-and-gentlemen-start-your-semi-colons
要戒掉的坏习惯:错误处理日期/范围查询 https://sqlblog.org/2009/10/16/bad-habits-to-kick-mis-handling-date-range-queries
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)