From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stephen Frost <sfrost@snowman.net>
Cc: kpi6288@gmail.com
Cc: pgsql-general@postgresql.org
Subject: Re: CTE with JOIN of two tables is much faster than a regular query
Date: Sat, 18 Aug 2018 11:29:06 -0400
Message-ID: <18425.1534606146@sss.pgh.pa.us> (raw)
In-Reply-To: <20180818143848.GB3326@tamriel.snowman.net>
References: <004b01d436d6$ff96c840$fec458c0$@gmail.com>
<20180818143848.GB3326@tamriel.snowman.net>
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
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: tgl@sss.pgh.pa.us, sfrost@snowman.net, kpi6288@gmail.com
Subject: Re: CTE with JOIN of two tables is much faster than a regular query
In-Reply-To: <18425.1534606146@sss.pgh.pa.us>
References: <004b01d436d6$ff96c840$fec458c0$@gmail.com> <20180818143848.GB3326@tamriel.snowman.net> <18425.1534606146@sss.pgh.pa.us>
* 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