Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZCVKk-0002bM-0L for pgsql-performance@arkaria.postgresql.org; Tue, 07 Jul 2015 16:02:58 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZCVKj-0002t8-D4 for pgsql-performance@arkaria.postgresql.org; Tue, 07 Jul 2015 16:02:57 +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) (envelope-from ) id 1ZCVKh-0002pn-AT for pgsql-performance@postgresql.org; Tue, 07 Jul 2015 16:02:55 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZCVKe-00067I-DC for pgsql-performance@postgresql.org; Tue, 07 Jul 2015 16:02:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t67G2nL7027075; Tue, 7 Jul 2015 12:02:49 -0400 From: Tom Lane To: Mike Broers cc: pgsql-performance@postgresql.org Subject: Re: wildcard text filter switched to boolean column, performance is way worse In-reply-to: References: Comments: In-reply-to Mike Broers message dated "Tue, 07 Jul 2015 10:40:48 -0500" Date: Tue, 07 Jul 2015 12:02:49 -0400 Message-ID: <27074.1436284969@sss.pgh.pa.us> 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 Mike Broers writes: > I had a query that was filtering with a wildcard search of a text field for > %SUCCESS%. The query took about 5 seconds and was running often so I wanted > to improve it. I suggested that the engineers include a new boolean column > for successful status. They implemented the requested field, but the query > that filters on that new column runs very long (i kill it after letting it > run for about an hour). Can someone help me understand why that is the > case and how to resolve it? It's hashing the subplan output in the first case and not the second: > Seq Scan on lead (cost=130951.81..158059.21 rows=139957 width=369) (actual > time=4699.619..4699.869 rows=1 loops=1) > Filter: ((NOT (hashed SubPlan 1)) AND (("ReferenceNumber")::text <> > ''::text) AND ((NOT (hashed SubPlan 2)) OR (NOT (hashed SubPlan 3)))) ^^^^^^^^^^^^^^^^ vs > Seq Scan on lead (cost=85775.78..9005687281.12 rows=139957 width=369) > Filter: ((NOT (hashed SubPlan 1)) AND (("ReferenceNumber")::text <> > ''::text) AND ((NOT (hashed SubPlan 2)) OR (NOT (SubPlan 3)))) ^^^^^^^^^ Presumably, the new more-accurate rows count causes the planner to realize that the hash table will exceed work_mem so it doesn't choose to hash ... but for your situation, you'd rather it did, because what you're getting instead is a Materialize node that spills to disk (again, because the data involved exceeds work_mem) and that's a killer for this query. You should be able to get back the old behavior if you raise work_mem enough. Another idea you might think about is changing the OR'd IN conditions to a single IN over a UNION ALL of the subselects. I'm not really sure if that would produce a better plan, but it's worth trying if it wouldn't require too much app-side contortion. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance