如果我在IS NULL
子句中使用WHERE
,但当我将其更改为IS NOT NULL
时,我有一个查询返回的结果不到一秒(115行)
它需要1分16秒(129行)。我正在测试的NULL
列已建立索引,我只是无法弄清楚为什么2个WHERE子句应该有如此大的差异,有人可以看到我在哪里出问题了吗?
我正在使用SQL Server 2012
SELECT position.counterparty,
position.positiontype,
contract.contract,
Trade.cstcontractstart AS ContractStartDate,
Trade.cstcontractend AS ContractEndDate,
Trade.trade AS TradeID,
Trade.tradedate AS TradeDate,
powerquantity.begtime AS StartDateTime,
powerquantity.endtime AS EndDateTime,
CASE
WHEN CAST((powerquantity.endtime - powerquantity.begtime) AS int) <= 31 THEN 'M'
WHEN CAST((powerquantity.endtime - powerquantity.begtime) AS int) > 31
AND CAST((powerquantity.endtime - powerquantity.begtime) AS int) <= 93 THEN 'Q'
WHEN CAST((powerquantity.endtime - powerquantity.begtime) AS int) > 93
AND CAST((powerquantity.endtime - powerquantity.begtime) AS int) <= 183 THEN 'S'
ELSE 'M'
END AS BlockDescription,
CAST((powerquantity.endtime - powerquantity.begtime) AS int) AS Days,
CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0 AS HoursInPeriod,
dbo.WorkingDays(powerquantity.begtime, powerquantity.endtime) AS WorkingDays,
dbo.WorkingDays(powerquantity.begtime, powerquantity.endtime) * 24 AS WorkingHours,
SUM(CASE WHEN LEFT(powerquantity.tsperiod, 2) = 'WD' THEN 1 ELSE 0 END) AS WD_EFA_Periods,
SUM(CASE WHEN LEFT(powerquantity.tsperiod, 2) = 'WE' THEN 1 ELSE 0 END) AS WE_EFA_Periods,
CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0 - (dbo.WorkingDays(powerquantity.begtime, powerquantity.endtime) * 24) AS WeekendHours,
CASE
WHEN fee.feemode = 'FIXED' THEN CASE
WHEN position.unit = 'MW' THEN CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he1) * fee.pricediff
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he25) * fee.pricediff
ELSE (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he2) * fee.pricediff
END
WHEN 2 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he3) * fee.pricediff
WHEN 3 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he4) * fee.pricediff
WHEN 4 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he5) * fee.pricediff
WHEN 5 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he6) * fee.pricediff
WHEN 6 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he7) * fee.pricediff
WHEN 7 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he8) * fee.pricediff
WHEN 8 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he9) * fee.pricediff
WHEN 9 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he10) * fee.pricediff
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he11) * fee.pricediff
WHEN 11 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he12) * fee.pricediff
WHEN 12 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he13) * fee.pricediff
WHEN 13 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he14) * fee.pricediff
WHEN 14 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he15) * fee.pricediff
WHEN 15 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he16) * fee.pricediff
WHEN 16 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he17) * fee.pricediff
WHEN 17 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he18) * fee.pricediff
WHEN 18 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he19) * fee.pricediff
WHEN 19 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he20) * fee.pricediff
WHEN 20 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he21) * fee.pricediff
WHEN 21 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he22) * fee.pricediff
WHEN 22 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he23) * fee.pricediff
WHEN 23 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he24) * fee.pricediff
END
ELSE CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN MAX(powerquantity.he1) * fee.pricediff
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN MAX(powerquantity.he25) * fee.pricediff
ELSE MAX(powerquantity.he2) * fee.pricediff
END
WHEN 2 THEN MAX(powerquantity.he3) * fee.pricediff
WHEN 3 THEN MAX(powerquantity.he4) * fee.pricediff
WHEN 4 THEN MAX(powerquantity.he5) * fee.pricediff
WHEN 5 THEN MAX(powerquantity.he6) * fee.pricediff
WHEN 6 THEN MAX(powerquantity.he7) * fee.pricediff
WHEN 7 THEN MAX(powerquantity.he8) * fee.pricediff
WHEN 8 THEN MAX(powerquantity.he9) * fee.pricediff
WHEN 9 THEN MAX(powerquantity.he10) * fee.pricediff
WHEN 10 THEN MAX(powerquantity.he11) * fee.pricediff
WHEN 11 THEN MAX(powerquantity.he12) * fee.pricediff
WHEN 12 THEN MAX(powerquantity.he13) * fee.pricediff
WHEN 13 THEN MAX(powerquantity.he14) * fee.pricediff
WHEN 14 THEN MAX(powerquantity.he15) * fee.pricediff
WHEN 15 THEN MAX(powerquantity.he16) * fee.pricediff
WHEN 16 THEN MAX(powerquantity.he17) * fee.pricediff
WHEN 17 THEN MAX(powerquantity.he18) * fee.pricediff
WHEN 18 THEN MAX(powerquantity.he19) * fee.pricediff
WHEN 19 THEN MAX(powerquantity.he20) * fee.pricediff
WHEN 20 THEN MAX(powerquantity.he21) * fee.pricediff
WHEN 21 THEN MAX(powerquantity.he22) * fee.pricediff
WHEN 22 THEN MAX(powerquantity.he23) * fee.pricediff
WHEN 23 THEN MAX(powerquantity.he24) * fee.pricediff
END
END
ELSE CASE
WHEN position.unit = 'MW' THEN CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he1) * fee.priceindex
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he25) * fee.priceindex
ELSE (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he2) * fee.priceindex
END
WHEN 2 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he3) * fee.priceindex
WHEN 3 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he4) * fee.priceindex
WHEN 4 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he5) * fee.priceindex
WHEN 5 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he6) * fee.priceindex
WHEN 6 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he7) * fee.priceindex
WHEN 7 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he8) * fee.priceindex
WHEN 8 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he9) * fee.priceindex
WHEN 9 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he10) * fee.priceindex
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he11) * fee.priceindex
WHEN 11 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he12) * fee.priceindex
WHEN 12 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he13) * fee.priceindex
WHEN 13 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he14) * fee.priceindex
WHEN 14 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he15) * fee.priceindex
WHEN 15 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he16) * fee.priceindex
WHEN 16 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he17) * fee.priceindex
WHEN 17 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he18) * fee.priceindex
WHEN 18 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he19) * fee.priceindex
WHEN 19 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he20) * fee.priceindex
WHEN 20 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he21) * fee.priceindex
WHEN 21 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he22) * fee.priceindex
WHEN 22 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he23) * fee.priceindex
WHEN 23 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he24) * fee.priceindex
END
ELSE CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN MAX(powerquantity.he1) * fee.priceindex
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN MAX(powerquantity.he25) * fee.priceindex
ELSE MAX(powerquantity.he2) * fee.priceindex
END
WHEN 2 THEN MAX(powerquantity.he3) * fee.priceindex
WHEN 3 THEN MAX(powerquantity.he4) * fee.priceindex
WHEN 4 THEN MAX(powerquantity.he5) * fee.priceindex
WHEN 5 THEN MAX(powerquantity.he6) * fee.priceindex
WHEN 6 THEN MAX(powerquantity.he7) * fee.priceindex
WHEN 7 THEN MAX(powerquantity.he8) * fee.priceindex
WHEN 8 THEN MAX(powerquantity.he9) * fee.priceindex
WHEN 9 THEN MAX(powerquantity.he10) * fee.priceindex
WHEN 10 THEN MAX(powerquantity.he11) * fee.priceindex
WHEN 11 THEN MAX(powerquantity.he12) * fee.priceindex
WHEN 12 THEN MAX(powerquantity.he13) * fee.priceindex
WHEN 13 THEN MAX(powerquantity.he14) * fee.priceindex
WHEN 14 THEN MAX(powerquantity.he15) * fee.priceindex
WHEN 15 THEN MAX(powerquantity.he16) * fee.priceindex
WHEN 16 THEN MAX(powerquantity.he17) * fee.priceindex
WHEN 17 THEN MAX(powerquantity.he18) * fee.priceindex
WHEN 18 THEN MAX(powerquantity.he19) * fee.priceindex
WHEN 19 THEN MAX(powerquantity.he20) * fee.priceindex
WHEN 20 THEN MAX(powerquantity.he21) * fee.priceindex
WHEN 21 THEN MAX(powerquantity.he22) * fee.priceindex
WHEN 22 THEN MAX(powerquantity.he23) * fee.priceindex
WHEN 23 THEN MAX(powerquantity.he24) * fee.priceindex
END
END
END AS BlockCostGBP,
CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN MAX(powerquantity.he1)
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN MAX(powerquantity.he25)
ELSE MAX(powerquantity.he2)
END
WHEN 2 THEN MAX(powerquantity.he3)
WHEN 3 THEN MAX(powerquantity.he4)
WHEN 4 THEN MAX(powerquantity.he5)
WHEN 5 THEN MAX(powerquantity.he6)
WHEN 6 THEN MAX(powerquantity.he7)
WHEN 7 THEN MAX(powerquantity.he8)
WHEN 8 THEN MAX(powerquantity.he9)
WHEN 9 THEN MAX(powerquantity.he10)
WHEN 10 THEN MAX(powerquantity.he11)
WHEN 11 THEN MAX(powerquantity.he12)
WHEN 12 THEN MAX(powerquantity.he13)
WHEN 13 THEN MAX(powerquantity.he14)
WHEN 14 THEN MAX(powerquantity.he15)
WHEN 15 THEN MAX(powerquantity.he16)
WHEN 16 THEN MAX(powerquantity.he17)
WHEN 17 THEN MAX(powerquantity.he18)
WHEN 18 THEN MAX(powerquantity.he19)
WHEN 19 THEN MAX(powerquantity.he20)
WHEN 20 THEN MAX(powerquantity.he21)
WHEN 21 THEN MAX(powerquantity.he22)
WHEN 22 THEN MAX(powerquantity.he23)
WHEN 23 THEN MAX(powerquantity.he24)
END AS Volume,
CASE
WHEN position.unit = 'MW' THEN CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he1)
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he25)
ELSE (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he2)
END
WHEN 2 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he3)
WHEN 3 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he4)
WHEN 4 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he5)
WHEN 5 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he6)
WHEN 6 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he7)
WHEN 7 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he8)
WHEN 8 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he9)
WHEN 9 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he10)
WHEN 10 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he11)
WHEN 11 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he12)
WHEN 12 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he13)
WHEN 13 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he14)
WHEN 14 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he15)
WHEN 15 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he16)
WHEN 16 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he17)
WHEN 17 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he18)
WHEN 18 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he19)
WHEN 19 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he20)
WHEN 20 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he21)
WHEN 21 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he22)
WHEN 22 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he23)
WHEN 23 THEN (CAST((powerquantity.endtime - powerquantity.begtime) AS float) * 24.0) * MAX(powerquantity.he24)
END
ELSE CASE DATEPART(HOUR, powerquantity.begtime)
WHEN 0 THEN MAX(powerquantity.he1)
WHEN 1 THEN CASE DATEPART(MONTH, powerquantity.begtime)
WHEN 10 THEN MAX(powerquantity.he25)
ELSE MAX(powerquantity.he2)
END
WHEN 2 THEN MAX(powerquantity.he3)
WHEN 3 THEN MAX(powerquantity.he4)
WHEN 4 THEN MAX(powerquantity.he5)
WHEN 5 THEN MAX(powerquantity.he6)
WHEN 6 THEN MAX(powerquantity.he7)
WHEN 7 THEN MAX(powerquantity.he8)
WHEN 8 THEN MAX(powerquantity.he9)
WHEN 9 THEN MAX(powerquantity.he10)
WHEN 10 THEN MAX(powerquantity.he11)
WHEN 11 THEN MAX(powerquantity.he12)
WHEN 12 THEN MAX(powerquantity.he13)
WHEN 13 THEN MAX(powerquantity.he14)
WHEN 14 THEN MAX(powerquantity.he15)
WHEN 15 THEN MAX(powerquantity.he16)
WHEN 16 THEN MAX(powerquantity.he17)
WHEN 17 THEN MAX(powerquantity.he18)
WHEN 18 THEN MAX(powerquantity.he19)
WHEN 19 THEN MAX(powerquantity.he20)
WHEN 20 THEN MAX(powerquantity.he21)
WHEN 21 THEN MAX(powerquantity.he22) * fee.pricediff
WHEN 22 THEN MAX(powerquantity.he23) * fee.pricediff
WHEN 23 THEN MAX(powerquantity.he24) * fee.pricediff
END
END AS MWh,
position.unit,
powerquantity.timeunit,
CASE WHEN fee.feemode = 'FIXED' THEN fee.pricediff ELSE fee.priceindex END AS GBPMWh,
fee.feemode
FROM Trade
INNER JOIN position ON Trade.trade = position.trade
INNER JOIN powerposition ON position.position = powerposition.position
INNER JOIN powerquantity ON powerposition.position = powerquantity.position
AND powerposition.posdetail = powerquantity.posdetail
INNER JOIN contract ON position.contract = contract.contract
AND contract.contracttype IN ('ETSA', 'SETSA')
INNER JOIN fee ON position.position = fee.dbvalue
AND fee.dbcolumn = 'POSITION'
AND fee.feemethod = 'COMMODITY PRICE'
WHERE contract.contract = '110156'
AND powerquantity.posstatus = 1
AND position.loadshape IS NULL
GROUP BY powerquantity.begtime,
powerquantity.endtime,
Trade.trade,
Trade.tradedate,
fee.pricediff,
fee.priceindex,
position.unit,
contract.contract,
fee.feemode,
powerquantity.timeunit,
position.counterparty,
position.positiontype,
Trade.cstcontractstart,
Trade.cstcontractend
ORDER BY powerquantity.begtime,
powerquantity.endtime;