Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 5B5261337B9C for ; Wed, 20 Apr 2011 05:47:18 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 17240-02 for ; Wed, 20 Apr 2011 08:46:59 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f46.google.com (mail-vw0-f46.google.com [209.85.212.46]) by mail.postgresql.org (Postfix) with ESMTP id 7260D1337BA3 for ; Wed, 20 Apr 2011 05:46:59 -0300 (ADT) Received: by vws1 with SMTP id 1so392710vws.19 for ; Wed, 20 Apr 2011 01:46:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=cqa9YcUNh0OgHX5v+oibMKDK+8oUdsX33R+V36s/4QQ=; b=bFWPv9zpNdQTIrjnrOK93hTyBtltkmY4W9chliD5v8NCkCa6wDvcBalVcnNb7qPIen zCGWKX1gqhTl6679/62B+omgeRwf5yljTtQ+8fVTmYyjAjhvgt21r55F8cdCAW1spDsw fBlKz0TwHvSAAhmo3ctl2SO4iZERyuwgyRcxk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=ZB/+Wyj5wB3+PY2edaJGaryUTVAxNG1jKK5uHdja2VH72mL7hysRcTgD+KbEkpIDK3 F67N/b1q2+M/zbvd+YJLtKwa2+hl556S2OeZXD2l3ks+zivBnsi72z1w+FaWZSpShKGs NO1/uYOYMsV5z5fwbUR4HOLB70USxBjEo8m6c= MIME-Version: 1.0 Received: by 10.52.97.10 with SMTP id dw10mr415450vdb.23.1303289219357; Wed, 20 Apr 2011 01:46:59 -0700 (PDT) Received: by 10.52.181.233 with HTTP; Wed, 20 Apr 2011 01:46:59 -0700 (PDT) Date: Wed, 20 Apr 2011 10:46:59 +0200 Message-ID: Subject: not using partial index From: Willy-Bas Loos To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=20cf307d01eef59a7704a155aa15 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.887 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RFC_ABUSE_POST=0.001, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201104/278 X-Sequence-Number: 43340 --20cf307d01eef59a7704a155aa15 Content-Type: text/plain; charset=ISO-8859-1 Hi, I'm using PostgreSQL 8.4 (and also 8.3). A partial index like this: CREATE INDEX table2_field1_idx ON table2 (field1) WHERE NOT field1 ISNULL; Will not be used when select one record from 100K records: explain select * from table2 where field1 = 256988 'Seq Scan on table2 (cost=0.00..1693.01 rows=1 width=4)' ' Filter: (field1 = 256988)' But it WILL be used like this: explain select * from table2 where field1 = 256988 and not field1 isnull 'Index Scan using table2_field1_idx on table2 (cost=0.00..8.28 rows=1 width=4)' ' Index Cond: (field1 = 256988)' But, when i change the index from"NOT field1 ISNULL " to "field1 NOTNULL", then the index WILL be used in both queries: explain select * from table1 where field1 = 256988 'Index Scan using table1_field1_idx on table1 (cost=0.00..8.28 rows=1 width=4)' ' Index Cond: (field1 = 256988)' 'Index Scan using table1_field1_idx on table1 (cost=0.00..8.28 rows=1 width=4)' ' Index Cond: (field1 = 256988)' ' Filter: (NOT (field1 IS NULL))' Any ideas why this might be? Cheers, WBL Code below: --drop table table1; create table table1(field1 integer); CREATE INDEX table1_field1_idx ON table1 (field1) WHERE field1 NOTNULL; insert into table1 values(null); insert into table1 select generate_series(1,100000); vacuum analyze table1; explain select * from table1 where field1 = 256988 explain select * from table1 where field1 = 256988 and not field1 isnull --drop table table2; create table table2(field1 integer); CREATE INDEX table2_field1_idx ON table2 (field1) WHERE NOT field1 ISNULL; insert into table2 values(null); insert into table2 select generate_series(1,100000); vacuum analyze table2; explain select * from table2 where field1 = 256988 explain select * from table2 where field1 = 256988 and not field1 isnull -- "Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw --20cf307d01eef59a7704a155aa15 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
Hi,

I'm using PostgreSQL 8.4 (and also 8.= 3).

A partial index like this:
CREA= TE INDEX table2_field1_idx
=A0=A0ON table2 (field1)
=A0WHERE NOT field1 ISNULL;

Will not be used when = select one record from 100K records:

explain selec= t * from table2 where field1 =3D 256988
'Seq Scan on tab= le2 =A0(cost=3D0.00..1693.01 rows=3D1 width=3D4)'
' =A0Filter: (field1 =3D 256988)'

But it WILL be used like this:

explain select *= from table2 where field1 =3D 256988 and not field1 isnull
'I= ndex Scan using table2_field1_idx on table2 =A0(cost=3D0.00..8.28 rows=3D1 = width=3D4)'
' =A0Index Cond: (field1 =3D 256988)'


But, when i change the index from"NOT field1 ISNULL &q= uot; to "field1 NOTNULL", then the index WILL be used in both que= ries:

explain select * from table1 where field1 =3D 256988
'Index Scan using table1_field1_idx on table1 =A0(cost=3D0= .00..8.28 rows=3D1 width=3D4)'
' =A0Index Cond: (field1 = =3D 256988)'

'Index Scan using table1_field1_idx on t= able1 =A0(cost=3D0.00..8.28 rows=3D1 width=3D4)'
' =A0Ind= ex Cond: (field1 =3D 256988)'
' =A0Filter: (NOT (field1 I= S NULL))'


Any ideas why this might be?
=


Cheers,

WBL

Code below:

--drop tabl= e table1;
create table table1(field1 integer);
CREATE INDEX table1_fie= ld1_idx
=A0=A0ON table1 (field1)
=A0=A0WHERE field1 NOT= NULL;
insert into table1 values(null);
insert into tabl= e1 select generate_series(1,100000);

vacuum analyze table1;

explain= select * from table1 where field1 =3D 256988
explain select * fr= om table1 where field1 =3D 256988 and not field1 isnull


--drop table table2;
create table table2(fiel= d1 integer);
CREATE INDEX table2_field1_idx
=A0=A0ON ta= ble2 (field1)
=A0=A0WHERE NOT field1 ISNULL;
insert int= o table2 values(null);
insert into table2 select generate_series(1,100000);

vacuum analyze table2;

explain select * fr= om table2 where field1 =3D 256988
explain select * from table2 wh= ere field1 =3D 256988 and not field1 isnull


--
"Patriotism is the convicti= on that your country is superior to all others because you were born in it.= " -- George Bernard Shaw
--20cf307d01eef59a7704a155aa15--