Sie sind auf Seite 1von 2

I've seen the following technique in several beginner code samples for demonstrating SQL Server 2005's ability

to return paged results I've added the !otal"ows # $ount%&' ()*"%' line to demonstrate how return the total rows returned above and beyond the row count for the paged set !his removes the need for a second query to get the total rows available for paging techniques in your application In your application+ ,ust checto ma-e sure your resultset has records+ then ,ust grab the first record and retrieve its !otal"ows column value .otice that in this query+ the /(I. between the (rders table and the 0sers table is being run across all records that are found .(! ,ust the records returned in the paged set declare 1Start"ow int declare 12a3"ows int select 1Start"ow # 4 select 12a3"ows # 40 select & from %select o &+u 5irst.ame+u Last.ame+ !otal"ows#$ount%&' ()*"%'+ "(67.028*"%' ()*"%("9*" 8: o $reate9ate!ime desc' as "ow.um from (rders o + 0sers u 6;*"* o $reate9ate!ime < getdate%' =>0 ?.9 %o 0serI9 # u 0serI9' ' 6;*"* "ow.um 8*!6**. 1Start"ow ?.9 %1Start"ow @ 12a3"ows' =4 If you ad,ust your query as follows+ you will see a substantial boost in performance .otice this query only performs the ,oin on the returned resultset which is much+ much smaller S*L*$! 2y!able &+u 5irst.ame+u Last.ame 5"(2 %S*L*$! o &+ !otal"ows#$ount%&' ()*"%'+ "(67.028*"%' ()*"%("9*" 8: o $reate9ate!ime desc' as "ow.um 5"(2 (rders o 6;*"* o $reate9ate!ime < getdate%' =>0 ' as 2y!able+ 0sers u 6;*"* "ow.um 8*!6**. 1Start"ow ?.9 %1Start"ow @ 12a3"ows' =4 and %2y!able 0serI9 # u 0serI9' ;aving recently reviewed ways to speed up the paging grids at *gg;ead$afe %they were never slow but I'm always loo-ing for ways to optimiAe things'+ I decided to put SQL Server's new paging mechanism to a test between itself and using standard queries in con,unction with !?8L* variables ?cross 5 different very large tables at *gg;ead$afe+ the !?8L* variable option performed twice as fast as the suggested paging mechanism above 6hy I didn't of thin- of why much sooner is beyond me 6hen you loo- at the inner query in the paging sample above+ you notice that it is pulling bac- the entire (rders record for every single record in the (rders table !hen+ the outer query queries the inner table's results !he !?8L* variable sample stored procedure synta3 doesn't perform the same /(I.S as above It is included here ,ust to give you an idea of how you might write your own (f course+ depending on how often your data changes+ you could even implement a cache of the primary -eys to speed this up even more I saw the biggest performance gains when querying for pages deep in the result set In our case+ that was tens of thousands of pages deep $"*?!* B"($*90"* CdboD CEet"ecordsBagedD % 1Start"ow int+ 12a3"ows int ' ?S declare 1!otal"ows bigint declare 1Bager table % "ow.umber int I9*.!I!: %4+ 4' Brimary -ey .(! .0LL + "ecordI9 bigint+ Brimary Fey $lustered%"ow.umber' ' == .otice that this I.S*"! I.!( query can get 400G of its results from the clustered primary -ey inde3 I.S*"! I.!( 1Bager %"ecordI9' S*L*$! "ecordI9 5"(2 dbo "ecord ("9*" 8: "ecordI9 desc == :ou can ("9*" 8: datetime columns if your primary -ey is not a number oriented column S*L*$! top 4 1!otal"ows # $(0.!%&' from 1Bager == 9id this because it is a little faster than !otal"ows#$ount%&' ()*"%' == :ou would append your /(I.S etc to the result query below S*L*$! "ecord &+ 1!otal"ows as !otal"ows 5"(2 dbo "ecord 6;*"* "ecordI9 in %S*L*$! "ecordI9

5"(2 1Bager i 6;*"* i "ow.umber 8*!6**. 1Start"ow ?.9 %1Start"ow @ 12a3"ows' = 4' ("9*" 8: "ecordI9 desc

Das könnte Ihnen auch gefallen