pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
select count(*) from a large table is very slow
6+ messages / 4 participants
[nested] [flat]

* select count(*) from a large table is very slow
@ 2007-05-18 13:15  Jessica Richard <rjessil@yahoo.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Jessica Richard @ 2007-05-18 13:15 UTC (permalink / raw)
  To: pgsql-admin

I have a ~30 milliow row table, select count(*) from this table is very very very slow...

How dow I get it run faster?

Thanks,
Jessica

 
---------------------------------
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: select count(*) from a large table is very slow
@ 2007-05-18 13:19  Kenneth Marshall <ktm@rice.edu>
  parent: Jessica Richard <rjessil@yahoo.com>
  1 sibling, 1 reply; 6+ messages in thread

From: Kenneth Marshall @ 2007-05-18 13:19 UTC (permalink / raw)
  To: Jessica Richard <rjessil@yahoo.com>; +Cc: pgsql-admin

Make a summary table and select from there instead.

Ken

On Fri, May 18, 2007 at 06:15:25AM -0700, Jessica Richard wrote:
> I have a ~30 milliow row table, select count(*) from this table is very very very slow...
> 
> How dow I get it run faster?
> 
> Thanks,
> Jessica
> 
>  
> ---------------------------------
> We won't tell. Get more on shows you hate to love
> (and love to hate): Yahoo! TV's Guilty Pleasures list.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: select count(*) from a large table is very slow
@ 2007-05-18 13:22  Jonah H. Harris <jonah.harris@gmail.com>
  parent: Jessica Richard <rjessil@yahoo.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Jonah H. Harris @ 2007-05-18 13:22 UTC (permalink / raw)
  To: Jessica Richard <rjessil@yahoo.com>; +Cc: pgsql-admin

On 5/18/07, Jessica Richard <rjessil@yahoo.com> wrote:
> I have a ~30 milliow row table, select count(*) from this table is very very
> very slow...

Yes, see the topic "Much Ado about COUNT(*)"

> How dow I get it run faster?

Unfortunately, aside from reducing the size of the table, you can't.

-- 
Jonah H. Harris, Software Architect | phone: 732.331.1324
EnterpriseDB Corporation            | fax: 732.331.1301
33 Wood Ave S, 3rd Floor            | jharris@enterprisedb.com
Iselin, New Jersey 08830            | http://www.enterprisedb.com/



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: select count(*) from a large table is very slow
@ 2007-05-18 13:30  Jonah H. Harris <jonah.harris@gmail.com>
  parent: Kenneth Marshall <ktm@rice.edu>
  0 siblings, 1 reply; 6+ messages in thread

From: Jonah H. Harris @ 2007-05-18 13:30 UTC (permalink / raw)
  To: Kenneth Marshall <ktm@rice.edu>; +Cc: Jessica Richard <rjessil@yahoo.com>; pgsql-admin

On 5/18/07, Kenneth Marshall <ktm@rice.edu> wrote:
> Make a summary table and select from there instead.

Yes, a summary table updated by triggers is the only way to have
correct numbers... but you also pay the overhead of maintaining those
numbers on every INSERT/UPDATE statement.  If you just need
*reasonable* guesstimates, use reltuples in pg_class after an analyze.

-- 
Jonah H. Harris, Software Architect | phone: 732.331.1324
EnterpriseDB Corporation            | fax: 732.331.1301
33 Wood Ave S, 3rd Floor            | jharris@enterprisedb.com
Iselin, New Jersey 08830            | http://www.enterprisedb.com/



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: select count(*) from a large table is very slow
@ 2007-05-18 13:58  Daniel Cristian Cruz <danielcristian@gmail.com>
  parent: Jonah H. Harris <jonah.harris@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Daniel Cristian Cruz @ 2007-05-18 13:58 UTC (permalink / raw)
  To: Jonah H. Harris <jonah.harris@gmail.com>; +Cc: Kenneth Marshall <ktm@rice.edu>; Jessica Richard <rjessil@yahoo.com>; pgsql-admin

2007/5/18, Jonah H. Harris <jonah.harris@gmail.com>:
> Yes, a summary table updated by triggers is the only way to have
> correct numbers... but you also pay the overhead of maintaining those
> numbers on every INSERT/UPDATE statement.  If you just need
> *reasonable* guesstimates, use reltuples in pg_class after an analyze.

Oops... INSERT or DELETE... :)

And remember that if using TRUNCATE, it will not run your trigger and
you will need to reset the counter manually.

Regards...
-- 
Daniel Cristian Cruz



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: select count(*) from a large table is very slow
@ 2007-05-18 14:00  Jonah H. Harris <jonah.harris@gmail.com>
  parent: Daniel Cristian Cruz <danielcristian@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Jonah H. Harris @ 2007-05-18 14:00 UTC (permalink / raw)
  To: Daniel Cristian Cruz <danielcristian@gmail.com>; +Cc: Kenneth Marshall <ktm@rice.edu>; Jessica Richard <rjessil@yahoo.com>; pgsql-admin

On 5/18/07, Daniel Cristian Cruz <danielcristian@gmail.com> wrote:
> Oops... INSERT or DELETE... :)

Heh, yeah... doh!

> And remember that if using TRUNCATE, it will not run your trigger and
> you will need to reset the counter manually.

Correct.

-- 
Jonah H. Harris, Software Architect | phone: 732.331.1324
EnterpriseDB Corporation            | fax: 732.331.1301
33 Wood Ave S, 3rd Floor            | jharris@enterprisedb.com
Iselin, New Jersey 08830            | http://www.enterprisedb.com/



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2007-05-18 14:00 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-05-18 13:15 select count(*) from a large table is very slow Jessica Richard <rjessil@yahoo.com>
2007-05-18 13:19 ` Kenneth Marshall <ktm@rice.edu>
2007-05-18 13:30   ` Jonah H. Harris <jonah.harris@gmail.com>
2007-05-18 13:58     ` Daniel Cristian Cruz <danielcristian@gmail.com>
2007-05-18 14:00       ` Jonah H. Harris <jonah.harris@gmail.com>
2007-05-18 13:22 ` Jonah H. Harris <jonah.harris@gmail.com>

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