Received: from localhost (unknown [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id E64AD2E2DD6 for ; Fri, 16 Nov 2007 15:51:41 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 75299-05 for ; Fri, 16 Nov 2007 15:51:37 -0400 (AST) Received: from mx00.pub.collaborativefusion.com (mx00.pub.collaborativefusion.com [206.210.89.199]) by postgresql.org (Postfix) with ESMTP id 122072E2D7D for ; Fri, 16 Nov 2007 15:51:36 -0400 (AST) Received: from vanquish.pitbpa0.priv.collaborativefusion.com (vanquish.pitbpa0.priv.collaborativefusion.com [192.168.2.61]) (SSL: TLSv1/SSLv3,256bits,AES256-SHA) by wingspan with esmtp; Fri, 16 Nov 2007 14:51:35 -0500 id 0005641D.473DF4C7.00006EA8 Date: Fri, 16 Nov 2007 14:51:35 -0500 From: Bill Moran To: Jeff Davis Cc: adrobj , pgsql-performance@postgresql.org Subject: Re: Clustered/covering indexes (or lack thereof :-) Message-Id: <20071116145135.af61f4b1.wmoran@collaborativefusion.com> In-Reply-To: <1195241676.22428.218.camel@dogma.ljc.laika.com> References: <13700848.post@talk.nabble.com> <1195241676.22428.218.camel@dogma.ljc.laika.com> Organization: Collaborative Fusion X-Mailer: Sylpheed 2.4.4 (GTK+ 2.10.14; i386-portbld-freebsd6.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200711/249 X-Sequence-Number: 27664 In response to Jeff Davis : > On Sun, 2007-11-11 at 22:59 -0800, adrobj wrote: > > This is probably a FAQ, but I can't find a good answer... > > > > So - are there common techniques to compensate for the lack of > > clustered/covering indexes in PostgreSQL? To be more specific - here is my > > table (simplified): > > > > topic_id int > > post_id int > > post_text varchar(1024) > > > > The most used query is: SELECT post_id, post_text FROM Posts WHERE > > topic_id=XXX. Normally I would have created a clustered index on topic_id, > > and the whole query would take ~1 disk seek. > > > > What would be the common way to handle this in PostgreSQL, provided that I > > can't afford 1 disk seek per record returned? > > > > Periodically CLUSTER the table on the topic_id index. The table will not > be perfectly clustered at all times, but it will be close enough that it > won't make much difference. > > There's still the hit of performing a CLUSTER, however. > > Another option, if you have a relatively small number of topic_ids, is > to break it into separate tables, one for each topic_id. Or materialize the data, if performance is the utmost requirement. Create second table: materialized_topics ( topic_id int, post_ids int[], post_texts text[] ) Now add a trigger to your original table that updates materialized_topics any time the first table is altered. Thus you always have fast lookups. Of course, this may be non-optimal if that table sees a lot of updates. -- Bill Moran Collaborative Fusion Inc. http://people.collaborativefusion.com/~wmoran/ wmoran@collaborativefusion.com Phone: 412-422-3463x4023