473,378 Members | 1,478 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,378 software developers and data experts.

Where can I find a design pattern for paging through a list?

Hi,

How do websites like google page through large numbers of records?

For example; if I do a search on Google, get 187,000 matches, and go to the
22nd page of the results; I'm sure Google doesn't do a new SELECT statement
and traverse through 440 records (22pgs x 20 per page). But I'm also pretty
sure Google doesn't keep a hell of a lot of session data open either.

I know there are all kinds of ways to do this, I've created hacks which did
the job in the past. But there has to be a 'best practice' way of handling
this.

Can anybody point me in the right direction? I'm open to technology
specific ways, but would prefer generic.

Thanks,
John
Nov 18 '05 #1
7 1370
In the ADO days, you could do this with built-in objects. A small recordset
that returns (in googles case) probably 2 or 3 columns, but that is 200,000
deep - it's not really that significant. The hardest part is rendering and
formatting it.

So - I believe they may just do that. Any Design Pattern would need to be
more low level than what we can do. In other words, if this functionality
isn't built into the objects we're using then chances are, our attempts at
trying to be more efficient will likely not be good.
All that aside, if you had some huge resultset and needed to break it up -
you could always do it inside the stored procedure.. something like:

declare @pagenum as integer
declare @count as integer

set @pagenum = 1

select @count=count(*) from myviewortable where mywhereclause=1

select top 25 field1, field2, @count from myviewortable where
mywhereclause=1 and
field1 not in (select top (25 * @pagenum) field1 from myviewortable
where mywhereclause=1)
(this is completely off the top of my head) so basically, you can get a
rowcount (which is fast), then pull back 25 at a time.. when you get to page
3 for example, the exclude list will be "select top 75" - so the main select
will bring back from 76 to 100 - and it will also bring back the total
number of rows (@count), so you can create the facade on the front end of
"pages".. hopefully that's not confusing, I'm not explaining it well.. but
do you get the point?
"John" <Pl****@reply.to.group.com> wrote in message
news:SD********************@news20.bellglobal.com. ..
Hi,

How do websites like google page through large numbers of records?

For example; if I do a search on Google, get 187,000 matches, and go to the 22nd page of the results; I'm sure Google doesn't do a new SELECT statement and traverse through 440 records (22pgs x 20 per page). But I'm also pretty sure Google doesn't keep a hell of a lot of session data open either.

I know there are all kinds of ways to do this, I've created hacks which did the job in the past. But there has to be a 'best practice' way of handling this.

Can anybody point me in the right direction? I'm open to technology
specific ways, but would prefer generic.

Thanks,
John

Nov 18 '05 #2
Drebin wrote:
In the ADO days, you could do this with built-in objects. A small
recordset that returns (in googles case) probably 2 or 3 columns, but
that is 200,000 deep - it's not really that significant. The hardest
part is rendering and formatting it.

So - I believe they may just do that. Any Design Pattern would need
to be more low level than what we can do. In other words, if this
functionality isn't built into the objects we're using then chances
are, our attempts at trying to be more efficient will likely not be
good.
All that aside, if you had some huge resultset and needed to break it
up - you could always do it inside the stored procedure.. something
like:

declare @pagenum as integer
declare @count as integer

set @pagenum = 1

select @count=count(*) from myviewortable where mywhereclause=1

select top 25 field1, field2, @count from myviewortable where
mywhereclause=1 and
field1 not in (select top (25 * @pagenum) field1 from myviewortable
where mywhereclause=1)
(this is completely off the top of my head) so basically, you can get
a rowcount (which is fast), then pull back 25 at a time.. when you
get to page 3 for example, the exclude list will be "select top 75" -
so the main select will bring back from 76 to 100 - and it will also
bring back the total number of rows (@count), so you can create the
facade on the front end of "pages".. hopefully that's not confusing,
I'm not explaining it well.. but do you get the point?
"John" <Pl****@reply.to.group.com> wrote in message
news:SD********************@news20.bellglobal.com. ..

I don't think you can specify an expression for "top" (at least, I have
never gotten that to work :-) )

What we do here:
- create a temp table
- use a cursor with the full where-clause
- fetch absolute to the first record in the page
- add that row to the temp table
- fetch next, repeat until one page is copied
- select all from the temp table
(you can store the entire row in the temp table, or just the key-value
and join original and temp tables)

Hans Kesting
Nov 18 '05 #3
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
I don't think you can specify an expression for "top" (at least, I have
never gotten that to work :-) )


I just tried this, this works:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 30 account_nbr from AccountData where
net_worth > 0)

and if I increment:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 40 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 50 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 60 account_nbr from AccountData where
net_worth > 0)

These all work - and as expected!!
Nov 18 '05 #4
Hi Drebin,

I never even considered a NOT IN subquery, but that definitely looks like
the way to go.

Thanks,
John

PS-I think Hans is probably using a non-SQLServer database, hence the mix up
with the 'TOP' expression.
"Drebin" <th*******@hotmail.com> wrote in message
news:1i*****************@newssvr31.news.prodigy.co m...
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
I don't think you can specify an expression for "top" (at least, I have
never gotten that to work :-) )


I just tried this, this works:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 30 account_nbr from AccountData where
net_worth > 0)

and if I increment:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 40 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 50 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 60 account_nbr from AccountData where
net_worth > 0)

These all work - and as expected!!

Nov 18 '05 #5
Understood - but let me add, this is more likely a last resort type
scenario, depending on the size and data you are bringing back, returning a
high number of rows like that, but only displaying a handful at a time, is
*relatively* scalable.. your mileage may vary.

Point is, you may want to spend some time on metering your network and
CPU/memory to see when it will start being a problem and act on it
accordingly. It may not even become an issue if your app is not too big.
"John" <Pl****@reply.to.group.com> wrote in message
news:O_*********************@news20.bellglobal.com ...
Hi Drebin,

I never even considered a NOT IN subquery, but that definitely looks like
the way to go.

Thanks,
John

PS-I think Hans is probably using a non-SQLServer database, hence the mix up with the 'TOP' expression.
"Drebin" <th*******@hotmail.com> wrote in message
news:1i*****************@newssvr31.news.prodigy.co m...
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
I don't think you can specify an expression for "top" (at least, I have
never gotten that to work :-) )


I just tried this, this works:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 30 account_nbr from AccountData where
net_worth > 0)

and if I increment:

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 40 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 50 account_nbr from AccountData where
net_worth > 0)

select top 10 * from AccountData where net_worth > 0
and account_nbr not in (select top 60 account_nbr from AccountData where
net_worth > 0)

These all work - and as expected!!


Nov 18 '05 #6
John wrote:
Hi Drebin,

I never even considered a NOT IN subquery, but that definitely looks
like the way to go.

Thanks,
John

PS-I think Hans is probably using a non-SQLServer database, hence the
mix up with the 'TOP' expression.


No, it is sqlserver. What I meant was: in a *stored procedure* I can use
"TOP 10" (or whatever), but I can't use "TOP @amount" or
"TOP (10*@pagenumber)". A fixed number works, an expression is
not accepted.
I realize (now) that you can also build a query-string where you can use
any expression to come up with a "TOP n" phrase, before you send it to
the database engine. It's just that we *always* use stores procedures,
never strings of sql code.

Hans Kesting
Nov 18 '05 #7
Ahh.. got it. I didn't catch that. I wonder if you couldn't create a dynamic
SQL statement that fills a temp table, then do your "not in" from there..
I'll mess around with this more...

Good one!

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:uE**************@TK2MSFTNGP14.phx.gbl...
John wrote:
Hi Drebin,

I never even considered a NOT IN subquery, but that definitely looks
like the way to go.

Thanks,
John

PS-I think Hans is probably using a non-SQLServer database, hence the
mix up with the 'TOP' expression.


No, it is sqlserver. What I meant was: in a *stored procedure* I can use
"TOP 10" (or whatever), but I can't use "TOP @amount" or
"TOP (10*@pagenumber)". A fixed number works, an expression is
not accepted.
I realize (now) that you can also build a query-string where you can use
any expression to come up with a "TOP n" phrase, before you send it to
the database engine. It's just that we *always* use stores procedures,
never strings of sql code.

Hans Kesting

Nov 18 '05 #8

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

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
3
by: Omer van Kloeten | last post by:
The Top Level Design: The class Base is a factory class with a twist. It uses the Assembly/Type classes to extract all types that inherit from it and add them to the list of types that inherit...
12
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
2
by: farhad13841384 | last post by:
Hi , I Hope You fine. I have some problem with this code for paging in asp.net this bottom code work correctly without any error but when I try to place separate code in .VB file then error is...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
3
by: Froefel | last post by:
I'm trying to modem a relationship with classes and I'm having trouble finding the correct design pattern. Maybe someone with more experience knows which pattern(s) I'm looking for. Here's an...
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.