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 1oBKZq-0000f6-65 for pgsql-admin@arkaria.postgresql.org; Tue, 12 Jul 2022 18:25:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oBKZo-00005Y-Ex for pgsql-admin@arkaria.postgresql.org; Tue, 12 Jul 2022 18:25:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBKZn-0008Sf-Vp for pgsql-admin@lists.postgresql.org; Tue, 12 Jul 2022 18:25:40 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oBKZl-0004Bp-6G for pgsql-admin@lists.postgresql.org; Tue, 12 Jul 2022 18:25:38 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id F1A4E380976; Tue, 12 Jul 2022 20:25:33 +0200 (CEST) Received: from rs.plausibolo.de ([IPv6:::1]) by localhost (h2367442.stratoserver.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id wYg6brelsly0; Tue, 12 Jul 2022 20:25:33 +0200 (CEST) Received: from [192.168.192.86] (dynamic-046-114-106-203.46.114.pool.telefonica.de [46.114.106.203]) by rs.plausibolo.de (Postfix) with ESMTPSA id 7BB9D380152; Tue, 12 Jul 2022 20:25:33 +0200 (CEST) Message-ID: <6e9caa48-5e29-a493-d54d-3a97f3893029@jakobs.com> Date: Tue, 12 Jul 2022 20:25:32 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Content-Language: de-DE To: pgsql-admin@lists.postgresql.org References: <1B1927BA-F7DE-40E8-8F67-BCC001CD4C6B@gmail.com> <1eb233ff-90f0-529b-f0cc-fa982f668a8f@gmail.com> <1221536526.20277.1657636277921@email.ionos.com> From: Holger Jakobs Subject: Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------oIK0ZBIBH2bwRM0wO4y8vXCT" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --------------oIK0ZBIBH2bwRM0wO4y8vXCT Content-Type: multipart/mixed; boundary="------------ImPBuIF0KUGBpeDrMdPKqUcR"; protected-headers="v1" From: Holger Jakobs To: pgsql-admin@lists.postgresql.org Message-ID: <6e9caa48-5e29-a493-d54d-3a97f3893029@jakobs.com> Subject: Re: [EXT] Re: Improve "select count(*)" query - takes more than 30 mins for some large tables References: <1B1927BA-F7DE-40E8-8F67-BCC001CD4C6B@gmail.com> <1eb233ff-90f0-529b-f0cc-fa982f668a8f@gmail.com> <1221536526.20277.1657636277921@email.ionos.com> In-Reply-To: --------------ImPBuIF0KUGBpeDrMdPKqUcR Content-Type: multipart/alternative; boundary="------------2YJn32lozo5Ky7DA7ZYXCQsJ" --------------2YJn32lozo5Ky7DA7ZYXCQsJ Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: base64 QW0gMTIuMDcuMjIgdW0gMjA6MTMgc2NocmllYiBQaWVyc29uIFBhdHJpY2lhIEwgKENvbnRy YWN0b3IpOg0KPg0KPiBIZWxsbywNCj4NCj4gRG8gYSBjb3VudCBvbiB0aGUgcHJpbWFyeSBr ZXkuwqAgV2lsbCBmb3JjZSBpbmRleCBhY2Nlc3MgYW5kIHlvdSBkb27igJl0IA0KPiBhY2Nl c3MgdGhlIGVudGlyZSByb3cgd2h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Wxl bikNCg0KZGI9IyBleHBsYWluIHNlbGVjdCBjb3VudCgqKSBmcm9tIGF1ZnRyYWc7DQogwqDC oMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDC oMKgwqDCoMKgwqDCoMKgwqDCoMKgIFFVRVJZIFBMQU4NCi0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0t LS0tLS0tLS0tLS0tLS0tLQ0KIMKgRmluYWxpemUgQWdncmVnYXRlwqAgKGNvc3Q9MTE2MTQu NTUuLjExNjE0LjU2IHJvd3M9MSB3aWR0aD04KQ0KIMKgwqAgLT7CoCBHYXRoZXLCoCAoY29z dD0xMTYxNC4zMy4uMTE2MTQuNTQgcm93cz0yIHdpZHRoPTgpDQogwqDCoMKgwqDCoMKgwqDC oCBXb3JrZXJzIFBsYW5uZWQ6IDINCiDCoMKgwqDCoMKgwqDCoMKgIC0+wqAgUGFydGlhbCBB Z2dyZWdhdGXCoCAoY29zdD0xMDYxNC4zMy4uMTA2MTQuMzQgcm93cz0xIHdpZHRoPTgpDQog wqDCoMKgwqDCoMKgwqDCoMKgwqDCoMKgwqDCoCAtPsKgIFBhcmFsbGVsIFNlcSBTY2FuIG9u IGF1ZnRyYWcgKGNvc3Q9MC4wMC4uOTU3Mi42NyANCnJvd3M9NDE2NjY3IHdpZHRoPTApDQoo NSBaZWlsZW4pDQoNCi0tIA0KSG9sZ2VyIEpha29icywgQmVyZ2lzY2ggR2xhZGJhY2gsIFRl bC4gKzQ5LTE3OC05NzU5MDEyDQoNCg== --------------2YJn32lozo5Ky7DA7ZYXCQsJ Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Am 12.07.22 um 20:13 schrieb Pierson Patricia L (Contractor):

Hello,

=C2=A0

Do a count on the primary key.=C2=A0 Will force index access and= you don=E2=80=99t access the entire row which may be very long.

LIKE : select count(ID) from my_table;


            

If you do a COUNT(*) the database won't do anything different. That's a myth.

Just compare the execution plans.


db=3D# explain select count(anr) from auftrag;
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 QUERY PLAN=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 =C2=A0
-------------------------------------------------------------------------= -----------------
=C2=A0Finalize Aggregate=C2=A0 (cost=3D11614.55..11614.56 rows=3D1 = width=3D8)
=C2=A0=C2=A0 ->=C2=A0 Gather=C2=A0 (cost=3D11614.33..11614.54 ro= ws=3D2 width=3D8)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Workers Planned: 2=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ->=C2=A0 Partia= l Aggregate=C2=A0 (cost=3D10614.33..10614.34 rows=3D1 width=3D8)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 ->=C2=A0 Parallel Seq Scan on auftrag=C2=A0 (cost=3D0.00..9572.67 rows=3D416667 width=3D8)
(5 Zeilen)

db=3D# explain select count(*) from auftrag;
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 QUERY PLAN=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 =C2=A0
-------------------------------------------------------------------------= -----------------
=C2=A0Finalize Aggregate=C2=A0 (cost=3D11614.55..11614.56 rows=3D1 = width=3D8)
=C2=A0=C2=A0 ->=C2=A0 Gather=C2=A0 (cost=3D11614.33..11614.54 ro= ws=3D2 width=3D8)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Workers Planned: 2=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ->=C2=A0 Partia= l Aggregate=C2=A0 (cost=3D10614.33..10614.34 rows=3D1 width=3D8)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 ->=C2=A0 Parallel Seq Scan on auftrag=C2=A0 (cost=3D0.00..9572.67 rows=3D416667 width=3D0)
(5 Zeilen)

--=20
Holger Jakobs, Bergisch Gladbach, Tel. +49-178-9759012
--------------2YJn32lozo5Ky7DA7ZYXCQsJ-- --------------ImPBuIF0KUGBpeDrMdPKqUcR-- --------------oIK0ZBIBH2bwRM0wO4y8vXCT Content-Type: application/pgp-signature; name="OpenPGP_signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="OpenPGP_signature" -----BEGIN PGP SIGNATURE----- wmMEABEIACMWIQScuJCxWJK1p5BOebGKV5yTsxlhugUCYs28nAUDAAAAAAAKCRCKV5yTsxlhulzi AJ40/wS2U9jHI0/OB0Td3d8JFJ9T7wCg2/PDUxDaOwxSsZZ/9Xt7aYdhGLY= =vmag -----END PGP SIGNATURE----- --------------oIK0ZBIBH2bwRM0wO4y8vXCT--