pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Keary Suska <hierophant@pcisys.net>
To: Postgres-PHP <pgsql-php@postgresql.org>
Subject: Re: Pagination of search results
Date: Wed, 23 Nov 2005 14:06:32 -0700
Message-ID: <BFAA27E8.23AA4%hierophant@pcisys.net> (raw)
In-Reply-To: <ddcd549e0511230814s1d5f235en17a25b7ca9edea36@mail.gmail.com>

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 <LOTS OF CLAUSES>) AS c WHERE
> <SAME LOT OF CLAUSES> 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"




view thread (5+ messages)  latest in thread

Message-ID: <BFAA27E8.23AA4%hierophant@pcisys.net>
Permalink:  ../BFAA27E8.23AA4%25hierophant@pcisys.net/
Also on:    postgresql.org/message-id/BFAA27E8.23AA4%hierophant@pcisys.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: hierophant@pcisys.net
  Subject: Re: Pagination of search results
  In-Reply-To: <BFAA27E8.23AA4%hierophant@pcisys.net>
  References: <ddcd549e0511230814s1d5f235en17a25b7ca9edea36@mail.gmail.com> <BFAA27E8.23AA4%hierophant@pcisys.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox