Re: question about count(b) where b is a custom type

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: "Pollard, Mike" <mpollard(at)cincom(dot)com>
Cc: Richard Huxton <dev(at)archonet(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: question about count(b) where b is a custom type
Date: 2005-11-16 13:44:27
Message-ID: 20051116134423.GG31063@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Nov 16, 2005 at 08:28:28AM -0500, Pollard, Mike wrote:
> Details, details. But there is a valid general question here, and
> changing the semantics of the query will not address it. When doing a
> count(col), why convert col into a string just so you can determine if
> it is null or not? This isn't a problem on a small amount of data, but
> it seems like a waste, especially if you are counting millions of
> records. Is there some way to convert this to have the caller convert
> nulls to zero and non-nulls to 1, and then just pass an int? So
> logically the backend does:
>
> Select count(case <col> when null then 0 else 1) from <table>
>
> And count just adds the number to the running tally.

Actually, something is wrong with this whole thread. count(x) is
defined to take any type, hence count(b) won't convert anything to text
or anything else.

Which seems to imply that in the original query it's the '~' operator
that has the text conversion. Can you post an EXPLAIN VERBOSE for that
query so we can see where the conversion is being called.

Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Pollard, Mike 2005-11-16 13:52:51 Re: question about count(b) where b is a custom type
Previous Message Tino Wildenhain 2005-11-16 13:42:30 Re: question about count(b) where b is a custom type