pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Karsten Hilbert <Karsten.Hilbert@gmx.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: count(*) vs count(id)
Date: Wed, 03 Feb 2021 11:30:45 +0100
Message-ID: <d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at> (raw)
In-Reply-To: <YBpy2meHVgF4yMIa@hermes.hilbert.loc>
References: <CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com>
	<cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com>
	<CAOLfK3W99Cdf9Sq+XyJv5MAjp9wsYBKpAC2UAVp7+dB881KmjQ@mail.gmail.com>
	<3502043.1612226098@sss.pgh.pa.us>
	<CAOLfK3XBUxP3jbAnKVn9cei4hh4snozhVj5yjxCipVnYvwfTsA@mail.gmail.com>
	<fe5b3e56cf75094864ac0d05eadefacc42d572c0.camel@cybertec.at>
	<CAN3Qy4q1VXQHbUsVH7+yKumdfyfcxQAaHTGS5XKgjZ-5PMOpRA@mail.gmail.com>
	<CAKHqFkKWVam7LyVM7bxDMXxsr4fZVbNuTG=rfHx07jpF5f6MMA@mail.gmail.com>
	<YBpy2meHVgF4yMIa@hermes.hilbert.loc>

On Wed, 2021-02-03 at 10:54 +0100, Karsten Hilbert wrote:
> > I just ran a few practical tests on large (~14mil rows) tables that have
> > multiple indexes.
> > SELECT COUNT(id) forces PostgreSQL to use the primary key index.
> > SELECT COUNT(*) allows PostgreSQL to chose an index to use and it seems to
> > be choosing one of smaller size which leads to less IO and hence returns
> > the result faster.
> 
> Would you mind throwing in a test for
> 
>         select count(1) ...

No need to test.  That will be slightly slower because it repeatedly checks
if 1 is NULL or not.

Yours,
Laurenz Albe
-- 
Cybertec | https://www.cybertec-postgresql.com






view thread (14+ messages)

Message-ID: <d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at>
Permalink:  ../d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at/
Also on:    postgresql.org/message-id/d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, Karsten.Hilbert@gmx.net
  Subject: Re: count(*) vs count(id)
  In-Reply-To: <d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at>
  References: <CAOLfK3VWa_Mv3OSumvpHZdppd+qDj8EUWCdxY5+vUe=gAo+h0A@mail.gmail.com> <cb465259-e29a-8fc2-1890-2666b8d79f36@gmail.com> <CAOLfK3W99Cdf9Sq+XyJv5MAjp9wsYBKpAC2UAVp7+dB881KmjQ@mail.gmail.com> <3502043.1612226098@sss.pgh.pa.us> <CAOLfK3XBUxP3jbAnKVn9cei4hh4snozhVj5yjxCipVnYvwfTsA@mail.gmail.com> <fe5b3e56cf75094864ac0d05eadefacc42d572c0.camel@cybertec.at> <CAN3Qy4q1VXQHbUsVH7+yKumdfyfcxQAaHTGS5XKgjZ-5PMOpRA@mail.gmail.com> <CAKHqFkKWVam7LyVM7bxDMXxsr4fZVbNuTG=rfHx07jpF5f6MMA@mail.gmail.com> <YBpy2meHVgF4yMIa@hermes.hilbert.loc> <d89a256ac1d07b7e833a979689f306f76f1ce2e9.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox