我有以下查詢在哪裏取決於站點名稱(afield1)值不同的列用於獲取PatrolNo信息。查詢將從星期日開始從當前視圖獲取當前星期的數據,然後爲每個站點構建計數。查詢需要很長時間才能完成
但返回值需要很長時間。
select cast(temp.timetag1 as date) as Date, temp.PatrolNo,temp.ScansDue, COUNT(*) as Total,temp.aField1
from (select timetag1, afield1 ,
case when afield1 = 'site1' then afield17
when afield1 = 'site2' then afield16
when afield1 = 'site3' then afield18
when afield1 = 'site4' then afield19
when afield1 = 'site5' then afield20
when afield1 = 'site6' then afield21
when afield1 = 'site7' then afield22 end as PatrolNo,
case when afield1 = 'site1' then 17
when afield1 = 'site2' then 8
when afield1 = 'site3' then 9
when afield1 = 'site4' then 32
when afield1 = 'site5' then 15
when afield1 = 'site6' then 15
when afield1 = 'site7' then 52 end as ScansDue
FROM someview
where timetag1 >= (dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)) and timetag1 < GETDATE()
and aField1 like 'site2'
and (afield3 like '%SER%' OR afield10 like '%SER%' OR afield11 like '%SER%' OR afield12 like '%SER%' OR afield13 like '%SER%'
OR afield14 like '%SER%' OR afield15 like '%SER%')
) temp
group by cast(temp.timetag1 as date),aField1, PatrolNo,ScansDue;
date PatrolNo ScansDue Total aField1
2014-07-21 1 32 63 site1
2014-07-21 2 32 63 site2
2014-07-21 3 32 32 site1
2014-07-21 4 32 31 site7
2014-07-21 5 32 30 site7
您是否檢查過通常的嫌疑犯,即您是否在用於過濾和分組的列上有索引? –
您還應該考慮使該表正常化。那裏有多少個球場?你如何處理添加一個新的?這應該是第二個表格而不是整列列表。 –
你確定你一開始需要通配符嗎?如果不是索引就成爲可能。 – Mihai