我在表格中的基本选择中的这个片段是从3秒到8进行查询。任何想法?
case
when @excludeprojtag = 1
then qtyruletag
else 0
end as qtyruletag
编辑: 这里是整个查询
select case when @excludeprojtag = 1 then qtyruletag else 0 end as qtyruletag,listid,quantity
from tb_sales where date between '1/1/2015' and '1/1/2016'
AND CASE WHEN @excludeTestOrders = 1 THEN AccountID ELSE 123 END <> 1234
答案 0 :(得分:0)
USE AdventureWorks -- 2012 version
GO
DECLARE @includezip INT = 0
SELECT @includezip = DATEPART(ss,GETDATE()) % 2
SET STATISTICS TIME ON;
SELECT pa.AddressLine1, pa.City,
CASE -- CASE method
WHEN @includezip = 1
THEN pa.PostalCode
ELSE ''
END AS ZipCode
FROM Person.BusinessEntity pbe
LEFT JOIN Person.BusinessEntityAddress pbea
ON pbe.BusinessEntityID = pbea.BusinessEntityID
LEFT JOIN Person.Address pa
ON pbea.AddressID = pa.AddressID
SELECT pa.AddressLine1, pa.City,
SUBSTRING(pa.PostalCode,1,LEN(pa.PostalCode) * @includezip) AS ZipCode -- No CASE used
FROM Person.BusinessEntity pbe
LEFT JOIN Person.BusinessEntityAddress pbea
ON pbe.BusinessEntityID = pbea.BusinessEntityID
LEFT JOIN Person.Address pa
ON pbea.AddressID = pa.AddressID
GO
SET STATISTICS TIME OFF;
GO
(受影响的20812行)
SQL Server执行时间: CPU时间= 15 ms,经过时间= 102 ms。
(受影响的20812行)
SQL Server执行时间: CPU时间= 16 ms,经过时间= 140 ms。
我经常跑了很多次。在我看来,第二个总是比较慢。当我第一次创建这个时,我认为zip是数字的,但是我忘了加拿大等等。所以如果由于某种原因你反对CASE语句的一般原则,你可以在有二进制参数时使用这个方法。在这里,我只是将其设置为或多或少随机。
答案 1 :(得分:0)
我认为哪里可以改进
select case when @excludeprojtag = 1 then qtyruletag else 0 end as qtyruletag
, listid, quantity
from tb_sales
where date between '1/1/2015' and '1/1/2016'
AND ( @excludeTestOrders <> 1 OR AccountID <> 1234 )