473,382 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Sorting and Paging in the Database

Hi,

I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB
side paging. However, currently I perform my sorting on the front end.

If I perform a sort and then ask for page two, I will retrieve the wrong
items for tha page as the sort item would be wrong.

Is it possible to do something like this?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

TIA

MattC
May 15 '07 #1
10 1616
Paging in the database is frequently a bad idea simply because you introduce
load that isn't necessary and may cause bottlenecks with load. What about
the paging isn't working? You need to set the correct page index, have you
done so?

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
"MattC" <m@m.comwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
Hi,

I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB
side paging. However, currently I perform my sorting on the front end.

If I perform a sort and then ask for page two, I will retrieve the wrong
items for tha page as the sort item would be wrong.

Is it possible to do something like this?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

TIA

MattC

May 16 '07 #2
Ok for 100 rows I would do it all in my middle tier. For 10,000 then I
don't want that coming down the line and having 9,950 rows unseen,

So for my mediocre sized table I employ PagedDataSource stuff. What I was
looking for was a generic way of using some paging code for my 'larger'
tables.

Although if I'm only doing this a few times the code maintenance wont be too
bad on writing each one individually.

Thanks

MattC
"Alvin Bruney [MVP]" <some guy without an email addresswrote in message
news:O%****************@TK2MSFTNGP03.phx.gbl...
Paging in the database is frequently a bad idea simply because you
introduce load that isn't necessary and may cause bottlenecks with load.
What about the paging isn't working? You need to set the correct page
index, have you done so?

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
"MattC" <m@m.comwrote in message
news:eI**************@TK2MSFTNGP02.phx.gbl...
>Hi,

I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB
side paging. However, currently I perform my sorting on the front end.

If I perform a sort and then ask for page two, I will retrieve the wrong
items for tha page as the sort item would be wrong.

Is it possible to do something like this?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

TIA

MattC


May 16 '07 #3
On May 16, 3:20 am, "Alvin Bruney [MVP]" <some guy without an email
addresswrote:
Paging in the database is frequently a bad idea simply because you introduce
load that isn't necessary and may cause bottlenecks with load. What about
the paging isn't working? You need to set the correct page index, have you
done so?
Alvin, are you serious about sql paging?

May 16 '07 #4
On May 15, 6:58 pm, "MattC" <m...@m.comwrote:
Hi,

I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB
side paging. However, currently I perform my sorting on the front end.

If I perform a sort and then ask for page two, I will retrieve the wrong
items for tha page as the sort item would be wrong.

Is it possible to do something like this?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

TIA

MattC
Matt, I think the statement is correct and working well on my server
(also for a second page).

How do you calculate the @Start value, maybe this is the reason of
your problem?

May 16 '07 #5
Well I wanted to write a TVF that would allow me to pass in a table and the
start and end points.

Select x,y,z FROM into #mytable
bigtable where ID = @SomeID
SortBy @MyColumn

then do

SELECT * FROM MyPagingFunction(#mytable , @PageNumber, @PageSize)

Now my first issue was I can't do dynamic variable based sorting ok so maybe
I'll use dynamic sql or case statements. But I really wanted to wrap the
paging code into a function.

Can CLR written TVF's accept tables?

This topic probably should now be on a SQL Server 2005 group I think though
now.

MattC
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
On May 15, 6:58 pm, "MattC" <m...@m.comwrote:
>Hi,

I wanted to take advantage of the Row_Number feature of SQL 2005 to to DB
side paging. However, currently I perform my sorting on the front end.

If I perform a sort and then ask for page two, I will retrieve the wrong
items for tha page as the sort item would be wrong.

Is it possible to do something like this?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

TIA

MattC

Matt, I think the statement is correct and working well on my server
(also for a second page).

How do you calculate the @Start value, maybe this is the reason of
your problem?

May 16 '07 #6
On May 16, 3:41 pm, "MattC" <m...@m.comwrote:
Well I wanted to write a TVF that would allow me to pass in a table and the
start and end points.

Select x,y,z FROM into #mytable
bigtable where ID = @SomeID
SortBy @MyColumn

then do

SELECT * FROM MyPagingFunction(#mytable , @PageNumber, @PageSize)

Now my first issue was I can't do dynamic variable based sorting ok so maybe
I'll use dynamic sql or case statements. But I really wanted to wrap the
paging code into a function.
But here the dynamic variable based sorting is working (@OrderField),
isn't it?

With Cust AS
( SELECT CustomerID, CompanyName,
ROW_NUMBER() OVER (order by @OrderField) as RowNumber
FROM Customers )
select *
from Cust
Where RowNumber Between @Start and @End

Or, I don't get it...

>
Can CLR written TVF's accept tables?
Well, I've never try it, what about a stored procedure?

Alexey

May 16 '07 #7
I see where my answer could cause confusion. I'm referring to page index at
the front end level NOT sql paging.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On May 16, 3:20 am, "Alvin Bruney [MVP]" <some guy without an email
addresswrote:
>Paging in the database is frequently a bad idea simply because you
introduce
load that isn't necessary and may cause bottlenecks with load. What about
the paging isn't working? You need to set the correct page index, have
you
done so?

Alvin, are you serious about sql paging?

May 17 '07 #8
On May 17, 1:58 pm, "Alvin Bruney [MVP]" <some guy without an email
addresswrote:
I see where my answer could cause confusion. I'm referring to page index at
the front end level NOT sql paging.
Please forgive me as I've misunderstood you.

May 17 '07 #9
What's your opinion on his architecture? Mine? sure it can work and it is
probably the easiest solution. However, with large data sets as s/he
implies, I can see a DBA getting really angry about paging logic inside SQL
server.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
https://www.microsoft.com/MSPress/books/10933.aspx
OWC Black Book www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@q75g2000hsh.googlegro ups.com...
On May 17, 1:58 pm, "Alvin Bruney [MVP]" <some guy without an email
addresswrote:
>I see where my answer could cause confusion. I'm referring to page index
at
the front end level NOT sql paging.

Please forgive me as I've misunderstood you.

May 18 '07 #10
On May 18, 9:29 pm, "Alvin Bruney [MVP]" <some guy without an email
addresswrote:
What's your opinion on his architecture? Mine? sure it can work and it is
probably the easiest solution. However, with large data sets as s/he
implies, I can see a DBA getting really angry about paging logic inside SQL
server.
It's a question of what is more important in this particular
application (performance vs. design)

Actually, this is what Microsoft recommended as "an improvement
of .NET application performance and scalability"

http://msdn2.microsoft.com/en-us/library/ms979197.aspx

(Note, this whitepaper is for SQL 2000 and .NET 1.1)

Although there are significant improvements in .NET 2 (Gridview and
ObjectDataSource), a quick and efficient database paging is still a
problem.

May 18 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Henry | last post by:
I have a large number of user define class objects and want display in a datagrid and able to perform paging and column sorting. It seems using DataView is the easiest way but the objects are not...
0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
2
by: Hajime Kusakabe | last post by:
Hi. I have created a datagrid (datagrid1) without any columns on a aspx page. Then aspx.vb adds columns from a database. It is somthing like this .... ================================== Dim...
1
by: Guoqi Zheng | last post by:
Sir, The default paging of datagrid is somehow use too much resource, so I am using Stored procedure for the paging. You can find my Stored procedure at the end of this message. It works...
8
by: Matthew Curiale | last post by:
I am creating an app that lists clients of a company for management of different attributes for that company. The first page is a listing of the companies currently in the database. I have my...
0
by: Daniel Bass | last post by:
I've already posted this in the asp forum, and other asp.net forums on the net with no luck... I've got a data grid, with paging, and sorting allowed. I can find loads of examples, and have...
5
by: yefei | last post by:
In my web design, I display records from a SQL DataBase according to some filters the GridView is defined with select commands and selest parameters however, I also want to display all records...
1
by: puja | last post by:
hi all, am using grid view in asp.net 2.0 . My problem is easy but can't get it to work. My grid view id = grdContract and am binding grid view using dataset My dataset returns 5 columns from...
1
by: John A Grandy | last post by:
In regard to a GridView that must support searching, filtering, sorting, and paging ... There is a tradeoff in performing the sorting and paging in the database versus to creating a CLR sort...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.