Re: Multiple counts on criteria - Approach to a problem

From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Neil Saunders <n(dot)j(dot)saunders(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Multiple counts on criteria - Approach to a problem
Date: 2009-09-17 16:44:17
Message-ID: 20090917124417.34f547e2.wmoran@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Neil Saunders <n(dot)j(dot)saunders(at)gmail(dot)com>:

> Hi all,
>
> I maintain an online property rental application. The main focus of the UI
> is the search engine, which I'd now like to improve by allowing filtering of
> the search results shown on some criteria, but provide a count of the number
> of properties that meet that criteria.
>
> For example, we're looking all properties, no criteria. I'd like to show
> something like:
>
> Bedrooms:
> 1 Bedroom (122)
> 2 Bedrooms (143)
> 3 Bedrooms (88)
>
> Facilities
> BBQ (232)
> Pool (122)
>
> ...and so on. My question is simple - What's the best way to implement this
> - Do I literally have to execute a count for the WHERE criteria with the
> filter criteria tagged on, or is there some clever trick that I'm not aware
> of? I'd rather not count in the application as I'd like to plan for the day
> we have up to 100k properties (
>
> Any suggestions gratefully received!

In addition to the other excellent suggestions, you can provide estimates
at a very small cost by using explain:

EXPLAIN SELECT count(*) FROM properties WHERE bedrooms=3;

Then parse the explain output to get the estimated # of rows. Very low
overhead, but of course it's only an estimate.

--
Bill Moran
http://www.potentialtech.com
http://people.collaborativefusion.com/~wmoran/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martin Gainty 2009-09-17 16:56:06 'Weird' errors
Previous Message hubert depesz lubaczewski 2009-09-17 16:40:08 creation of foreign key without checking prior data?