Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BBF6063FC33 for ; Thu, 14 Jan 2010 11:18:41 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 81435-01-2 for ; Thu, 14 Jan 2010 15:18:22 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.17.9]) by mail.postgresql.org (Postfix) with ESMTP id 4D670633DA1 for ; Thu, 14 Jan 2010 11:18:22 -0400 (AST) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mrbap2) with ESMTP (Nemesis) id 0MdWxQ-1NF00D03x6-00PSqb; Thu, 14 Jan 2010 16:18:18 +0100 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1NVRSg-0001vj-00 for pgsql-performance@postgresql.org; Thu, 14 Jan 2010 16:18:14 +0100 Date: Thu, 14 Jan 2010 16:18:14 +0100 From: "A. Kretschmer" To: pgsql-performance@postgresql.org Subject: Re: Slow "Select count(*) ..." query on table with 60 Mio. rows Message-ID: <20100114151813.GH30196@a-kretschmer.de> References: <1263481124.3307.59.camel@zitrone> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1263481124.3307.59.camel@zitrone> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX19bt8BbrZnVWzlQm2OmyTQWukVHvYSdqquQV5q sQd548z5qB5hJiJoYEML8SogUb6EWF1M8BaCy+AzlQGPuYPhne kOypUGrG0IzrfD2CLY0nQ== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.107 tagged_above=-10 required=5 tests=AWL=0.492, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201001/280 X-Sequence-Number: 37160 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