From: A. Kretschmer <andreas.kretschmer@schollglas.com>
To: pgsql-performance@postgresql.org
Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows
Date: Thu, 14 Jan 2010 16:18:14 +0100
Message-ID: <20100114151813.GH30196@a-kretschmer.de> (raw)
In-Reply-To: <1263481124.3307.59.camel@zitrone>
References: <1263481124.3307.59.camel@zitrone>
In response to tom :
> Hi,
>
> === Problem ===
>
> i have a db-table "data_measurand" with about 60000000 (60 Millions)
> rows and the following query takes about 20-30 seconds (with psql):
>
> mydb=# select count(*) from data_measurand;
> count
> ----------
> 60846187
> (1 row)
>
>
> === Question ===
>
> - What can i do to improve the performance for the data_measurand table?
Short answer: nothing.
Long answer: PG has to check the visibility for each record, so it
forces a seq.scan.
But you can get an estimation, ask pg_class (a system table), the column
reltuples there contains an estimated row rount.
http://www.postgresql.org/docs/current/static/catalog-pg-class.html
If you really needs the correct row-count you should create a TRIGGER
and count with this trigger all INSERTs and DELETEs.
Regards, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: andreas.kretschmer@schollglas.com
Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows
In-Reply-To: <20100114151813.GH30196@a-kretschmer.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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