pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedCTE with JOIN of two tables is much faster than a regular query
5+ messages / 5 participants
[nested] [flat]
* CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-18 09:36 kpi6288@gmail.com
2018-08-18 10:26 ` Re: CTE with JOIN of two tables is much faster than a regular query Andreas Kretschmer <andreas@a-kretschmer.de>
2018-08-18 14:38 ` Re: CTE with JOIN of two tables is much faster than a regular query Stephen Frost <sfrost@snowman.net>
2018-08-18 16:25 ` Re: CTE with JOIN of two tables is much faster than a regular query Ravi Krishna <sravikrishna@aol.com>
0 siblings, 3 replies; 5+ messages in thread
From: kpi6288@gmail.com @ 2018-08-18 09:36 UTC (permalink / raw)
To: pgsql-general
Running PostgreSQL 9.5 on Windows.
The CTE mentioned below completes the query in 4.5 seconds while the regular
query takes 66 seconds. I read from EXPLAIN ANALYSE that the regular query
starts with a full table scan over "Doc" while the CTE joins the two tables
first and applies the filter condition in the 2nd step.
I believe that some rows in "Doc" which are not referenced by "F" contain a
large amount of data in the field "szText" and this will slow down the ILIKE
operator.
What can I do to improve the performance of the regular query without using
a CTE?
This is a much simplified extract from a larger application:
CREATE TABLE Doc (
oID UUID NOT NULL PRIMARY KEY,
uDocID UUID NOT NULL UNIQUE,
szText TEXT
);
CREATE TABLE F (
oID UUID NOT NULL PRIMARY KEY,
uDocRef UUID,
CONSTRAINT F_fkey1 FOREIGN KEY (uDocRef) REFERENCES Doc (uDocID)
);
-- just in case .
ALTER TABLE Doc ALTER uDocID SET STATISTICS 10000;
ALTER TABLE Doc ALTER szText SET STATISTICS 10000;
VACUUM ANALYSE Doc;
SELECT COUNT(*) FROM Doc;
=> 125946 records
ALTER TABLE F ALTER uDocRef SET STATISTICS 10000;
VACUUM ANALYSE F;
SELECT COUNT(*) FROM F;
=> 32605 records
Result with CTE:
EXPLAIN ANALYSE
WITH a AS (
SELECT F.oID, Doc.szText
FROM F
JOIN Doc ON F.uDocRef = Doc.udocid
)
SELECT *
FROM a
WHERE szText ILIKE '%480GB%';
"CTE Scan on a (cost=9463.42..10197.03 rows=52 width=48) (actual
time=478.770..4551.613 rows=10 loops=1)"
" Filter: (sztext ~~* '%480GB%'::text)"
" Rows Removed by Filter: 32595"
" CTE a"
" -> Hash Join (cost=973.61..9463.42 rows=32605 width=359) (actual
time=36.998..100.337 rows=32605 loops=1)"
" Hash Cond: (doc.udocid = f.udocref)"
" -> Seq Scan on doc (cost=0.00..7691.46 rows=125946 width=359)
(actual time=0.008..18.269 rows=125946 loops=1)"
" -> Hash (cost=566.05..566.05 rows=32605 width=32) (actual
time=35.825..35.825 rows=32605 loops=1)"
" Buckets: 32768 Batches: 1 Memory Usage: 2294kB"
" -> Seq Scan on f (cost=0.00..566.05 rows=32605 width=32)
(actual time=0.005..14.677 rows=32605 loops=1)"
"Planning time: 4.689 ms"
"Execution time: 4554.893 ms"
Result with regular query:
EXPLAIN ANALYSE
SELECT F.oID, Doc.szText
FROM F
JOIN Doc ON F.uDocRef = Doc.udocid
WHERE szText ILIKE '%480GB%';
"Hash Join (cost=8006.56..8694.93 rows=5 width=359) (actual
time=66500.415..66506.978 rows=10 loops=1)"
" Hash Cond: (f.udocref = doc.udocid)"
" -> Seq Scan on f (cost=0.00..566.05 rows=32605 width=32) (actual
time=0.002..3.143 rows=32605 loops=1)"
" -> Hash (cost=8006.32..8006.32 rows=19 width=359) (actual
time=66500.023..66500.023 rows=16 loops=1)"
" Buckets: 1024 Batches: 1 Memory Usage: 19kB"
" -> Seq Scan on doc (cost=0.00..8006.32 rows=19 width=359) (actual
time=8864.720..66499.991 rows=16 loops=1)"
" Filter: (sztext ~~* '%480GB%'::text)"
" Rows Removed by Filter: 125930"
"Planning time: 263.542 ms"
"Execution time: 66507.003 ms"
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 09:36 CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
@ 2018-08-18 10:26 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2 siblings, 0 replies; 5+ messages in thread
From: Andreas Kretschmer @ 2018-08-18 10:26 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Am 18.08.2018 um 11:36 schrieb kpi6288@gmail.com:
> What can I do to improve the performance of the regular query without
> using a CTE?
try to rewrite it to a subselect:
select ... from ... join (selec ... from ... where ...) x on ...
Regards, Andreas
--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 09:36 CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
@ 2018-08-18 14:38 ` Stephen Frost <sfrost@snowman.net>
2018-08-18 15:29 ` Re: CTE with JOIN of two tables is much faster than a regular query Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 1 reply; 5+ messages in thread
From: Stephen Frost @ 2018-08-18 14:38 UTC (permalink / raw)
To: kpi6288@gmail.com; +Cc: pgsql-general
Greetings,
* kpi6288@gmail.com (kpi6288@gmail.com) wrote:
> The CTE mentioned below completes the query in 4.5 seconds while the regular
> query takes 66 seconds. I read from EXPLAIN ANALYSE that the regular query
> starts with a full table scan over "Doc" while the CTE joins the two tables
> first and applies the filter condition in the 2nd step.
>
> I believe that some rows in "Doc" which are not referenced by "F" contain a
> large amount of data in the field "szText" and this will slow down the ILIKE
> operator.
Yup, that appears to be what's happening.
> What can I do to improve the performance of the regular query without using
> a CTE?
You could possibly build a trigram index on the field you're searching,
which could avoid the full table scan. Of course, that index could be
quite large, so there's downsides to that. If these are words you're
looking for then you could use PG's full text indexing to build indexes
on the words and then use that instead. If you are fine working with
words but are concerned about misspellings then you can extract out the
distinct words, build a trigram index on those, find the most similar
words based on the input and then search for those words using the FTI.
Unfortunately, we don't currently pay attention to things like average
string length when considering the cost of performing an 'ilike', so we
figure that doing the filtering first and then the join will be faster,
but that obviously falls over in some cases, like this one. Using the
CTE forces PG to (today, at least) do the join first, but that isn't
really good to rely on.
Thanks!
Stephen
Attachments:
signature.asc (application/pgp-signature, 818B) download
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 09:36 CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
2018-08-18 14:38 ` Re: CTE with JOIN of two tables is much faster than a regular query Stephen Frost <sfrost@snowman.net>
@ 2018-08-18 15:29 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2018-08-18 15:29 UTC (permalink / raw)
To: Stephen Frost <sfrost@snowman.net>; +Cc: kpi6288@gmail.com; pgsql-general
Stephen Frost <sfrost@snowman.net> writes:
> * kpi6288@gmail.com (kpi6288@gmail.com) wrote:
>> The CTE mentioned below completes the query in 4.5 seconds while the regular
>> query takes 66 seconds.
> Unfortunately, we don't currently pay attention to things like average
> string length when considering the cost of performing an 'ilike', so we
> figure that doing the filtering first and then the join will be faster,
> but that obviously falls over in some cases, like this one. Using the
> CTE forces PG to (today, at least) do the join first, but that isn't
> really good to rely on.
Well, it's simpler than that: filter quals are always evaluated at
the lowest possible plan level. One of the Berkeley PhD theses that
we ripped out ages ago tried to be smarter about that, but the
cost/benefit/complexity ratio just wasn't very good, mainly because
it's so darn hard to estimate the selectivity of quals on subsets
of relations.
It's not very apparent why the results are so bad in this case,
either. One of the plans has the ILIKE being applied to circa 32600
rows, and the other one runs it on circa 126000 rows. That should
produce less than a 4x penalty, not 14x. Do the rows removed by
the join have significantly-longer-on-average sztext fields?
(If so, the odds that the planner would ever recognize such a
correlation seem pretty small.)
In any case, given that the ILIKE selects so few rows (and the planner
knows it!), finding a way to index that is clearly the right answer.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 09:36 CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
@ 2018-08-18 16:25 ` Ravi Krishna <sravikrishna@aol.com>
2 siblings, 0 replies; 5+ messages in thread
From: Ravi Krishna @ 2018-08-18 16:25 UTC (permalink / raw)
To: kpi6288@gmail.com; pgsql-general
> What can I do to improve the performance of the regular query without using a CTE?
Why do you care ? When I find that I can write a SQL 3 different ways, I will go for the most
efficient one. So why not accept the CTE version of this SQL. Just curious.
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2018-08-18 16:25 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-18 09:36 CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
2018-08-18 10:26 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2018-08-18 14:38 ` Stephen Frost <sfrost@snowman.net>
2018-08-18 15:29 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-08-18 16:25 ` Ravi Krishna <sravikrishna@aol.com>
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