pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Laurenz Albe <laurenz.albe@cybertec.at>
To: gzh <gzhcoder@126.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: How to improve the performance of my SQL query?
Date: Mon, 24 Jul 2023 07:43:46 +0200
Message-ID: <4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at> (raw)
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>
	<ea3893a955f4ae396efb05260bfab056f027fcf9.camel@cybertec.at>
	<6f3fd2f9.12b3.189761e1ccc.Coremail.gzhcoder@126.com>

On Fri, 2023-07-21 at 09:43 +0800, gzh wrote:
> The definitions of the columns used in SQL are as follows.
> 
> TBL_SHA
> 
> ms_cd character(6) NOT NULL       -- PRIMARY KEY
> et_cd character(8)
> etrys character(8)
> 
> TBL_INF
> 
> ms_cd character(6) NOT NULL       -- PRIMARY KEY
> ry_cd character(8) NOT NULL       -- PRIMARY KEY
> 
> I made some modifications to the data, and I realized that I should not change the length of the data. 
> The actual data and its corresponding execution plan are shown below.
> 
> explain analyze
> select COUNT(ET_CD)
> from TBL_SHA
> WHERE TBL_SHA.MS_CD = 'MLD009'
> and TBL_SHA.ETRYS in
>    (select TBL_INF.RY_CD
>     from TBL_INF
>     WHERE TBL_INF.MS_CD = 'MLD009'
>    AND TBL_INF.RY_CD = '00000001'
>    )
> ----- Execution Plan -----
> Limit  (cost=2738709.57..2738709.58 rows=1 width=8) (actual time=124168.769..124168.771 rows=1 loops=1)
>   ->  Aggregate  (cost=2738709.57..2738709.58 rows=1 width=8) (actual time=124168.767..124168.769 rows=1 loops=1)
>         ->  Nested Loop  (cost=0.29..2730702.63 rows=3202774 width=9) (actual time=97264.166..123920.769 rows=3200000 loops=1)
>               ->  Index Only Scan using TBL_INF_pkc on TBL_INF  (cost=0.29..8.31 rows=1 width=9) (actual time=0.025..0.030 rows=1 loops=1)
>                     Index Cond: ((MS_CD = 'MLD009'::bpchar) AND (RY_CD = '00000001'::bpchar))
>                     Heap Fetches: 1
>               ->  Seq Scan on TBL_SHA  (cost=0.00..2698666.58 rows=3202774 width=18) (actual time=97264.138..123554.792 rows=3200000 loops=1)
>                     Filter: ((MS_CD = 'MLD009'::bpchar) AND (ETRYS = '00000001'::bpchar))
>                     Rows 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, etrsys).

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





view thread (17+ messages)  latest in thread

Message-ID: <4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at>
Permalink:  ../4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at/
Also on:    postgresql.org/message-id/4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: laurenz.albe@cybertec.at, gzhcoder@126.com
  Subject: Re: How to improve the performance of my SQL query?
  In-Reply-To: <4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at>
  References: <6212ea1b.425b.1897221c3cc.Coremail.gzhcoder@126.com> <369ee851ef59bc84190f1631219b5a0c8f7907e9.camel@cybertec.at> <1937d056.71e9.18973a752d0.Coremail.gzhcoder@126.com> <ea3893a955f4ae396efb05260bfab056f027fcf9.camel@cybertec.at> <6f3fd2f9.12b3.189761e1ccc.Coremail.gzhcoder@126.com> <4ae89a642b8ee57b8fdcd2f6e106aadc1eb60a4b.camel@cybertec.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox