Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dIykF-0003Ug-U6 for pgsql-performance@arkaria.postgresql.org; Thu, 08 Jun 2017 14:49:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dIykF-0003gG-CO for pgsql-performance@arkaria.postgresql.org; Thu, 08 Jun 2017 14:49:07 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dIyiS-0007Qo-PG for pgsql-performance@postgresql.org; Thu, 08 Jun 2017 14:47:16 +0000 Received: from mail-pf0-x236.google.com ([2607:f8b0:400e:c00::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dIyiQ-0000zL-61 for pgsql-performance@postgresql.org; Thu, 08 Jun 2017 14:47:16 +0000 Received: by mail-pf0-x236.google.com with SMTP id 83so17993937pfr.0 for ; Thu, 08 Jun 2017 07:47:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=q4VioGCjbDqTPooVmmeYrwwDN80n+DLp5SebjxP55MY=; b=Q7IxoUqWwEK6VYLak5IbmVXW+nlFUBLguiwOqrxMie2xJG+vGEaReW6CV0bdIYIjTX 2RsLqtLJ6EU9TfnjwoIypYY6/JBuwij6UbUZcabBKtQB0lsscnGK7moQ4AAs/iBrl9kb /vuG6BFfBrUI5CKDZyplKeuoTEido+5ofUPJ9XWLRWKDkg5zsWXt08ONCVfr1WMZjXph g0t3yB/wO5aHLKbpb+m6bBeDzODx8UJiEkn0BbhXzphIgF3oFTwyRw9NAox/B1SSXSnV 43iB0XmT9z2s0ASLpconXT1CBVrcTRXEL5fNhFMHzHTkMFzzuYdx9DAo3ZLVJ4k5k16z QEgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=q4VioGCjbDqTPooVmmeYrwwDN80n+DLp5SebjxP55MY=; b=lTg3E62dTCLNyWrUjWebQrrv+sRXxsKX1EPe+WrTbjU/G2HLVk+VlQ+g1rDbcqcwh+ 1w6JW3zC0iQ5+gV4dTtZHO3RrQd7LiLVAQj3p5xkGacMKKqgmZQZ5JF3tX+b1CZB2qRP XeJ/b4h/EdfEY41Jccu0GlJZswQIIh2UehiNmoJcEVKD1ZtPSkedAqzCV3+rpg8omGR3 x2lCqGKGcMGT9I76/IPsIddOkx4Eh78D3oNg2pfIr05u90fk2R7YXOJTzfymGvlMQXBA Me7jFzeMnSfhWvL9x5Q6oe1JJ7c3LMNbGy6qbjvtlaRg4CM5/bgWUNMNAQ0oDrWOyXKE YMxw== X-Gm-Message-State: AODbwcBnvjiQpSjhP33WAAipO9BT/r52yN9J7nE5yDqsOCXPrg9pzFCn ket2JtTsuZCn20WUc6IZqwmecddi9g== X-Received: by 10.99.142.201 with SMTP id k192mr23412872pge.161.1496933232020; Thu, 08 Jun 2017 07:47:12 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.166.237 with HTTP; Thu, 8 Jun 2017 07:47:11 -0700 (PDT) In-Reply-To: References: From: Merlin Moncure Date: Thu, 8 Jun 2017 09:47:11 -0500 Message-ID: Subject: Re: index of only not null, use function index? To: Ariel Cc: postgres performance list Content-Type: text/plain; charset="UTF-8" X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org On Mon, May 22, 2017 at 10:17 AM, Ariel wrote: > > I need to be able to quickly find rows where a column is not null (only a > small percent of the rows will have that column not null). > > Should I do: > > CREATE INDEX ON table ((col IS NOT NULL)) WHERE col IS NOT NULL > > or: > > CREATE INDEX ON table (col) WHERE col IS NOT NULL > > I'm thinking the first index will make a smaller, simpler, index since I > don't actually need to index the value of the column. But are there any > drawbacks I may not be aware of? Or perhaps there are no actual benefits? You are correct. I don't see any downside to converting to bool; this will be more efficient especially if 'col' is large at the small cost of some generality. Having said that, what I typically do in such cases (this comes a lot in database driven work queues) something like this: CREATE INDEX ON table (OrderCol) WHERE col IS NOT NULL; Where "OrderCol" is some field that defines some kind of order to the items that you are marking off. This will give very good performance of queries in the form of: SELECT Col FROM table WHERE col IS NOT NULL ORDER BY OrderCol LIMIT 1; merlin -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance