pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedAW: CTE with JOIN of two tables is much faster than a regular query
7+ messages / 3 participants
[nested] [flat]
* AW: CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-18 11:08 ` kpi6288@gmail.com
2018-08-18 14:24 ` Re: AW: CTE with JOIN of two tables is much faster than a regular query Adrian Klaver <adrian.klaver@aklaver.com>
4 siblings, 1 reply; 7+ messages in thread
From: kpi6288@gmail.com @ 2018-08-18 11:08 UTC (permalink / raw)
To: pgsql-general
> -----Ursprüngliche Nachricht-----
> Von: Andreas Kretschmer <andreas@a-kretschmer.de>
> Gesendet: Samstag, 18. August 2018 12:27
> 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 ...
>
Do mean like this?
EXPLAIN ANALYSE
SELECT F.oID, D.szText
FROM F
JOIN (SELECT Doc.uDocID, Doc.szText FROM Doc WHERE szText ILIKE '%480GB%')
AS D ON D.uDocID = F.uDocRef;
Just as bad as my regular query:
"Hash Join (cost=8006.56..8694.93 rows=5 width=359) (actual
time=66777.898..66784.630 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.563 rows=32605 loops=1)"
" -> Hash (cost=8006.32..8006.32 rows=19 width=359) (actual
time=66777.471..66777.471 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=9013.317..66777.438 rows=16 loops=1)"
" Filter: (sztext ~~* '%480GB%'::text)"
" Rows Removed by Filter: 125930"
"Planning time: 236.354 ms"
"Execution time: 66784.651 ms"
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: AW: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 11:08 ` AW: CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
@ 2018-08-18 14:24 ` Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 7+ messages in thread
From: Adrian Klaver @ 2018-08-18 14:24 UTC (permalink / raw)
To: kpi6288@gmail.com; pgsql-general
On 08/18/2018 04:08 AM, kpi6288@gmail.com wrote:
>
>
>> -----Ursprüngliche Nachricht-----
>> Von: Andreas Kretschmer <andreas@a-kretschmer.de>
>> Gesendet: Samstag, 18. August 2018 12:27
>
>> 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 ...
>>
>
> Do mean like this?
>
> EXPLAIN ANALYSE
> SELECT F.oID, D.szText
> FROM F
> JOIN (SELECT Doc.uDocID, Doc.szText FROM Doc WHERE szText ILIKE '%480GB%')
> AS D ON D.uDocID = F.uDocRef;
To try to replicate what the CTE is doing I would try:
SELECT
*
FROM
Doc
JOIN
(SELECT
uDocRef, F.oID, Doc.szText
FROM
F
JOIN
Doc
ON
F.uDocRef = Doc.udocid
) AS D
ON
D.uDocRef = Doc.udocid
WHERE
D.szText ILIKE '%480GB%'
>
> Just as bad as my regular query:
>
> "Hash Join (cost=8006.56..8694.93 rows=5 width=359) (actual
> time=66777.898..66784.630 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.563 rows=32605 loops=1)"
> " -> Hash (cost=8006.32..8006.32 rows=19 width=359) (actual
> time=66777.471..66777.471 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=9013.317..66777.438 rows=16 loops=1)"
> " Filter: (sztext ~~* '%480GB%'::text)"
> " Rows Removed by Filter: 125930"
> "Planning time: 236.354 ms"
> "Execution time: 66784.651 ms"
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* AW: CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-18 15:07 ` kpi6288@gmail.com
4 siblings, 0 replies; 7+ messages in thread
From: kpi6288@gmail.com @ 2018-08-18 15:07 UTC (permalink / raw)
To: pgsql-general
> -----Ursprüngliche Nachricht-----
> Von: Stephen Frost <sfrost@snowman.net>
> Gesendet: Samstag, 18. August 2018 16:39
Hello,
>
> > 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.
A trigram index would be a possible help in this particular scenario but
size and updating the index in other parts of the application would be
probably create other issues. I may try it, though.
But thanks to confirming my assumption. I just thought that it should be
obvious to the optimizer to do the join first and filter on this result. But
I'm reading you r post that there is nothing that I can do to modify the
behavior of the optimizer. Or is there a way to specify the cost for an
operator (ILIKE in this case) on a specific column?
Thanks
Klaus
^ permalink raw reply [nested|flat] 7+ messages in thread
* AW: CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-18 15:43 ` kpi6288@gmail.com
4 siblings, 0 replies; 7+ messages in thread
From: kpi6288@gmail.com @ 2018-08-18 15:43 UTC (permalink / raw)
To: pgsql-general
> -----Ursprüngliche Nachricht-----
> Von: Tom Lane <tgl@sss.pgh.pa.us>
> Gesendet: Samstag, 18. August 2018 17:29
>
> Well, it's simpler than that: filter quals are always evaluated at the
lowest
> possible plan level.
Thank you. This "always" was not clear to me, but it explains a few similar
cases (with not-so-extreme differences) that I could not understand.
Regards
Klaus
^ permalink raw reply [nested|flat] 7+ messages in thread
* AW: CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-18 16:33 ` kpi6288@gmail.com
2018-08-19 02:56 ` Re: AW: CTE with JOIN of two tables is much faster than a regular query Tim Cross <theophilusx@gmail.com>
4 siblings, 1 reply; 7+ messages in thread
From: kpi6288@gmail.com @ 2018-08-18 16:33 UTC (permalink / raw)
To: pgsql-general
> -----Ursprüngliche Nachricht-----
> Von: Ravi Krishna <sravikrishna@aol.com>
> Gesendet: Samstag, 18. August 2018 18:25
>
> > 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.
We're using object mapping / entity frameworks (e.g. XPO, Entity Framework Core). These frameworks support regular queries out-of-the box; a CTEs require additional effort and are more difficult to maintain.
Regards
Klaus
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: AW: CTE with JOIN of two tables is much faster than a regular query
2018-08-18 16:33 ` AW: CTE with JOIN of two tables is much faster than a regular query kpi6288@gmail.com
@ 2018-08-19 02:56 ` Tim Cross <theophilusx@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Tim Cross @ 2018-08-19 02:56 UTC (permalink / raw)
To: kpi6288@gmail.com; +Cc: pgsql-general
kpi6288@gmail.com writes:
>> -----Ursprüngliche Nachricht-----
>> Von: Ravi Krishna <sravikrishna@aol.com>
>> Gesendet: Samstag, 18. August 2018 18:25
>>
>> > 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.
>
> We're using object mapping / entity frameworks (e.g. XPO, Entity Framework Core). These frameworks support regular queries out-of-the box; a CTEs require additional effort and are more difficult to maintain.
>
Ah, another reason to avoid object mapping/entity frameworks! I guess
really the same reason - loss of flexibility and expressive power.
Sorry, having a similar battle with some developers who are insisting on
using a particular framework because it makes maintenance easier as it
'automates' creation of controllers (MVC). However, they are frustrated
by performance and I'm frustrated as the framework also fails to pass
additional information, such as PGAPPNAME, which would make some
analysis easier. Part of the reason for the performance issues is
because the developers are doing things with result sets within the
client that would be far more efficient performed within the database.
One way I have resolved this in the past is to create database
procedures which present a 'mapped' view back to the framework layer
which hides the SQL from the framework. Works well, with the only main
downside being you now have SQL in a different (another) place, which
can make some people uncomfortable and can be a maintenance issue if all
your developers are just front-end devs who treat a database as just a
key/value repository. .
Tim
--
Tim Cross
^ permalink raw reply [nested|flat] 7+ messages in thread
* AW: CTE with JOIN of two tables is much faster than a regular query
@ 2018-08-19 08:59 ` kpi6288@gmail.com
4 siblings, 0 replies; 7+ messages in thread
From: kpi6288@gmail.com @ 2018-08-19 08:59 UTC (permalink / raw)
To: pgsql-general
> -----Ursprüngliche Nachricht-----
> Von: Tom Lane <tgl@sss.pgh.pa.us>
> Gesendet: Samstag, 18. August 2018 17:29
>
> 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.
A trigram index took 9 minutes to build but improved the regular query from
67 seconds down to 500 ms.
Although this is an impressive improvement, I'm afraid that the index might
create a delays in other parts of the application (INSERT / UPDATE). We will
probably rework the design of this particular table.
Thanks to everyone who helped me in this matter.
Regards
Klaus
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2018-08-19 08:59 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-18 11:08 ` kpi6288@gmail.com
2018-08-18 14:24 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-08-18 15:07 ` kpi6288@gmail.com
2018-08-18 15:43 ` kpi6288@gmail.com
2018-08-18 16:33 ` kpi6288@gmail.com
2018-08-19 02:56 ` Tim Cross <theophilusx@gmail.com>
2018-08-19 08:59 ` kpi6288@gmail.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