X-Original-To: pgsql-php-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 91988DBAEA for ; Wed, 23 Nov 2005 20:17:13 -0400 (AST) 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 04180-09 for ; Thu, 24 Nov 2005 00:17:15 +0000 (GMT) X-Greylist: delayed 03:10:35.988146 by SQLgrey- Received: from svr2.postgresql.org (svr2.postgresql.org [65.19.161.25]) by svr1.postgresql.org (Postfix) with ESMTP id 19628DBA93 for ; Wed, 23 Nov 2005 20:17:11 -0400 (AST) Received: from krusty.pcisys.net (krusty.pcisys.net [216.229.32.178]) by svr2.postgresql.org (Postfix) with ESMTP id 0043CF0B5A for ; Wed, 23 Nov 2005 21:06:35 +0000 (GMT) Received: from [192.168.0.101] (dsl-216-229-35-228.cos.pcisys.net [216.229.35.228]) by krusty.pcisys.net (8.13.3/8.13.3) with ESMTP id jANL6Wol026168 for ; Wed, 23 Nov 2005 14:06:33 -0700 (MST) User-Agent: Microsoft-Entourage/10.1.4.030702.0 Date: Wed, 23 Nov 2005 14:06:32 -0700 Subject: Re: Pagination of search results From: Keary Suska To: Postgres-PHP Message-ID: In-Reply-To: Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[none] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/25 X-Sequence-Number: 3342 on 11/23/05 9:14 AM, n.j.saunders@gmail.com purportedly said: > I'm writing a search engine, the results of which will displayed in > blocks of 25, paginated. I will also display the number of records > found (1-25 of 345). > > I'm trying to determine the most efficient way to implement this. > Since the session is dropped after the script is terminated, I'm > ruling our cursors. Also, cursors would prevent me from retrieving the > total number of records potentially returned from a search, and so I > couldn't display the correct number of page links. Also, this would > require persistent connections, which I'd like to avoid for a number > of reasons. > > The best I've come up with is "SELECT b.*, c.cnt FROM table AS b, > (SELECT COUNT(1) AS cnt FROM table WHERE ) AS c WHERE > LIMIT 25 OFFSET 0" > > However this means that the query is essentially getting executed > twice for each page. > > Is there any better way of implementing this? All advice gratefully received. As I understand the query planner in PG optimizes for LIMIT, but may not be able to work in all situations or may require some tweaking for best performance. If performance is a big issue, the way I have found (that has the least overall overhead) is to have the application cache results--either in memory (stored as part of the PHP session) or as a temporary text file tied to the session. For the latter, the tricky bit is making sure to remove old files, and there are several ways this can be done depending on anticipated volume and storage issues. For the former, you need to watch out for memory issues. I tend to prefer the latter technique unless I can guarantee that results will be small (including accumulated storage from multiple simultaneous sessions). Best regards, Keary Suska Esoteritech, Inc. "Demystifying technology for your home or business"