Optimize SQL Server Aggregation Query -
i'm looking ideas on how optimize query. i've evaluated execution plan not offer ideas missing index curious if writing query better (different tactics) result in faster/lighter query.
select [place], count([place]) ( select scoresid, replace(replace(eventplace1,'t', ''),'*','') [place] [ms.prod]..mso_scores union select scoresid, replace(replace(eventplace2,'t', ''),'*','') [mso.prod]..mso_scores union select scoresid, replace(replace(eventplace3,'t', ''),'*','') [mso.prod]..mso_scores union select scoresid, replace(replace(eventplace4,'t', ''),'*','') [mso.prod]..mso_scores union select scoresid, replace(replace(eventplace5,'t', ''),'*','') [mso.prod]..mso_scores union select scoresid, replace(replace(eventplace6,'t', ''),'*','') [mso.prod]..mso_scores union select scoresid, replace(replace(aaplace,'t', ''),'*','') [mso.prod]..mso_scores ) data1 join [mso.prod]..mso_scores scores on scores.scoresid = data1.scoresid , scores.usagnum = '274246' , scores.teamresult='n' data1.place in ('1', '2', '3') group place
so quick explanation: there 6 event place fields. data in these fields looks "1", "2", "1t", "3", "5t"; "t" tie. care number, 1,2,3 parsing out "t" or "*" place , grouping query count.
how many 1st places have, how many 2nd places, , on..
try 1 (for 2008 , higher) -
select [place], count(1) ( select [place] = replace(replace(t.[place], 't', ''), '*', '') dbo.mso_scores r outer apply ( values (eventplace1), (eventplace2), (eventplace3), (eventplace4), (eventplace5), (eventplace6), (aaplace) ) t([place]) r.usagnum = '274246' , r.teamresult = 'n' ) d d.place in ('1', '2', '3') group d.place
for additional information read topic: tips sql query optimization analyzing query plan
Comments
Post a Comment