Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0W65-0004K7-4E for pgsql-performance@arkaria.postgresql.org; Thu, 13 Sep 2018 18:12:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g0W63-0002Fn-3T for pgsql-performance@arkaria.postgresql.org; Thu, 13 Sep 2018 18:12:07 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0W62-0002Fg-Ow for pgsql-performance@lists.postgresql.org; Thu, 13 Sep 2018 18:12:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0W60-0001Gb-BK for pgsql-performance@lists.postgresql.org; Thu, 13 Sep 2018 18:12:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8DIC2h8013502; Thu, 13 Sep 2018 14:12:02 -0400 From: Tom Lane To: Fd Habash cc: "pgsql-performance@lists.postgresql.org" Subject: Re: Select count(*) on a 2B Rows Tables Takes ~20 Hours In-reply-to: <5b9a9f81.1c69fb81.43388.0e8b@mx.google.com> References: <5b9a9f81.1c69fb81.43388.0e8b@mx.google.com> Comments: In-reply-to Fd Habash message dated "Thu, 13 Sep 2018 13:33:54 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <13500.1536862322.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 13 Sep 2018 14:12:02 -0400 Message-ID: <13501.1536862322@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Fd Habash writes: > Based on my research in the forums and Google , it is described in multi= ple places that =E2=80=98select count(*)=E2=80=99 is expected to be slow i= n Postgres because of the MVCC controls imposed upon the query leading a t= able scan. Also, the elapsed time increase linearly with table size. = > However, I do not know if elapsed time I=E2=80=99m getting is to be expe= cted. = > Table reltuples in pg_class =3D 2,266,649,344 (pretty close) > Query =3D select count(*) from jim.sttyations ; > Elapsed time (ET) =3D 18.5 hrs That's pretty awful. My recollection is that in recent PG releases, SELECT COUNT(*) runs at something on the order of 100ns/row given an all-in-memory table. Evidently you're rather badly I/O bound. > This is an Aurora cluster running on r4.2xlarge (8 vCPU, 61g). Don't know much about Aurora, but I wonder whether you paid for guaranteed (provisioned) IOPS, and if so what service level. > refpep-> select count(*) from jim.sttyations; = > QUERY PLAN = = > ------------------------------------------------------------------------= ------------------------------------------ > Aggregate (cost=3D73451291.77..73451291.78 rows=3D1 width=3D8) > Output: count(*) > -> Index Only Scan using stty_indx_fk03 on jim.sttyations (cost=3D0= .58..67784668.41 rows=3D2266649344 width=3D0) > Output: vsr_number > (4 rows) Oh, hmm ... the 100ns figure I mentioned was for a seqscan. IOS could be a lot worse for a number of reasons, foremost being that if the table isn't mostly all-visible then it'd involve a lot of random heap access. It might be interesting to try forcing a seqscan plan (see enable_indexscan). regards, tom lane