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

Pagination - 1 or 2 queries?

CSN
Since you usually need to know the total number of
rows a query would return, do you think it's better
to:

a) Do one query with a LIMIT and OFFSET to get the
results, and another COUNT query to get the total
number of rows?

b) Do a single query without a LIMIT and OFFSET, then
do a seek or similiar to get at the rows you want?

Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #1
7 3330
On Fri, 5 Sep 2003, CSN wrote:
Since you usually need to know the total number of
rows a query would return, do you think it's better
to:

a) Do one query with a LIMIT and OFFSET to get the
results, and another COUNT query to get the total
number of rows?

b) Do a single query without a LIMIT and OFFSET, then
do a seek or similiar to get at the rows you want?

Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".


Either way works. Does the eclipse library use a cursor, or grab the
whole dataset and then seek on the client side? If it uses a cursor, I'd
expect it to be the fastest and simplest implementation. Since a lot of
libs are designed to work with MySQL, they often are written in the first
method, where select count(*) is quite quick on MySQL, and MySQL doesn't
have cursor support.

With Postgresql, the cursor is likely to be the faster method.
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 11 '05 #2
CSN

--- "scott.marlowe" <sc***********@ihs.com> wrote:
On Fri, 5 Sep 2003, CSN wrote:
Since you usually need to know the total number of
rows a query would return, do you think it's

better
to:

a) Do one query with a LIMIT and OFFSET to get the
results, and another COUNT query to get the total
number of rows?

b) Do a single query without a LIMIT and OFFSET,

then
do a seek or similiar to get at the rows you want?


Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".


Either way works. Does the eclipse library use a
cursor, or grab the
whole dataset and then seek on the client side? If
it uses a cursor, I'd
expect it to be the fastest and simplest
implementation. Since a lot of
libs are designed to work with MySQL, they often are
written in the first
method, where select count(*) is quite quick on
MySQL, and MySQL doesn't
have cursor support.

With Postgresql, the cursor is likely to be the
faster method.


Eclipse appears to just use pg_fetch_array($result,
$index). That'd be pretty similiar to a cursor
wouldn't it? i.e. only the specified rows would be
sent to the client (but all rows would be in the
server's memory).

Eclipse's docs make the argument that "b" is better
because "a" still needs to select/examine all rows
before doing the LIMIT and OFFSET.

http://www.students.cs.uu.nl/people/...api/index.html
(PagedQuery)

CSN

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #3
On Fri, 5 Sep 2003, CSN wrote:

--- "scott.marlowe" <sc***********@ihs.com> wrote:
On Fri, 5 Sep 2003, CSN wrote:
Since you usually need to know the total number of
rows a query would return, do you think it's

better
to:

a) Do one query with a LIMIT and OFFSET to get the
results, and another COUNT query to get the total
number of rows?

b) Do a single query without a LIMIT and OFFSET,

then
do a seek or similiar to get at the rows you want?


Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".


Either way works. Does the eclipse library use a
cursor, or grab the
whole dataset and then seek on the client side? If
it uses a cursor, I'd
expect it to be the fastest and simplest
implementation. Since a lot of
libs are designed to work with MySQL, they often are
written in the first
method, where select count(*) is quite quick on
MySQL, and MySQL doesn't
have cursor support.

With Postgresql, the cursor is likely to be the
faster method.


Eclipse appears to just use pg_fetch_array($result,
$index). That'd be pretty similiar to a cursor
wouldn't it? i.e. only the specified rows would be
sent to the client (but all rows would be in the
server's memory).

Eclipse's docs make the argument that "b" is better
because "a" still needs to select/examine all rows
before doing the LIMIT and OFFSET.


If they aren't explicitly declaring a cursor, then b isn't exactly the
same. If you do:

select * from table order by fieldname

then

$row = pg_fetch_array()

then the whole data set is returned to the client (i.e. php) before we can
get the row. Now, if they do:

begin;
declare bubba as cursor for select * from table order by fieldname;
move forward 100 in bubba;
fetch 5 from bubba;
rollback;

Then you get the same kind of effect, but only 5 rows have to be retrieved
from the database to the client, and pg_fetch_array will now iterate over
those 5 rows only, and then run dry, so to speak.
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 11 '05 #4
scott.marlowe wrote:
On Fri, 5 Sep 2003, CSN wrote:
Since you usually need to know the total number of
rows a query would return, do you think it's better
to:

a) Do one query with a LIMIT and OFFSET to get the
results, and another COUNT query to get the total
number of rows?

b) Do a single query without a LIMIT and OFFSET, then
do a seek or similiar to get at the rows you want?

Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".


Either way works. Does the eclipse library use a cursor, or grab the
whole dataset and then seek on the client side? If it uses a cursor, I'd
expect it to be the fastest and simplest implementation. Since a lot of
libs are designed to work with MySQL, they often are written in the first
method, where select count(*) is quite quick on MySQL, and MySQL doesn't
have cursor support.

With Postgresql, the cursor is likely to be the faster method.


I agree --- with a LIMIT and COUNT(*), you run the query twice. With a
cursor, you run it once, and only pull the rows to the client you want.

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 11 '05 #5
CSN

--- "scott.marlowe" <sc***********@ihs.com> wrote:
begin;
declare bubba as cursor for select * from table
order by fieldname;
move forward 100 in bubba;
fetch 5 from bubba;
rollback;

Then you get the same kind of effect, but only 5
rows have to be retrieved
from the database to the client, and pg_fetch_array
will now iterate over
those 5 rows only, and then run dry, so to speak.


Actually, with this method would you be able to get
the count of all rows that could be returned (not just
the 5)?

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 11 '05 #6
CSN

Behind the scenes, is there much performance
difference between:

SELECT *
FROM table_with_millions_of_rows
ORDER BY col1;

and:

SELECT *
FROM table_with_millions_of_rows
ORDER BY col1
LIMIT 100
OFFSET 100000;

?

Wouldn't the second query would use far less memory?
CSN
--- Bruce Momjian <pg***@candle.pha.pa.us> wrote:
scott.marlowe wrote:
On Fri, 5 Sep 2003, CSN wrote:
Since you usually need to know the total number of rows a query would return, do you think it's better to:

a) Do one query with a LIMIT and OFFSET to get the results, and another COUNT query to get the total number of rows?

b) Do a single query without a LIMIT and OFFSET, then do a seek or similiar to get at the rows you want?
Most tutorials, code, etc. I've seen do "a". The
eclipse library does "b".


Either way works. Does the eclipse library use a

cursor, or grab the
whole dataset and then seek on the client side?

If it uses a cursor, I'd
expect it to be the fastest and simplest

implementation. Since a lot of
libs are designed to work with MySQL, they often

are written in the first
method, where select count(*) is quite quick on

MySQL, and MySQL doesn't
have cursor support.

With Postgresql, the cursor is likely to be the

faster method.

I agree --- with a LIMIT and COUNT(*), you run the
query twice. With a
cursor, you run it once, and only pull the rows to
the client you want.

--
Bruce Momjian |
http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610)
359-1001
+ If your life is a hard drive, | 13 Roberts
Road
+ Christ can be your backup. | Newtown
Square, Pennsylvania 19073

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 11 '05 #7
On Fri, 5 Sep 2003, CSN wrote:

--- "scott.marlowe" <sc***********@ihs.com> wrote:
begin;
declare bubba as cursor for select * from table
order by fieldname;
move forward 100 in bubba;
fetch 5 from bubba;
rollback;

Then you get the same kind of effect, but only 5
rows have to be retrieved
from the database to the client, and pg_fetch_array
will now iterate over
those 5 rows only, and then run dry, so to speak.


Actually, with this method would you be able to get
the count of all rows that could be returned (not just
the 5)?


My previous one about using absolute count was wrong, btw, so you can
either fetch forward all and get the count that returns or run select
count(*). note that if you fetch forward all on a complex query, you may
NOT be able to fetch backward all since cursors have a hard time going
backwards on complex queries.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 11 '05 #8

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

Similar topics

2
by: Somerset Bob | last post by:
I've got pagination errors in a phpBB board. Some forums are showing "page 1 of 0". Previous enquiries both here and at the phpBB forum have given me half-answers, the latest of which contained...
9
by: Sharif T. Karim | last post by:
Anyone know of a up-to-date tutorial for pagination where I can have it like: Prev 1 2 3 4 Next Thanks. -- Sharif T. Karim ....you don't know wrath yet...
1
by: Faree | last post by:
I am workign on a news portal which needs paginaiton as usual.but all the code i got depends on the number of records that will come from database :( .it is working well too. But i need...
2
by: Chris H | last post by:
I am having a problem with pagination, basically the problem is happening in the "PREV / NUMBERS / NEXT" links, it appears as if the reason is becasue the increment and decrement operators aren't...
11
by: ste | last post by:
Hi there, Further to my recent posts where I've received excellent help from Rik and Jerry, I've ended up with an image gallery on my website that displays images in a table, 3 images per row. ...
1
by: shalini jain | last post by:
Hi, I want to know how can we do pagination using XSL. There are number of tutorials available on pagination using PHP but nothing with XSL. i am really stuck with my code. Below is the code that...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
4
by: ArizonaJohn | last post by:
Hello, The code below works great. The user enters a name into an HTML form, the code looks up a table with that name, and then that table is displayed. I am trying to use pagination with it,...
2
by: kkshansid | last post by:
this is my search page on which i am getting two parameters from previous page but the problem is that as soon as i click any other next pages my sql query fails as it doesnt get these two parameters...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.