Re: CTE with JOIN of two tables is much faster than a regular query - Mailing list pgsql-general

From Tom Lane
Subject Re: CTE with JOIN of two tables is much faster than a regular query
Date
Msg-id 18425.1534606146@sss.pgh.pa.us
Whole thread Raw
In response to Re: CTE with JOIN of two tables is much faster than a regular query  (Stephen Frost <sfrost@snowman.net>)
Responses AW: CTE with JOIN of two tables is much faster than a regular query
AW: CTE with JOIN of two tables is much faster than a regular query
List 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


pgsql-general by date:

Previous
From: Oleksii Kliukin
Date:
Subject: Re: regex match and special characters
Next
From:
Date:
Subject: AW: CTE with JOIN of two tables is much faster than a regular query