Monday, 15 June 2015

sql - Efficient way to write this query -



sql - Efficient way to write this query -

i trying order records 3 columns , select particular id , record before plus row after that. here query:

;with cte ( select row_number() over(order book, pageint, [idauto]) rownum, [idauto] ccwiseinstr2 ) select * cte rownum = (select rownum cte idauto = 211079) union select * cte rownum = (select rownum - 1 cte idauto = 211079) union select * cte rownum = (select rownum + 1 cte idauto = 211079)

what other efficient way write query. @ moment query takes 336 ms after creating indexes looks bit higher me.

here plan query:

http://gyazo.com/9a7f1c37d4433665d0949acf03c4561c

any help appreciated.

how query:

;with cte ( select row_number() over(order book, pageint, [idauto]) rownum, [idauto] ccwiseinstr2 ) select rownum, idauto cte rownum in ( select rownumber ( select rownum - 1 rownumprev, rownum rownum, rownum + 1 rownumnext cte idauto = 211079 ) vw unpivot ( rownumber idauto in (rownumprev, rownum, rownumnext ) ) unpw )

instead of union utilize unpivot convert columns rows utilize in in. allow me know how goes.

sql sql-server tsql sql-server-2012

No comments:

Post a Comment