Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFPG4-0004BI-Ft for pgsql-performance@arkaria.postgresql.org; Fri, 30 Aug 2013 14:01:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VFPG3-0004Uw-Mi for pgsql-performance@arkaria.postgresql.org; Fri, 30 Aug 2013 14:01:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFPG2-0004Up-KR for pgsql-performance@postgresql.org; Fri, 30 Aug 2013 14:01:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFPFz-0005Cm-LF for pgsql-performance@postgresql.org; Fri, 30 Aug 2013 14:01:02 +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 r7UE0SJM005900; Fri, 30 Aug 2013 10:00:29 -0400 From: Tom Lane To: Mathieu De Zutter cc: pgsql-performance Subject: Re: Query plan change with multiple elements in IN clause In-reply-to: References: Comments: In-reply-to Mathieu De Zutter message dated "Fri, 30 Aug 2013 11:05:46 +0200" Date: Fri, 30 Aug 2013 10:00:28 -0400 Message-ID: <5899.1377871228@sss.pgh.pa.us> X-Pg-Spam-Score: -4.1 (----) 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 Mathieu De Zutter writes: > The problem is that this join is performing very badly when more than one > work is involved. It chooses a plan that is orders of magnitude slower. > I have attached > - The (simplified) table definitions > - The (simplified) view > - Two queries with explain analyze plan: "IN (1)" => fast, "IN (1,3)" => > slow > - postgresql.conf The reason you get a nice plan in the first case is that "w.id in (1)" is treated as "w.id = 1", and then there is logic that combines that with "w.id = wps.id" to conclude that we can synthesize a condition "wps.id = 1". None of that happens when there's more than one IN item, because it's not an equality operator anymore. You might be able to do something like JOIN (VALUES (1),(3)) foo(x) ON w.id = foo.x 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