Re: which one is faster

Lists: pgsql-performance
From: AI Rumman <rummandba(at)gmail(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: which one is faster
Date: 2010-10-26 10:56:48
Message-ID: AANLkTimgjmGCcxjSmf6BvQ=BBGLBrMspfcFgWhrpsVVr@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

Which one is faster?
select count(*) from talble
or
select count(id) from table
where id is the primary key.


From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: AI Rumman <rummandba(at)gmail(dot)com>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 10:59:05
Message-ID: AANLkTikWbbFNykGXdobR06dsePn40JrBYvfFZLmu+H1j@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 26 October 2010 12:56, AI Rumman <rummandba(at)gmail(dot)com> wrote:

> Which one is faster?
> select count(*) from talble
> or
> select count(id) from table
> where id is the primary key.
>

Check the query plan, both queries are the same.

regards
Szymon


From: Marcin Mirosław <marcin(at)mejor(dot)pl>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 11:59:03
Message-ID: 4CC6C287.8070803@mejor.pl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

W dniu 26.10.2010 12:59, Szymon Guz pisze:
> both queries are the same.

IMHO they aren't the same, but they returns the same value in this case.
I mean count(field) doesn't count NULL values, count(*) does it.
I'm writing this only for note:)
Regards


From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Marcin Mirosław <marcin(at)mejor(dot)pl>
Cc: pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:05:36
Message-ID: AANLkTi=sOW-X+DcZPaBk1S_sHzJV97XrSOHmsSXsYpeo@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

2010/10/26 Marcin Mirosław <marcin(at)mejor(dot)pl>

> W dniu 26.10.2010 12:59, Szymon Guz pisze:
> > both queries are the same.
>
> IMHO they aren't the same, but they returns the same value in this case.
> I mean count(field) doesn't count NULL values, count(*) does it.
> I'm writing this only for note:)
> Regards
>
>
Yup, indeed. I omitted that note, as it was written that the field is
primary key :).

regards
Szymon


From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: Szymon Guz <mabewlun(at)gmail(dot)com>
Cc: Marcin Mirosław <marcin(at)mejor(dot)pl>, pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:08:37
Message-ID: AANLkTinuLB_oZfFT17irk3OH-0q=QJnCpYWN6PED0LRY@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

implementation wise, count(*) is faster. Very easy to test:

SELECT COUNT(*) FROM generate_series(1,100) a, generate_series(1,1000) b;

SELECT COUNT(a) FROM generate_series(1,100) a, generate_series(1,1000) b;

;]


From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
Cc: Marcin Mirosław <marcin(at)mejor(dot)pl>, pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:16:31
Message-ID: AANLkTi=sidQSMAPNVJF9_EJ7xUiNX65ujsSXb9mF9RhN@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

2010/10/26 Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>

> implementation wise, count(*) is faster. Very easy to test:
>
> SELECT COUNT(*) FROM generate_series(1,100) a, generate_series(1,1000) b;
>
> SELECT COUNT(a) FROM generate_series(1,100) a, generate_series(1,1000) b;
>
>
> ;]
>

Well, strange. Why is that slower?


From: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
To: Szymon Guz <mabewlun(at)gmail(dot)com>
Cc: Marcin Mirosław <marcin(at)mejor(dot)pl>, pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:20:33
Message-ID: AANLkTimx3bcBoxVeU1CLVONGn+UE-Cf14UzNPgciL6Vp@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

2010/10/26 Szymon Guz <mabewlun(at)gmail(dot)com>:
>
> Well, strange. Why is that slower?

To answer that fully, you would need to see the implementation.
suffice to say,

count(a) does:

if (a <> NULL)
{
count++;
}

and count(*) does:

count++;

--
GJ


From: Szymon Guz <mabewlun(at)gmail(dot)com>
To: Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>
Cc: Marcin Mirosław <marcin(at)mejor(dot)pl>, pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:23:21
Message-ID: AANLkTikNYrEKhr=rFpGyEhgROv1wWE1XJk_ve1wtshCV@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

2010/10/26 Grzegorz Jaśkiewicz <gryzman(at)gmail(dot)com>

> 2010/10/26 Szymon Guz <mabewlun(at)gmail(dot)com>:
> >
> > Well, strange. Why is that slower?
>
> To answer that fully, you would need to see the implementation.
> suffice to say,
>
> count(a) does:
>
> if (a <> NULL)
> {
> count++;
> }
>
> and count(*) does:
>
> count++;
>
>
>
Yup, I was afraid of that, even if there is not null on the column... but I
think usually nobody notices the difference with count.

regards
Szymon


From: Mladen Gogala <mladen(dot)gogala(at)vmsinfo(dot)com>
To: pgsql-performance(at)postgresql(dot)org
Subject: Re: which one is faster
Date: 2010-10-26 12:32:54
Message-ID: 4CC6CA76.1020509@vmsinfo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-performance

On 10/26/2010 6:56 AM, AI Rumman wrote:
> Which one is faster?
> select count(*) from talble
> or
> select count(id) from table
> where id is the primary key.
PostgreSQL doesn't utilize the access methods known as "FULL INDEX SCAN"
and "FAST FULL INDEX SCAN", so the optimizer will generate the
sequential scan in both cases. In other words, PostgreSQL will read the
entire table when counting, no matter what.

--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
www.vmsinfo.com