mysql - What is the effect of performance of a SQL query when have aggregate function and where clause -
i have 2 queries :
1.
select u.unitid unitid, u.unitscode 'unitscode', ( cast(count(vd.date) float) / u.timediff ) * 100 'bookingcount', u.tradestarttime, u.tradestoptime, u.minimumslot, u.maximumterm @alldates vd inner join commcmlbookingdetail cd on vd.date between cd.dtfromtime , cd.dttotime , datepart(minute, vd.date) = datepart(minute, cd.dtfromtime) inner join commcmlbooking cb on cb.hmy = cd.hbooking , cb.istatus = 1 , cb.itype = 525 inner join @unitsinfo u on u.unitid = cb.hunit , cast(vd.date date) between cast(@bookingfromdate date) , cast(@bookingtodate date) , cast(vd.date time) between cast(u.tradestarttime time) , cast(u.tradestoptime time) cb.hrecord = case when @amendmentid = 0 cb.hrecord else @amendmentid end group u.unitid, u.unitscode, u.minimumslot, u.tradestarttime, u.timediff, u.tradestoptime, u.maximumterm;
2.
insert @tempbookingcount select u.unitid, u.timediff @alldates vd inner join commcmlbookingdetail cd on vd.date between cd.dtfromtime , cd.dttotime , datepart(minute, vd.date) = datepart(minute, cd.dtfromtime) inner join commcmlbooking cb on cb.hmy = cd.hbooking , cb.istatus = 1 , cb.itype = 525 inner join @unitsinfo u on u.unitid = cb.hunit , cast(vd.date date) between cast(@bookingfromdate date) , cast(@bookingtodate date) , cast(vd.date time) between cast(u.tradestarttime time) , cast(u.tradestoptime time) cb.hrecord = case when @amendmentid = 0 cb.hrecord else @amendmentid end insert @unitbookingcount select tt.unitid, u.unitscode, ( cast(count(tt.unitid) float) / tt.timediff ) * 100, u.tradestarttime, u.tradestoptime, u.minimumslot, u.maximumterm @tempbookingcount tt inner join @unitsinfo u on u.unitid = tt.unitid group tt.unitid, tt.timediff, u.tradestarttime, u.tradestoptime, u.minimumslot, u.maximumterm, u.unitscode
i have separated first query 2 parts, , can huge difference in performance!
first query take 14 seconds when executed 5 months next query take 4 seconds.
your original query uses 2 table variables @alldates , @unitsinfo.
by using table variables, aren't giving sql fighting chance of optimizing query because there no statistics on table variables , row count estimations , query plan impacted.
one reference, can find many more: http://blogs.msdn.com/b/psssql/archive/2010/08/24/query-performance-and-table-variables.aspx
try original #temptables instead of @tablevars
Comments
Post a Comment