X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 506C4D1BA8B for ; Fri, 22 Aug 2003 19:48:06 -0300 (ADT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 89019-02 for ; Fri, 22 Aug 2003 19:48:03 -0300 (ADT) Received: from c001.snv.cp.net (h013.c001.snv.cp.net [209.228.32.127]) by svr1.postgresql.org (Postfix) with SMTP id E9372D1BA82 for ; Fri, 22 Aug 2003 19:47:54 -0300 (ADT) Received: (cpmta 20484 invoked from network); 22 Aug 2003 15:48:02 -0700 Received: from 209.228.32.120 (HELO mail.dilger.cc.criticalpath.net) by smtp.register-admin.com (209.228.32.127) with SMTP; 22 Aug 2003 15:48:02 -0700 X-Sent: 22 Aug 2003 22:48:02 GMT Received: from [216.196.203.241] by mail.dilger.cc with HTTP; Fri, 22 Aug 2003 15:48:01 -0700 (PDT) Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 7bit MIME-Version: 1.0 To: tgl@sss.pgh.pa.us Cc: scott.marlowe@ihs.com, s.barnett-cormack@lancaster.ac.uk, pgsql-admin@postgresql.org From: "Nikolaus Dilger" Subject: Re: Partial indexes (was: Re: Indexing a boolean) X-Sent-From: nikolaus@dilger.cc Date: Fri, 22 Aug 2003 15:48:01 -0700 (PDT) X-Mailer: Web Mail 5.5.0-3_sol28 Message-Id: <20030822154802.21151.h006.c001.wm@mail.dilger.cc.criticalpath.net> X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/264 X-Sequence-Number: 9985 Tom's example shows that the query actually takes longer using the index than without. And that's not surprising. A btree index doesn't buy you much when you have very low cardinality. That's ideal for bitmap indexes that PostgreSQL currently doesn't offer. In the expample of the original question no index on the boolean column is probably also better/faster. Just try it. What may help is a multicolum index. So lets say you have the columns first_name, last_name, gender. Instead of creating one index for last_name and one for gender. Just create a multicolumn index on last_name and gender. Of course that assumes that you have both columns in your WHERE clause combined with an AND. CREATE INDEX name_gender ON person (last_name, gender); SELECT * FROM person WHERE last_name='Smith' AND gender='male'; Regards, Nikolaus On Fri, 22 Aug 2003 10:34:39 -0400, Tom Lane wrote: > > "scott.marlowe" writes: > > Basically, for partial / functional indexes, the > create index where clause > > needs to exactly (or nearly so) match the select > query's where clause: > > Another consideration is that an index declaration like > this: > > create index fooi on foo (flag) where flag; > > is really rather redundant, since the entries will only > be made for rows > where flag is true, and so storage of the column value > in the index is > useless. You may be able to get more mileage out of > the index by making > the index on another column that you often test in > conjunction with the > flag. For example: > > regression=# create table foo (flag bool, ts timestamp); > CREATE TABLE > regression=# create index fooi on foo(ts) where flag; > CREATE INDEX > regression=# explain select * from foo where ts > > '2003-10-11' and flag; > QUERY PLAN > -------------------------------------------------------------------------------- > Seq Scan on foo (cost=0.00..22.50 rows=167 width=9) > Filter: ((ts > '2003-10-11 00:00:00'::timestamp > without time zone) AND flag) > (2 rows) > > regression=# set enable_seqscan TO 0; > SET > regression=# explain select * from foo where ts > > '2003-10-11' and flag; > QUERY PLAN > ------------------------------------------------------------------------- > Index Scan using fooi on foo (cost=0.00..43.25 > rows=167 width=9) > Index Cond: (ts > '2003-10-11 00:00:00'::timestamp > without time zone) > Filter: flag > (3 rows) > > (In a more realistic situation, the planner would > probably have chosen > the indexscan without any prompting.) > > regards, tom lane > > ---------------------------(end of > broadcast)--------------------------- > TIP 4: Don't 'kill -9' the postmaster