Re: Slow count(*) again...

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: bricklen <bricklen(at)gmail(dot)com>
Cc: Neil Whelchel <neil(dot)whelchel(at)gmail(dot)com>, pgsql-performance(at)postgresql(dot)org
Subject: Re: Slow count(*) again...
Date: 2010-10-21 04:07:22
Message-ID: 201010210407.o9L47M912017@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

bricklen wrote:
> On Sat, Oct 9, 2010 at 4:26 PM, Neil Whelchel <neil(dot)whelchel(at)gmail(dot)com> wrote:
> > Maybe an
> > estimate(*) that works like count but gives an answer from the index without
> > checking visibility? I am sure that this would be good enough to make a page
> > list, it is really no big deal if it errors on the positive side, maybe the
> > list of pages has an extra page off the end. I can live with that. What I
> > can't live with is taking 13 seconds to get a page of results from 850,000
> > rows in a table.
> > -Neil-
> >
>
> FWIW, Michael Fuhr wrote a small function to parse the EXPLAIN plan a
> few years ago and it works pretty well assuming your stats are up to
> date.
>
> http://markmail.org/message/gknqthlwry2eoqey

What I recommend is to execute the query with EXPLAIN, and look at the
estimated rows and costs. If the row number is large, just round it to
the nearest thousand and return it to the application as a count ---
this is what Google does for searches (just try it).

If the row count/cost are low, run the query and return an exact count.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Scott Carey 2010-10-21 04:47:24 Re: Slow count(*) again...
Previous Message Greg Stark 2010-10-21 04:03:31 Re: default_statistics_target WAS: max_wal_senders must die

Browse pgsql-performance by date

  From Date Subject
Next Message Scott Marlowe 2010-10-21 04:45:08 Re: BBU Cache vs. spindles
Previous Message Greg Smith 2010-10-21 02:39:32 Re: What is postmaster doing?