X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CC4E4D9EC5 for ; Wed, 19 Oct 2005 04:28:49 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 59890-07 for ; Wed, 19 Oct 2005 07:28:46 +0000 (GMT) Received: from pw6tiger.de (pw6tiger.de [217.160.140.116]) by svr1.postgresql.org (Postfix) with SMTP id 819E1D9A49 for ; Wed, 19 Oct 2005 04:28:46 -0300 (ADT) Received: (qmail 21938 invoked by uid 505); 19 Oct 2005 07:28:44 -0000 Received: from vygen@gmx.de by planwerk6 by uid 89 with qmail-scanner-1.14 (virus scan: Clear:. Processed in 0.311204 secs); 19 Oct 2005 07:28:44 -0000 Received: from isi-dsl-125-237.isionline-dialin.de (HELO jv.pw6.de) (vygen@planwerk6.de@213.128.125.237) by pw6tiger.de with SMTP; 19 Oct 2005 07:28:43 -0000 From: Janning Vygen To: pgsql-sql@postgresql.org Subject: Re: Double query (limit and offset) Date: Wed, 19 Oct 2005 09:34:22 +0200 User-Agent: KMail/1.7.2 Cc: Michael Landin Hostbaek References: <20051011151105.GA29943@mich2.itxmarket.com> In-Reply-To: <20051011151105.GA29943@mich2.itxmarket.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200510190934.22905.vygen@gmx.de> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.047 required=5 tests=[AWL=-0.003, FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200510/213 X-Sequence-Number: 23066 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/static/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