Re: Way to use count() and LIMIT? - Mailing list pgsql-general

From Greg Sabino Mullane
Subject Re: Way to use count() and LIMIT?
Date
Msg-id E16Gj25-0005gq-00@granger.mail.mindspring.net
Whole thread Raw
In response to Way to use count() and LIMIT?  (Joe Koenig <joe@jwebmedia.com>)
Responses Re: Way to use count() and LIMIT?
List pgsql-general
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> Simply ask the database for one more record that you're going to
> display. If you get that extra record, then display the next button,
> otherwise don't. The count is irrelevent

Great idea. If you are using DBI, you can even do a $sth->rows()
call to avoid the overhead of grabbing the data via a fetch.
(unless you are using fetchall/selectall)

Still, it seems that there should be a way to grab the information,
especially if you have an "ORDER BY" clause in your query - after
all, that means at some point, postgres knows how many records
matched before it ORDERS and LIMITS them.

Something like this:

SELECT pg_get_totalrows, artist, title, year
FROM giantcdcollection
WHERE genre = 'Rock'
ORDER BY artist, title
LIMIT 21
OFFSET 200

should be possible with no slow down of query time, I would think.
Inefficient in that it returns the same number 21 times, but
a bargain compared to the current way to do it (separate count query).

Greg Sabino Mullane
greg@turnstep.com
PGP Key: 0x14964AC8 200112191051
-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE8ILggvJuQZxSWSsgRAny6AKCG/DrD2dhft6/kwjiHJ5a5jPwvFQCfeoHJ
Ej56nl3x5+snq9wynedCfUo=
=LFIM
-----END PGP SIGNATURE-----



pgsql-general by date:

Previous
From: Lee Kindness
Date:
Subject: Outputting select into file.
Next
From: Tom Lane
Date:
Subject: Re: Way to use count() and LIMIT?