pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrus <kobruleht2@hot.ee>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Performance degradation if query returns no rows and column expression is used after upgrading to 12
Date: Tue, 7 Apr 2020 19:47:03 +0300
Message-ID: <B8B5CD76F8D1442B91F4DA690CA32E06@dell2> (raw)

Hi!

Query returns no rows but its execution time in Postgres 12 depends on the column expression.

Query with  column expression

coalesce( (select sum(taitmata) from rid join dok using (dokumnr)
where toode=toode.toode and doktyyp='T' and not dok.taidetud and dok.kinnitatud and
taitmata is not null),0)

takes  666 ms :

explain analyze select
coalesce( (select sum(taitmata) from rid join dok using (dokumnr)
where toode=toode.toode and doktyyp='T' and not dok.taidetud and dok.kinnitatud and
taitmata is not null),0)
from toode
    where toode.ribakood='testmiin'::text
       or toode.nimetus ilike '%'||'testmiin'||'%' escape '!'
       or toode.toode ilike '%'||'testmiin'||'%' escape '!'
       or toode.markused ilike '%'||'testmiin'||'%' escape '!'
       or to_tsvector('english',toode.nimetus) @@ plainto_tsquery('testmiin')
         or to_tsvector('english',toode.engnimetus) @@  plainto_tsquery('testmiin')

"Gather  (cost=1000.00..505930.82 rows=153 width=32) (actual time=661.419..661.476 rows=0 loops=1)"
"  Workers Planned: 1"
"  Workers Launched: 1"
"  ->  Parallel Seq Scan on toode  (cost=0.00..10015.31 rows=90 width=21) (actual time=574.922..574.922 rows=0 loops=2)"
"        Filter: (((ribakood)::text = 'testmiin'::text) OR (nimetus ~~* '%testmiin%'::text) OR (toode ~~* '%testmiin%'::text) OR 
(markused ~~* '%testmiin%'::text) OR (to_tsvector('english'::regconfig, (nimetus)::text) @@ plainto_tsquery('testmiin'::text)) OR 
(to_tsvector('english'::regconfig, (engnimetus)::text) @@ plainto_tsquery('testmiin'::text)))"
"        Rows Removed by Filter: 7202"
"  SubPlan 1"
"    ->  Aggregate  (cost=3234.63..3234.64 rows=1 width=32) (never executed)"
"          ->  Nested Loop  (cost=11.26..3234.52 rows=43 width=3) (never executed)"
"                ->  Bitmap Heap Scan on rid  (cost=10.84..1191.72 rows=270 width=7) (never executed)"
"                      Recheck Cond: (toode = toode.toode)"
"                      Filter: (taitmata IS NOT NULL)"
"                      ->  Bitmap Index Scan on rid_toode_pattern_idx  (cost=0.00..10.77 rows=312 width=0) (never executed)"
"                            Index Cond: (toode = toode.toode)"
"                ->  Index Scan using dok_pkey on dok  (cost=0.42..7.57 rows=1 width=4) (never executed)"
"                      Index Cond: (dokumnr = rid.dokumnr)"
"                      Filter: ((NOT taidetud) AND kinnitatud AND (doktyyp = 'T'::bpchar))"
"Planning Time: 2.102 ms"
"JIT:"
"  Functions: 24"
"  Options: Inlining true, Optimization true, Expressions true, Deforming true"
"  Timing: Generation 5.592 ms, Inlining 95.077 ms, Optimization 463.742 ms, Emission 277.062 ms, Total 841.473 ms"
"Execution Time: 666.007 ms"


Same query with sime column expression

1

run 3.6 times faster:


explain analyze select 1
from toode
    where toode.ribakood='testmiin'::text
       or toode.nimetus ilike '%'||'testmiin'||'%' escape '!'
       or toode.toode ilike '%'||'testmiin'||'%' escape '!'
       or toode.markused ilike '%'||'testmiin'||'%' escape '!'
       or to_tsvector('english',toode.nimetus) @@ plainto_tsquery('testmiin')
         or to_tsvector('english',toode.engnimetus) @@
         plainto_tsquery('testmiin')

"Gather  (cost=1000.00..11030.61 rows=153 width=4) (actual time=182.414..185.648 rows=0 loops=1)"
"  Workers Planned: 1"
"  Workers Launched: 1"
"  ->  Parallel Seq Scan on toode  (cost=0.00..10015.31 rows=90 width=4) (actual time=155.338..155.339 rows=0 loops=2)"
"        Filter: (((ribakood)::text = 'testmiin'::text) OR (nimetus ~~* '%testmiin%'::text) OR (toode ~~* '%testmiin%'::text) OR 
(markused ~~* '%testmiin%'::text) OR (to_tsvector('english'::regconfig, (nimetus)::text) @@ plainto_tsquery('testmiin'::text)) OR 
(to_tsvector('english'::regconfig, (engnimetus)::text) @@ plainto_tsquery('testmiin'::text)))"
"        Rows Removed by Filter: 7202"
"Planning Time: 1.729 ms"
"Execution Time: 185.674 ms"

If there are more column expressions, perfomance difference is bigger.
rid  table used in column expression contains 1.8 million of rows.
Performance degradation probably occured if upgraded from Postgres 9.1 to Postgres 12

Since no data is returned query perfomance should be same.
How to fix it ?

Andrus. 





view thread (8+ messages)  latest in thread

Message-ID: <B8B5CD76F8D1442B91F4DA690CA32E06@dell2>
Permalink:  ../B8B5CD76F8D1442B91F4DA690CA32E06@dell2/
Also on:    postgresql.org/message-id/B8B5CD76F8D1442B91F4DA690CA32E06@dell2

 · 

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: kobruleht2@hot.ee
  Subject: Re: Performance degradation if query returns no rows and column expression is used after upgrading to 12
  In-Reply-To: <B8B5CD76F8D1442B91F4DA690CA32E06@dell2>
  References: <B8B5CD76F8D1442B91F4DA690CA32E06@dell2>

* 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