Unix Technical Forum

Re: Double query (limit and offset)

This is a discussion on Re: Double query (limit and offset) within the pgsql Sql forums, part of the PostgreSQL category; --> Am Dienstag, 11. Oktober 2005 17:11 schrieb Michael Landin Hostbaek: > List, > > I'm using the OFFSET / ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 12:50 PM
Janning Vygen
 
Posts: n/a
Default Re: Double query (limit and offset)

Am Dienstag, 11. Oktober 2005 17:11 schrieb Michael Landin Hostbaek:
> List,
>
> I'm using the OFFSET / LIMIT combo in order to split up my query, so it
> only parses 20 rows at a time (for my php-scripted webpage).
>
> I'm using two queries; the first basically doing a select count(*) from
> [bla bla]; the second grabbing the actual data while setting LIMIT and
> OFFSET.
>
> In addition, I'm using the first query plus some calculations to parse
> total hits to the query, and number of pages etc etc.
>
> Now, my problem is this, the first query is simply counting all rows
> from the main table, whereas the second query has plenty of JOINS, and a
> GROUB BY statement - it's a fairly heavy query. The total (reported by
> the first query), it not at all the same as the amount of rows returned
> by the second query. I'd like to avoid having to run the "heavy" query
> twice, just in order to get the number of rows.
>
> Is there a smarter way of doing it ?


There is a smarter way of asking: Show us the queries!
But it also depends on what you expect the user to do.

Some hints:

In generell if you count table A and afterwards you join and group your tables
A,B,C,D the number of rows in the resultset may vary, of course.

- You could fetch ALL rows with the second query, count them (pg_numrows),
show the first ten results and keep all other results in cache for the next
webpage. (if we are talking about a smal set of rows not if we are talking
about 1 billion rows, of course)

- You can rewrite your first query to return the correct number and see if it
has a real performance impact. Optimize our query and you will be fine.
Postgresql is very fast.

- You can show the user an estimated count, if the correct number isn't of any
interest (like google)

- If you ever look at the CURSOR thing in postgresql and it looks attractive
to you ( http://www.postgresql.org/docs/8.0/s...sql-fetch.html ): I think
it isn't useful in a normal web environment, but it could be nice together
with AJAX scripting.

kind regards,
janning


---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com