Got more questions? Find advice on: SQL | XML | Regular Expressions | Windows
in Search
Welcome to AspAdvice Sign in | Join | Help

Justin Lovell's Blog

Mood: The Grumpy Coder

Pagination Removed from Whidbey

One of things featured in the Alpha (Preview Release) is Pagination. How pagination works is the use of the ExecutePageReader method from the DbCommand class. In the Beta One release, the ExecutePageReader method is now obsolute. With the pagination, you can query what section of the data can be read from database. For example, you can say that you want records 20 to 40 from any given table.

However, it used SQL Cursors. This method is SLOW when used on huge tables. So Microsoft decided to leave it and move on. However, there is a technique that has been in my “books” for a long time is the following SQL Query:

Select * From
   (Select * Top RowCount From (
         Select Top {StartRow + RowCount}
             ColumnsToSelect FROM TableNameOrFurtherSelect
                    [NormalStuff]
             Order By NormalOrder
   ) Order By OppositeOrder)
Order By NormalOrder

There are a view variables that I italiced and bolded. I will discuss the normal stuff below:

  • RowCount: Is the amount of rows to be returned from the database table.
  • StartRow: The row index to start the selection from. This is used in the sum with RowCount in place of the {StartRow + RowCount} -- treat it as one constant; not an equation.
  • ColumnsToSelect: Your normal columns to select from the table.
  • TableNameOrFurtherSelect: This is basically one of the two:
    • The table to select from.
    • The query to run on other tables. This query must be encollapsed by brackets.
  • NormalStuff: Things like WHERE clauses and such all goes here.
  • NormalOrder: Well, this the trick to magic. Basically, you would know more or less the order of the data that is going to be played to you. For example, if you don't really do any sorting, you know that the order of the records/rows will be recieved in Primary Key-Identity Order.
  • OppositeOrder: The complete reverse order of the NormalOrder. For example, if you NormalOrder was “Column1 ASC, Column2 Desc” then your OppositeOrder will be “Column1 Desc, Column2 Asc” and so forth.

Hope you guys and girls learnt something. If any of you have other ideas or tricks that you want to share for pagination, fire them my way. The only thing that I will be “over the moon” is for faster ways to do the selection process on the SQL Server.

Published Saturday, August 28, 2004 9:34 PM by jlovell

Comments

 

jlovell said:

I thought you needed constants for the Top keyword.

Unless of course you're dynamically creating the sql in code. Either way, I might have a faster way of execution than that when it comes to extreme tables. :) Data-mining experience makes for a killer sql situation where a single f-up can cost you minutes.
August 29, 2004 12:42 AM
 

jlovell said:

Also, the sorting can cause some delays in your execution plan. I'd go with a proper sorting right off the bat, and do it once or twice. I think there's an extra sort that's going on that's not necessary. :)

I'm SQL GOD!!!! next to Tmorton that is.
August 29, 2004 12:44 AM
 

jlovell said:

Hmmm... better? But you won't tell me ;-). I think that I have a better way... let me just go and test it first :-).

And yes, the TOP declaration is constant. I was just giving a "template" almost.
August 29, 2004 9:38 AM
 

jlovell said:

> I'm SQL GOD!!!! next to Tmorton that is.

Eh, you can have the title, Kay. It's overrated. :-)
September 4, 2004 10:08 PM
 

jlovell said:

Can I have a signature next to that post Terri? I'd like to put that on my business cards. :) *kidding*

LET'S SEE SOME BENCHMARKS!!!!

oh yeah.. you were asking me for those. doh! Get me an sql script to create the tables and procedure, and I'll benchmark for you.

Hasta.
September 5, 2004 2:26 PM
 

TrackBack said:

September 15, 2004 12:09 PM
Anonymous comments are disabled