Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 61ECD650183 for ; Tue, 11 Nov 2008 16:47:10 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 40116-07 for ; Tue, 11 Nov 2008 16:47:07 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 137086.vserver.de (static-ip-62-75-137-86.inaddr.intergenia.de [62.75.137.86]) by mail.postgresql.org (Postfix) with ESMTP id 66CCC65015A for ; Tue, 11 Nov 2008 16:47:06 -0400 (AST) Received: from a-kretschmer.de (p54B38712.dip0.t-ipconnect.de [84.179.135.18]) (authenticated bits=0) by 137086.vserver.de (8.12.8/8.12.8) with ESMTP id mABKl4QE022401 for ; Tue, 11 Nov 2008 21:47:04 +0100 Received: from kretschmer by a-kretschmer.de with local (Exim 4.69) (envelope-from ) id 1L008d-00047j-7I for pgsql-performance@postgresql.org; Tue, 11 Nov 2008 21:47:03 +0100 Date: Tue, 11 Nov 2008 21:47:03 +0100 From: Andreas Kretschmer To: pgsql-performance@postgresql.org Subject: Re: Using index for IS NULL query Message-ID: <20081111204703.GA15745@tux> References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-OS: Debian/GNU Linux - weil ich es mir Wert bin! X-GPG-Fingerprint: EE16 3C01 7B9C 10F7 2C8B 3B86 4DB3 D9EE 7F45 84DA X-Message-Flag: "Windows" is not the answer. "Windows" is the question and the answer is "no"! X-Lugdd: Gerd Kube X-Info: My name is root. Just root. And I am licensed to kill -9 User-Agent: Mutt/1.5.17+20080114 (2008-01-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/79 X-Sequence-Number: 31810 Andrus schrieb: > Index is not used for > > is null > > condition: > > create index makse_dokumnr_idx on makse(dokumnr); > explain select > sum( summa) > from MAKSE > where dokumnr is null > > "Aggregate (cost=131927.95..131927.96 rows=1 width=10)" > " -> Seq Scan on makse (cost=0.00..131927.94 rows=1 width=10)" > " Filter: (dokumnr IS NULL)" > > > > Table makse contains 1200000 rows and about 800 rows with dokumnr is null > so using index is much faster that seq scan. > How to fix ? Create a partial index like below: test=# create table foo ( i float); CREATE TABLE Zeit: 1,138 ms test=*# insert into foo select random() from generate_series(1,1000000); INSERT 0 1000000 test=*# insert into foo values (NULL); INSERT 0 1 test=*# create index idx_foo on foo(i) where i is null; CREATE INDEX test=*# explain analyse select * from foo where i is null; QUERY PLAN ------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on foo (cost=5.51..4690.89 rows=5000 width=8) (actual time=0.037..0.038 rows=1 loops=1) Recheck Cond: (i IS NULL) -> Bitmap Index Scan on idx_foo (cost=0.00..4.26 rows=5000 width=0) (actual time=0.033..0.033 rows=1 loops=1) Index Cond: (i IS NULL) Total runtime: 0.068 ms (5 Zeilen) Maybe there are other solutions... Andreas -- Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect. (Linus Torvalds) "If I was god, I would recompile penguin with --enable-fly." (unknown) Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°