Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bAW7T-0006p3-Kq for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2016 05:33:35 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bAW7S-000254-TW for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2016 05:33:34 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bAW7Q-00024D-Fj for pgsql-performance@postgresql.org; Wed, 08 Jun 2016 05:33:32 +0000 Received: from mail-oi0-x234.google.com ([2607:f8b0:4003:c06::234]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bAW7J-00008y-EB for pgsql-performance@postgresql.org; Wed, 08 Jun 2016 05:33:31 +0000 Received: by mail-oi0-x234.google.com with SMTP id e72so311463979oib.1 for ; Tue, 07 Jun 2016 22:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=U/KOtRvt5f81Mm0pj+MDr4DbUYSCU2YzR4y1Aaf7Xv0=; b=COLh1l7xtHV5XI2EALXslnLxhS7YM0uxHzCEwfqxK4TF1LCU0fW9nQmIlKs+LwXzS0 UNqtOxw4DPBfuYOvcEw+QWHM23pLGVq5yBYDAIR9N78OxUKQcodhm108TsLc0/BsiuDr PQZQ68V13A6usXy/HQUiLoKUP3Bug46ogXeyuiXr8fGcQcSejA/CR1Bdq4ZMx8HDpkp8 mA3DHWIXJzvVLA9Jv55Iguqv0x5GpZOj+v+pdkDbz6/CB74LWEvV6Zgav6x6HuTehO7S vuDONUuxu4U3l0X93Wsee6w6aKDE6Ivkr6hqzQ9ilm6x3jQ2ig+ecVDnDREz4KKGRKm5 TCLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=U/KOtRvt5f81Mm0pj+MDr4DbUYSCU2YzR4y1Aaf7Xv0=; b=bBK8LNGiul9IzwbszC3BsC3myXae1dEj6MEPwVGXt9GcLUt4gip2FdTeZEojGEClG3 0GXZW7KxwGcBgFuf9XQK/jn5L8mLCRqTeWPt+whAKOhy3x773LrmWg7oVLakkLy1o8Gy u04fXN0q2Gxp5mzr4I9Ff8pigL61ZFH3M/qnviavDyC61BJTBNtc/5/0KF3wti+7Uik8 VR/k81uyiwh0b5NXRPEWUsGc7BekDPlszlsEUq7PROs3na6qRpA/tfAiZhUlX89oUvGn nQv69zQZKclWS+dEVgqjPlRVui7+yBpEBbWM0E+WTOMVw2S9zp6/ANO+8n/9AKSXBRCz ILlQ== X-Gm-Message-State: ALyK8tIOw0vB2t1EJwKi6lsrRA2seHmsCUSeghs6Q4NdG5BJhmHYoCVXjt2kBMHMXRi9VELRYb6jkx1ThELCCA== MIME-Version: 1.0 X-Received: by 10.202.186.193 with SMTP id k184mr1861912oif.66.1465364004276; Tue, 07 Jun 2016 22:33:24 -0700 (PDT) Received: by 10.157.34.104 with HTTP; Tue, 7 Jun 2016 22:33:24 -0700 (PDT) In-Reply-To: References: Date: Wed, 8 Jun 2016 01:33:24 -0400 Message-ID: Subject: Re: Performance of LIKE/NOT LIKE when used in single query From: "David G. Johnston" To: Ed Felstein Cc: "pgsql-performance@postgresql.org" Content-Type: multipart/alternative; boundary=001a113ce218f1119e0534bda49e X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org --001a113ce218f1119e0534bda49e Content-Type: text/plain; charset=UTF-8 On Wednesday, June 8, 2016, Ed Felstein wrote: > 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? > Try AND...where col like '' and col not like '' Or a CTE (with) With likeqry as ( select where like ) Select from likeqry where not like (sorry for brevity but not at a pc) David J. --001a113ce218f1119e0534bda49e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

On Wednesday, June 8, 2016, Ed Felstein <efelstein@gmail.com> wrote:
Hello,
First time poster=C2=A0her= e.=C2=A0 Bear with me.
Using PostgreSQL 9.5
I have a si= tuation where I have a LIKE and a NOT LIKE in the same query to identify st= rings in=C2=A0a varchar=C2=A0field.=C2=A0 Since I am using wildcards, I hav= e created a GIN index on the field in question, which makes LIKE '%xxxx= %' searches run very fast.=C2=A0 The problem is the NOT LIKE phrases, w= hich (as would be expected) force a sequential scan.=C2=A0 Being that we= 9;re talking about millions of records, this is not desirable.
He= re's the question...
Is there a way, using a single query<= /u>, to emulate the process of running the LIKE part first, then running th= e NOT LIKE=C2=A0just on those results?=C2=A0 I can accomplish this in a mul= ti-step process=C2=A0by separating the single query into two queries,=C2=A0= populating a temporary table with the results of the LIKEs, then running th= e NOT LIKEs on the temporary table.=C2=A0 For various reasons, this is not = the ideal solution for me.
Or is there another approach that woul= d accomplish the same thing with the same level of performance?
=


Try AND...where col like &#= 39;'=C2=A0and col not like ''

Or a CTE= (with)

With likeqry as ( select where like )
Select from likeqry where not like

(sorry fo= r brevity but not at a pc)

David J.=C2=A0
--001a113ce218f1119e0534bda49e--