Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qMQEU-002MYF-Nx for pgsql-general@arkaria.postgresql.org; Thu, 20 Jul 2023 09:46:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qMQDU-004ASZ-TD for pgsql-general@arkaria.postgresql.org; Thu, 20 Jul 2023 09:45:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qMQDU-004ASQ-Hx for pgsql-general@lists.postgresql.org; Thu, 20 Jul 2023 09:45:00 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qMQDO-000Tcf-K5 for pgsql-general@lists.postgresql.org; Thu, 20 Jul 2023 09:44:59 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 9934689D53; Thu, 20 Jul 2023 11:44:51 +0200 (CEST) Date: Thu, 20 Jul 2023 11:44:51 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: How to improve the performance of my SQL =?utf-8?Q?query?= =?utf-8?B?77yf?= Message-ID: <20230720094451.m4hdv4hdhmjbjw53@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: <6212ea1b.425b.1897221c3cc.Coremail.gzhcoder@126.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="jz6dp2kqgq7tn2aw" Content-Disposition: inline In-Reply-To: <6212ea1b.425b.1897221c3cc.Coremail.gzhcoder@126.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --jz6dp2kqgq7tn2aw Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2023-07-20 15:09:22 +0800, gzh wrote: > Hi everyone, >=20 >=20 > I'm running into some performance issues with my SQL query. >=20 > The following SQL query is taking a long time to execute. >=20 >=20 > explain analyze > select COUNT(ET_CD) > from TBL_SHA > WHERE TBL_SHA.MS_CD =3D '009' > and TBL_SHA.ETRYS in > (select TBL_INF.RY_CD > from TBL_INF > WHERE TBL_INF.MS_CD =3D '009' > AND TBL_INF.RY_CD =3D '000001' > ) >=20 > ----- Execution Plan ----- [...] > -> Seq Scan on TBL_SHA (cost=3D0.00..2698666.58 rows=3D32= 02774 width=3D18) > (actual time=3D97264.138..123554.7= 92 rows=3D3200000 loops=3D1) > Filter: ((MS_CD =3D '009'::bpchar) AND (ETRYS =3D '00= 0001'::bpchar)) > Rows Removed by Filter: 32000325 [...] > -------------------------------------------------------------------------= ------- >=20 >=20 > The index is defined as follows. >=20 >=20 > CREATE INDEX index_search_01 ON mdb.TBL_SHA USING btree (MS_CD, ETRYS); >=20 >=20 >=20 > When I take the following sql statement, the index works fine and the que= ry is > fast. >=20 >=20 > select COUNT(ET_CD) > from TBL_SHA > WHERE MS_CD =3D '009' > AND ETRYS =3D '000001' What's the plan for that query? hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --jz6dp2kqgq7tn2aw Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmS5Ag4ACgkQ8g5IURL+ KF1AWhAAl8JqFLg/TXuZyU5ilpr02y+NaTR6nTM0zvWwmgseaPTGaFCkgaQoGBGH Wq2/m7CO758iM3lDp+eHepHAJv9AR7WKZTIIYmqroruLkAVyQFL1gw6w+wP5bdj7 yXaKrIyO5l4g4lLNxjo5F3R0sOewR8g+X53HWiR69GDqxx6oUwSqGzuKlr2iMwwG KYxWgUlexHRvoAHdoL/Z4rRRX4SHOu5A4inRVUAE5eDOvbfMoRpJdjM8txhroodq TT+dgYZuEVcgj7C6wBdMuVt9J2gGD+SW44U42A2wtTaukMQ+8kAixRrPMyecXuCF IjlkkGO/WcYl2qX7zbLfUkEkdSaDfHWxOBh0c40+/5vyN/n5+TWa+JPq314fkFeI r3hbypK9M8vOB64+3gElK9Z/bpMrNtM12OU21GsAPVbwkm5dT+Sax0tAV1t5Kwpd Z07nYfIZCRW+aDEX9KlSWEtxmY5eH2HCtQqrxYDO9BwxLQxO0EaGC+923VCy9LNz R0gJwJb+ZefgtPxLRA9rSAU0z49t+8COt/WtLRkckzo69D1T26HewuqHcUZWHvev d/CRpps1FTaibFh3ejOS8zpKCpa6JIcQ+667RP2abTYf7AU15WPv1J4JhktyKgr1 /WPksSjkkugRWmwX2aZmYVRIkMto0/oPcvY/GWUTaqnFGWfGt7g= =pEl7 -----END PGP SIGNATURE----- --jz6dp2kqgq7tn2aw--