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 1qNoMP-0061dD-FQ for pgsql-general@arkaria.postgresql.org; Mon, 24 Jul 2023 05:43:57 +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 1qNoMN-007vGK-NC for pgsql-general@arkaria.postgresql.org; Mon, 24 Jul 2023 05:43:55 +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 1qNoMN-007vFk-86 for pgsql-general@lists.postgresql.org; Mon, 24 Jul 2023 05:43:55 +0000 Received: from mail-lj1-x242.google.com ([2a00:1450:4864:20::242]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qNoMI-0019Gq-39 for pgsql-general@lists.postgresql.org; Mon, 24 Jul 2023 05:43:53 +0000 Received: by mail-lj1-x242.google.com with SMTP id 38308e7fff4ca-2b93fba1f62so54743831fa.1 for ; Sun, 23 Jul 2023 22:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1690177427; x=1690782227; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=eK11kOdVORKlbWYal+UJnVOiBfk7KuAjK6vVlRd2MYc=; b=IJd9r41j+hYWdugeNB05qEm7nPEXraxMQ3GizQ3GFhuh44dvbCLEE+yQ8545bFGCSY KWSMwxrakNPC8fZHzh7yGF/GV2247XvPWT7GxikDRMSfDvV99s4l1nEM+oKehmI9u+4N r3oL489xW8P/gDgD3GYXAJUHzRZ8oVLGJDouXr/oGeCP1t8t1UW7a6pEqaMGOg2cNxdT ja/2yDfxjt0OxcwAEQMnUOt7f+7VlnT8F36+tkxHNv6BZfl+kYW9zUqQb1iGRgtSTZOp Yvcs2U9UygEscld+LQYwp9fbMAfjiWbGEhlfp0WmUy+InivILaTQJAUM6eLIiqn7YGik Xd/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690177427; x=1690782227; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=eK11kOdVORKlbWYal+UJnVOiBfk7KuAjK6vVlRd2MYc=; b=lF7Cuk1DD6FtcP4qlefMLVNexpf25gyAAWyNVUOiQyjwBSDqXObOSz4jZCL46d7j33 5T7k6Xl0KQWmonwCfSsVQpe2yYdueOTSuMbAIhvnNphTSpDlQlJoz3No7DmVqxzPR+08 8fnclHQzstb/dD/vNhgZSdOkAZhfjsJ33DQDsxF7y9LyC06MJsfAdcHMU7zUYbx/u/+s KnMSxjd+T+xZ71uJOlrpSqytNnWLeKjyr+9KoYUiJjj1+cYbRKxyjWhRPDRqDZx4N1YN ZkSagRar2DfIBrCmJ2XPp5kQthBW98xHCaD0KyvvnvUidTGxB7M5JtBFFYYyfGITJLIO iaKw== X-Gm-Message-State: ABy/qLZUgKthw7MOfNIEnkShGs+L0HqrV/C8RMk+Z2SqEnoXX+5QYH9w r5UXQdI7bw23EPoMyCdu2vay8w== X-Google-Smtp-Source: APBJJlGF1StKrunOZ5kWe4aexMJV/Roz2DpD5HSZkk8UArsdprMzp1c7T71zPzBGYcvIVi4X3KyAeQ== X-Received: by 2002:a05:651c:237:b0:2b9:452f:41c3 with SMTP id z23-20020a05651c023700b002b9452f41c3mr5153586ljn.46.1690177427363; Sun, 23 Jul 2023 22:43:47 -0700 (PDT) Received: from localhost.localdomain ([41.66.96.65]) by smtp.gmail.com with ESMTPSA id f12-20020a7bcc0c000000b003fc0062f0f8sm9400445wmh.9.2023.07.23.22.43.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 22:43:47 -0700 (PDT) Message-ID: <4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at> Subject: Re: How to improve the performance of my SQL =?UTF-8?Q?query=EF=BC=9F?= From: Laurenz Albe To: gzh Cc: pgsql-general@lists.postgresql.org Date: Mon, 24 Jul 2023 07:43:46 +0200 In-Reply-To: <6f3fd2f9.12b3.189761e1ccc.Coremail.gzhcoder@126.com> References: <6212ea1b.425b.1897221c3cc.Coremail.gzhcoder@126.com> <369ee851ef59bc84190f1631219b5a0c8f7907e9.camel@cybertec.at> <1937d056.71e9.18973a752d0.Coremail.gzhcoder@126.com> <6f3fd2f9.12b3.189761e1ccc.Coremail.gzhcoder@126.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2023-07-21 at 09:43 +0800, gzh wrote: > The definitions of the columns used in SQL are as follows. >=20 > TBL_SHA >=20 > ms_cd character(6) NOT NULL=C2=A0 =C2=A0 =C2=A0 =C2=A0-- PRIMARY KEY > et_cd character(8) > etrys character(8) >=20 > TBL_INF >=20 > ms_cd character(6) NOT NULL=C2=A0 =C2=A0 =C2=A0 =C2=A0-- PRIMARY KEY > ry_cd character(8) NOT NULL=C2=A0 =C2=A0 =C2=A0 =C2=A0-- PRIMARY KEY >=20 > I made some modifications to the data, and I realized that I should not c= hange the length of the data.=C2=A0 > The actual data and its corresponding execution plan are shown below. >=20 > explain analyze > select COUNT(ET_CD) > from TBL_SHA > WHERE TBL_SHA.MS_CD =3D 'MLD009' > and TBL_SHA.ETRYS in > =C2=A0 =C2=A0(select TBL_INF.RY_CD > =C2=A0 =C2=A0 from TBL_INF > =C2=A0 =C2=A0 WHERE TBL_INF.MS_CD =3D 'MLD009' > =C2=A0 =C2=A0AND TBL_INF.RY_CD =3D '00000001' > =C2=A0 =C2=A0) > ----- Execution Plan ----- > Limit=C2=A0 (cost=3D2738709.57..2738709.58 rows=3D1 width=3D8) (actual ti= me=3D124168.769..124168.771 rows=3D1 loops=3D1) > =C2=A0 ->=C2=A0 Aggregate=C2=A0 (cost=3D2738709.57..2738709.58 rows=3D1 w= idth=3D8) (actual time=3D124168.767..124168.769 rows=3D1 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Nested Loop=C2=A0 (cost=3D0.29..2730= 702.63 rows=3D3202774 width=3D9) (actual time=3D97264.166..123920.769 rows= =3D3200000 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Index Only Scan= using TBL_INF_pkc on TBL_INF=C2=A0 (cost=3D0.29..8.31 rows=3D1 width=3D9) = (actual time=3D0.025..0.030 rows=3D1 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Ind= ex Cond: ((MS_CD =3D 'MLD009'::bpchar) AND (RY_CD =3D '00000001'::bpchar)) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Hea= p Fetches: 1 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ->=C2=A0 Seq Scan on TBL= _SHA=C2=A0 (cost=3D0.00..2698666.58 rows=3D3202774 width=3D18) (actual time= =3D97264.138..123554.792 rows=3D3200000 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Fil= ter: ((MS_CD =3D 'MLD009'::bpchar) AND (ETRYS =3D '00000001'::bpchar)) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Row= s Removed by Filter: 32000325 > Planning Time: 0.162 ms > Execution Time: 124168.838 ms Thanks. That should definitely use a b-tree index defined on (ms_cd, etrsy= s). Did you change any parameters that have an impact on query planning? You can see that in the output of EXPLAIN (ANALYZE, BUFFERS, SETTINGS). Yours, Laurenz Albe