Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dJ3ae-0002el-By for pgsql-performance@arkaria.postgresql.org; Thu, 08 Jun 2017 19:59:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dJ3ad-0005B4-RO for pgsql-performance@arkaria.postgresql.org; Thu, 08 Jun 2017 19:59:31 +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 1dJ3Yt-00026o-UG for pgsql-performance@postgresql.org; Thu, 08 Jun 2017 19:57:44 +0000 Received: from mail-pg0-x22d.google.com ([2607:f8b0:400e:c05::22d]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1dJ3Yq-0006vl-Tv for pgsql-performance@postgresql.org; Thu, 08 Jun 2017 19:57:43 +0000 Received: by mail-pg0-x22d.google.com with SMTP id v18so19279170pgb.1 for ; Thu, 08 Jun 2017 12:57:40 -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=4PPtV2KM59wMmjQT2ieB6VEky9buGOOqHUOlePPBaDc=; b=JtikFGXP6texRwviG/Mt3N/rNPM4Aoeo0DkNkrAXX6CgNKNw/RWdgb72KMkpqvI/BA jo0yXQErEhzsaHFAj1CHYi+G9kqwzH1qw5+UQluT5V9ZZ0BBm/dyLxJumNpdR28p9iio kA3pdqEC2dN5O4SIWkbGR+XvnePidOw+Wm0W/v+XHj0P3o2wFFUGztLlIAwBDZyOxkpR UVGdc5+IOluTDzhRLy21Mo38DbFgzGkTBKBb99q599fRataQLSfz3i7gpQtcWIGmN7rk Nj6euToDDgTK059CY0Uy0e1msNH3qtOArGZH7YwGqaWbrPwSBiUjm/+rVGrbYC6t6YN+ +KUQ== 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=4PPtV2KM59wMmjQT2ieB6VEky9buGOOqHUOlePPBaDc=; b=rXb6eB1It5BzXnz8Gd6WdvoqXL5mlGgVt25SZdyxrBHLq2rBgVpXN9RviwZNHsAbri jcVsONSi2uMoWuUZZrU69LWCsylQ1yazF4h/ExFBevGMgqENQaU+ipwVWgAJS1yJNaQz SWj0tJZd9NqJaqQSUrDrePxR9xrCfyesVbBfOmwnzobRCLt6Gs98b0nS+jrkUaZxq0LA Rk+Wmx5RU3qg1G1H1uJ2g7lxdSyyb7LA+qPJ91UtcdOlRd/bLuroG7Od3p+q1odkyvkb 6p3VYgxs6+SBqBnF2izUgZlfBwdcWhVmrIHNbljguzfBe2IJubdSyDiLMogwDOC0BF8f t9xQ== X-Gm-Message-State: AODbwcBELuEO1mWYs8EAiy9dHkCe3oumLmYFveycgYIKglnL1oybgSC8 iqxTHofYXXQ5Cy2UACX43S0wGKc2tA== X-Received: by 10.98.133.88 with SMTP id u85mr38608773pfd.211.1496951858837; Thu, 08 Jun 2017 12:57:38 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.166.237 with HTTP; Thu, 8 Jun 2017 12:57:38 -0700 (PDT) In-Reply-To: References: <13615.1496933920@sss.pgh.pa.us> From: Merlin Moncure Date: Thu, 8 Jun 2017 14:57:38 -0500 Message-ID: Subject: Re: index of only not null, use function index? To: Jeremy Finzel Cc: Tom Lane , Ariel , 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 Thu, Jun 8, 2017 at 11:05 AM, Jeremy Finzel wrote: > On Thu, Jun 8, 2017 at 9:58 AM, Tom Lane wrote: >> Merlin Moncure writes: >> > 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; >> >> Right, you can frequently get a lot of mileage out of indexing something >> that's unrelated to the predicate condition, but is also needed by the >> query you want to optimize. > Normally, I find that in these situations, it makes sense to index the > primary key of the table WHERE col is not null, because it will usually > cover the largest number of cases, and is much better than a two-value > boolean index, for example. [meta note: please try to avoid top-posting] Yeah, if you index the primary key and query it like this: CREATE INDEX ON table (pkey) WHERE col IS NOT NULL; SELECT pkey FROM table WHERE col IS NOT NULL ORDER BY pkey LIMIT n; This can give the best possible results since this can qualify for an index only scan :-). merlin -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance