Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bAVab-0004PA-C6 for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2016 04:59:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bAVaZ-0007E8-QP for pgsql-performance@arkaria.postgresql.org; Wed, 08 Jun 2016 04:59:35 +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 1bAVYu-0005Oe-TQ for pgsql-performance@postgresql.org; Wed, 08 Jun 2016 04:57:53 +0000 Received: from mail-yw0-x230.google.com ([2607:f8b0:4002:c05::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1bAVYn-0007rz-Vv for pgsql-performance@postgresql.org; Wed, 08 Jun 2016 04:57:51 +0000 Received: by mail-yw0-x230.google.com with SMTP id o16so190258560ywd.2 for ; Tue, 07 Jun 2016 21:57:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to; bh=39sYvaLhgi/MCGiRB6zUsaVO81e1kPFUwSRwImcAvxc=; b=AF9udGjnysT+Uxf5nT3IZCFXX15b+Yf/nqi6MB20JE21BMrWDsiLNQy6oEdrcSndo+ eziC0qMs2CVo95UMF+PYa/5YW+GJmWhEjdSyrKt81M8ESHVJMB0GSPin2eYcono1OKiB wQNQYS4Y4B1L9FvkfK0nuSRaeaYjUR4xR11BWeJPOcVCgH+QwDU9sOcdpLWO7S8XOjtI +QFrN7x5pe0BgvFbkQFVbqjJ+7KFs3jLVbhYQVgveLVAYOT9sfZQgu0+gy8ad4vPO73s Y/9DEQSKtgOVRkXvtjv1rQxV9hY1LbatPwAcEJz/16lKtuBgI6aHQvxL4OMCqAVmw782 d7ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=39sYvaLhgi/MCGiRB6zUsaVO81e1kPFUwSRwImcAvxc=; b=lKVP/PkbP+bRbfwy6jr3se6jKddxeAGxeeGxD+eDfietWWQ0U5tV5M7g1zoTGAsQQg W5LqeZi2cXLm9/7bCGw+NB8dDjzvVuP6r9ROuiZnG8WQ55XWdxGt3Y2GBparmpvymAfy CvFEzhxj4X7+dszYW/Y4Hj1KVieNsstBoqdtAoZ/sOaPWYHlTBk/zHRW8SG/Uc4kCOQv h+w67Dg7VsIPUIodDhK9G/ukZvODXMajZglw6aALJ9p5fMg58iw1uStJ66Q7Lwupp75q nAPEswgKVyPb4s3ftBDkvwLkQcVa5Gd1QGi/6qYIFEPJCZPGqhamNSCe6T+JcexUrJw5 eGuw== X-Gm-Message-State: ALyK8tKRIu8mb1UVCNNzZwnWSSlE99uVZGaqU9UKq6FgTC9vSICVLiF6BPf6WquN//YjRS493sb+Sya6JkoGvQ== X-Received: by 10.129.87.7 with SMTP id l7mr329370ywb.257.1465361865189; Tue, 07 Jun 2016 21:57:45 -0700 (PDT) MIME-Version: 1.0 Received: by 10.13.219.77 with HTTP; Tue, 7 Jun 2016 21:57:44 -0700 (PDT) From: Ed Felstein Date: Tue, 7 Jun 2016 21:57:44 -0700 Message-ID: Subject: Performance of LIKE/NOT LIKE when used in single query To: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=001a113aaafc7138da0534bd25ac 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 --001a113aaafc7138da0534bd25ac Content-Type: text/plain; charset=UTF-8 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? --001a113aaafc7138da0534bd25ac Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello,
First time poster=C2=A0here.=C2=A0 B= ear with me.
Using PostgreSQL 9.5
I have a situation wh= ere I have a LIKE and a NOT LIKE in the same query to identify strings in= =C2=A0a varchar=C2=A0field.=C2=A0 Since I am using wildcards, I have create= d a GIN index on the field in question, which makes LIKE '%xxxx%' s= earches run very fast.=C2=A0 The problem is the NOT LIKE phrases, which (as= would be expected) force a sequential scan.=C2=A0 Being that we're tal= king 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 LI= KE=C2=A0just on those results?=C2=A0 I can accomplish this in a multi-step = process=C2=A0by separating the single query into two queries,=C2=A0populati= ng a temporary table with the results of the LIKEs, then running the NOT LI= KEs on the temporary table.=C2=A0 For various reasons, this is not the idea= l solution for me.
Or is there another approach that would accomp= lish the same thing with the same level of performance?

--001a113aaafc7138da0534bd25ac--