Wednesday 15 April 2015

Tuning query in SQL Server -


I am grouping date ranges with missing dates

 ; With DateRange (Select Col1, Col2, Row_Number ()) (Order by No. 1, Col2, FromDate) as RowId, FromDate #SourceTable R, where it is not present (Select Col1, Col2, FromDate #SourceTable from R1 R1.FromDate = Dateadd (Day, -1, R. Pharmadeet) and R1. Call 1 = R. Call 1 and R1. Cole 2 = R. Call 2)) Call 1, Call 2, DR. Choose formatting as the start date, (# 1 source from where DOT & LT; COALESCE ((DRRRID = 1 = CR.RID and DR.Cell1 = CR.COL1), '9999-01 -01 ') and call 1 = DR. Call 1 and Call 2 = By DOT, by date, as the expiration date from date, order a order by call 1, by date   < P> The above questions are working well. I found this query on StackOverFlow but the performance is not good that anyone can tune that question?  

This is my table structure

  DBL BB 2014-05-06 General Rate DBL BB 2014-05- 07 General Rate DBL BB 2014-05-08 Weekly Rate DBL BB 2014-05-09 Weekend Rate DBL BB 2014-05-10 Weekend Rate DBL BB 2014-05-11 Normal Rate DBL BB 2014-05-12 Normal Rate   

I rate the weekend And merging with the normal rate

  DBL BB 2014-05-06 2014-05-07 General Rate DBL BB 2014-05-08 2014   Div> 

Instead of using embedded questions in the embedded queries ...

You can duplicate the logic you use, your FromDate and it will be sent to your < Change to get code> end date .

This is easy on the adapter (consult the execution plan to see the difference).

 ; With loweradrate range (select call 1, call2, row_number () over (call by call 1, call 2, type of data) type of type, type of roid, off-date, test type where not present (selection Do call 1, call 2, fluorescence from Test R1 where R1Formatate = DateEd (day, -1, R.Formaid) and R1col 1 = R. call 1 and R1. Cole 2 = R. Call 2 and R1 type = R type)), as the upper date range (select call 1, call2, row_number () rowing In the form of the last date, the type of test is not where the type is present, (call 1, call 2, sort by type, type 2, call 2, order test from R1 where r1. Formate = dateaid ( Day, +1, r fromDate) and R1.Col1 = R.Col1 and R1.Col2 = R.Col2 and R1.Type = R.Types)) l.col1, l.col2, l.ROWID, l.FromDate , Choose u.EndDate, l The type of LowerDateRange L includes L.ROWID = u.rowID at UpperDateRange U.   

See it in action:

No comments:

Post a Comment