Ridiculously huge impact of comparison operator on query execution duration
I have the following views defined:
dsplit_base - a union of 4 queries each of which is a simple join between
fact and mapping tables (contains call statistics); it consists of 201
columns
calls_check - a view derived from the dsplit_base meant to be used in data
consistency check. Here is the definition:
select a.Brand, a.[Call Center] ,c.date, c.weekday, COUNT(*) vol,
cast((COUNT(*)-g.vol) as real)/g.vol*100 vol_diff
, SUM(abncalls+acdcalls) calls
, case when g.calls<>0 then cast((SUM(abncalls+acdcalls)-g.calls) as
real)/g.calls*100 else case when SUM(abncalls+acdcalls)<>0 then 100
else 0 end end calls_diff
from dsplit_base a join calendar c on a.ROW_DATE=c.date
join (
select t.Brand, t.[Call Center], c.weekday,avg(cast(vol as
bigint)) vol, AVG(cast(calls as bigint)) calls
from (
select Brand, [Call Center], row_date, COUNT(*) vol,
SUM(abncalls+acdcalls) calls from dsplit_base group by
ROW_DATE, [Call Center], Brand
) t
join calendar c on t.row_date=c.date
group by c.weekday, t.[Call Center], t.Brand
) g
ON c.weekday=g.weekday and a.Brand=g.Brand and a.[Call Center]=g.[Call
Center]
group by c.date, c.weekday, g.vol, g.calls, a.[Call Center], a.Brand
The following query yields around 16000 rows in 1-3 seconds:
select * from calls_check
Brand Call Center date weekday vol vol_diff calls calls_diff
LMN Munich 2008-01-24 Thursday 3 -25 470 8.796296
LMN Munich 2008-04-26 Saturday 3 0 352 51.72414
...
Now the actual problem I encountered is when I tried to pull out results
for limited period of time. By adding where clause as follows the query
will not finish (surely not in ~10 minutes):
select * from calls_check
where date >= DATEADD(d, -8, sysdatetime())
And, what is maybe even weirder, this query executes successfully in a
second!
select * from calls_check
where date < DATEADD(d, -8, sysdatetime())
Can anybody tell why comparison operator in where clause makes such a
difference? Why < seems to very efficiently slice the result set while >
or = makes the query unresponsive?
No comments:
Post a Comment