public inbox for [email protected]  
help / color / mirror / Atom feed
From: Ed Felstein <[email protected]>
To: [email protected]
Subject: Performance of LIKE/NOT LIKE when used in single query
Date: Tue, 7 Jun 2016 21:57:44 -0700
Message-ID: <CAJpH9Wk3z+iuxFznSgS57wyAMh5dkx9+LPym=y0FEHq0oVWFRQ@mail.gmail.com> (raw)
List-Unsubscribe:  <mailto:[email protected]?body=unsub%20pgsql-performance>

Hello,
First time poster here.  Bear with me.
Using PostgreSQL 9.5
I have a situation where I have a LIKE and a NOT LIKE in the same query to
identify strings in a varchar field.  Since I am using wildcards, I have
created a GIN index on the field in question, which makes LIKE '%xxxx%'
searches run very fast.  The problem is the NOT LIKE phrases, which (as
would be expected) force a sequential scan.  Being that we're talking about
millions of records, this is not desirable.
Here's the question...
Is there a way, *using a single query*, to emulate the process of running
the LIKE part first, then running the NOT LIKE just on those results?  I
can accomplish this in a multi-step process by separating the single query
into two queries, populating a temporary table with the results of the
LIKEs, then running the NOT LIKEs on the temporary table.  For various
reasons, this is not the ideal solution for me.
Or is there another approach that would accomplish the same thing with the
same level of performance?


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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: Performance of LIKE/NOT LIKE when used in single query
  In-Reply-To: <CAJpH9Wk3z+iuxFznSgS57wyAMh5dkx9+LPym=y0FEHq0oVWFRQ@mail.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 agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox