Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kHhga-0006Sm-6X for pgsql-general@arkaria.postgresql.org; Mon, 14 Sep 2020 06:09:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kHhgV-0004UU-FI for pgsql-general@arkaria.postgresql.org; Mon, 14 Sep 2020 06:09:51 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kHhgV-0004UM-7r for pgsql-general@lists.postgresql.org; Mon, 14 Sep 2020 06:09:51 +0000 Received: from ms-10.1blu.de ([178.254.4.101]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kHhgS-00019H-MD for pgsql-general@lists.postgresql.org; Mon, 14 Sep 2020 06:09:50 +0000 Received: from [178.254.11.41] (helo=r314251-amd64) by ms-10.1blu.de with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kHhgQ-0007ep-Ip for pgsql-general@lists.postgresql.org; Mon, 14 Sep 2020 08:09:46 +0200 Date: Mon, 14 Sep 2020 08:09:44 +0200 From: Matthias Apitz To: pgsql-general@lists.postgresql.org Subject: Why SELECT COUNT(*) takes so long? Message-ID: <20200914060944.GA2050@r314251-amd64> Reply-To: Matthias Apitz MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Operating-System: FreeBSD 8.0-CURRENT (i386) User-Agent: Mutt/1.8.0 (2017-02-23) X-Con-Id: 51246 X-Con-U: 0-guru X-Originating-IP: 178.254.11.41 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, I've setup a relatively big database on RH 8.2 with PG 11.4. The table in question has aroung 38 millions of rows. Why a SELECT COUNT(*) of the full table takes around 1 minute: # date ; printf "select count(*) from d01buch ;\n" | /usr/local/sisis-pap/pgsql/bin/psql -Usisis -dsisis ; date Mo 14. Sep 07:48:36 CEST 2020 count ---------- 37982555 (1 Zeile) Mo 14. Sep 07:49:38 CEST 2020 while a SELECT using an indexed row does not take significant time: # date ; printf "select count(*) from d01buch where d01gsi='F-2014-30663189X' ; \n" | /usr/local/sisis-pap/pgsql/bin/psql -Usisis -dsisis ; date Mo 14. Sep 07:50:23 CEST 2020 count ------- 1 (1 Zeile) I now such longish SELECT COUNT(*) from Sybase when UPDATE STATISTICS is not done for the tables. Is there something similar for PostgreSQL? Thanks matthias -- Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045 Public GnuPG key: http://www.unixarea.de/key.pub May, 9: Спаси́бо освободители! Thank you very much, Russian liberators!