这个日期比较条件在SQL中是否可以SARG?

[英]Is this date comparison condition SARG-able in SQL?


Is this condition sargable?

这种情况是否可以接受?

AND  DATEDIFF(month,p.PlayerStatusLastTransitionDate,@now) BETWEEN 1 AND 7)

My rule of thumb is that a function on the left makes condition non sargable.. but in some places I have read that BETWEEN clause is sargable. So does any one know for sure?

我的经验法则是左边的一个函数使条件不可思议..但在某些地方我已经读过BETWEEN子句是sargable。那么任何人都知道吗?

For reference:

以供参考:

NOTE: If any guru ends here, please do update Sargable Wikipedia page. I updated it a little bit but I am sure it can be improved more :)

注意:如果任何大师在这里结束,请更新Sargable Wikipedia页面。我更新了一点,但我相信它可以改进:)

2 个解决方案

#1


19  

Using AdventureWorks, if we look at these two equivalent queries:

使用AdventureWorks,如果我们查看这两个等效查询:

SELECT OrderDate FROM Sales.SalesOrderHeader
WHERE DATEDIFF(month,OrderDate,GETDATE()) BETWEEN 1 AND 7;

SELECT OrderDate FROM Sales.SalesOrderHeader
WHERE OrderDate >= DATEADD(MONTH, -7, GETDATE())
  AND OrderDate <= DATEADD(MONTH, -1, GETDATE());

In both cases we see a clustered index scan:

在这两种情况下,我们都会看到聚集索引扫描:

enter image description here

But notice the recommended/missing index only on the latter query, since it's the only one that could benefit from it:

但请注意后一个查询的推荐/缺失索引,因为它是唯一可以从中受益的索引:

enter image description here

If we add an index to the OrderDate column, then run the queries again:

如果我们向OrderDate列添加索引,则再次运行查询:

CREATE INDEX dt ON Sales.SalesOrderHeader(OrderDate);
GO

SELECT OrderDate FROM Sales.SalesOrderHeader
WHERE DATEDIFF(month,OrderDate,GETDATE()) BETWEEN 1 AND 7;

SELECT OrderDate FROM Sales.SalesOrderHeader
WHERE OrderDate >= DATEADD(MONTH, -7, GETDATE())
  AND OrderDate <= DATEADD(MONTH, -1, GETDATE());

We see much difference - the latter uses a seek:

我们看到了很大的不同 - 后者使用了寻求:

enter image description here

enter image description here

Notice too how the estimates are way off for your version of the query. This can be absolutely disastrous on a large data set.

另请注意您的查询版本的估算方法。这对于大型数据集来说绝对是灾难性的。

There are very few cases where a function or other expression applied to the column will be sargable. One case I know of is CONVERT(DATE, datetime_column) - but that particular optimization is undocumented, and I recommend staying away from it anyway. Not only because you'd be implicitly suggesting that using functions/expressions against columns is okay (it's not in every other scenario), but also because it can lead to wasted reads and disastrous estimates.

极少数情况下,应用于列的函数或其他表达式将是可搜索的。我所知道的一个案例是CONVERT(DATE,datetime_column) - 但是这个特定的优化是没有记录的,我建议远离它。不仅因为你暗中暗示对列使用函数/表达式是可以的(它不是在所有其他场景中),而且还因为它可能导致浪费的读取和灾难性的估计。

#2


8  

I would be very surprised if that was sargable. One option might be to rewrite it as:

如果这是可以理解的,我会非常惊讶。一种选择可能是将其重写为:

WHERE p.PlayerStatusLastTransitionDate >= DATEADD(month,1,CAST(@now AS DATE))
AND   p.PlayerStatusLastTransitionDate <= DATEADD(month,7,CAST(@now AS DATE))

Which I believe will be sargable (even though it's not quite as pretty).

我认为这将是可怜的(即使它不是那么漂亮)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/06/01/1892bc36b3e9dadfa908269ba3fcc51d.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号