pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: kpi6288@gmail.com
To: pgsql-general@postgresql.org
Subject: AW: CTE with JOIN of two tables is much faster than a regular query
Date: Sat, 18 Aug 2018 17:07:47 +0200
Message-ID: <006401d43705$399f6aa0$acde3fe0$@gmail.com> (raw)
In-Reply-To: <20180818143848.GB3326@tamriel.snowman.net>
References: <004b01d436d6$ff96c840$fec458c0$@gmail.com>
	<20180818143848.GB3326@tamriel.snowman.net>

> -----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





view thread (7+ messages)  latest in thread

Message-ID: <006401d43705$399f6aa0$acde3fe0$@gmail.com>
Permalink:  ../006401d43705$399f6aa0$acde3fe0$@gmail.com/
Also on:    postgresql.org/message-id/006401d43705$399f6aa0$acde3fe0$@gmail.com

 · 

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: kpi6288@gmail.com
  Subject: Re: CTE with JOIN of two tables is much faster than a regular query
  In-Reply-To: <006401d43705$399f6aa0$acde3fe0$@gmail.com>
  References: <004b01d436d6$ff96c840$fec458c0$@gmail.com> <20180818143848.GB3326@tamriel.snowman.net> <006401d43705$399f6aa0$acde3fe0$@gmail.com>

* 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