Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 825986502E8 for ; Tue, 11 Nov 2008 17:02:02 -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 43837-02 for ; Tue, 11 Nov 2008 17:01:58 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gransy.com (unknown [82.208.29.208]) by mail.postgresql.org (Postfix) with ESMTP id 7FA3165015A for ; Tue, 11 Nov 2008 17:01:58 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by mail.gransy.com (Postfix) with ESMTP id 8573D7AC05D; Tue, 11 Nov 2008 22:01:55 +0100 (CET) Received: from mail.gransy.com ([127.0.0.1]) by localhost (nathalia.gransy.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id WUtOEKPqlJKd; Tue, 11 Nov 2008 22:01:55 +0100 (CET) Received: from [192.168.1.1] (ip-89-102-139-23.karneval.cz [89.102.139.23]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.gransy.com (Postfix) with ESMTP id 633577AC006; Tue, 11 Nov 2008 22:01:55 +0100 (CET) Message-ID: <4919F2C6.4000303@fuzzy.cz> Date: Tue, 11 Nov 2008 22:01:58 +0100 From: Tomas Vondra User-Agent: Thunderbird 2.0.0.9 (X11/20080213) MIME-Version: 1.0 To: Andrus CC: pgsql-performance@postgresql.org Subject: Re: Using index for IS NULL query References: In-Reply-To: Content-Type: text/plain; charset=windows-1257; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.1 tagged_above=0 required=5 tests=RDNS_NONE=0.1 X-Spam-Level: X-Archive-Number: 200811/81 X-Sequence-Number: 31812 > 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 ? Yes, NULL values are not stored in the index, but you may create functional index on (CASE WHEN dokumnr IS NULL THEN -1 ELSE dokumnr END) and then use the same expression in the WHERE clause. You may replace the '-1' value by something that's not used in the dokumnr column. regards Tomas