Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V76H0-0005Sg-U1 for pgsql-performance@arkaria.postgresql.org; Wed, 07 Aug 2013 16:07:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1V76H0-0005Av-4k for pgsql-performance@arkaria.postgresql.org; Wed, 07 Aug 2013 16:07:42 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V76Gz-0005AZ-9s for pgsql-performance@postgresql.org; Wed, 07 Aug 2013 16:07:41 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1V76Gw-0006No-Om for pgsql-performance@postgresql.org; Wed, 07 Aug 2013 16:07:40 +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 r77G7bc0006582; Wed, 7 Aug 2013 12:07:37 -0400 From: Tom Lane To: =?UTF-8?B?QWxleGlzIEzDqi1RdcO0Yw==?= cc: pgsql-performance@postgresql.org Subject: Re: Better performance possible for a pathological query? In-reply-to: References: Comments: In-reply-to =?UTF-8?B?QWxleGlzIEzDqi1RdcO0Yw==?= message dated "Wed, 07 Aug 2013 11:38:47 -0400" Date: Wed, 07 Aug 2013 12:07:37 -0400 Message-ID: <6581.1375891657@sss.pgh.pa.us> X-Pg-Spam-Score: -0.0 (/) 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 =?UTF-8?B?QWxleGlzIEzDqi1RdcO0Yw==?= writes: > The query itself is very simple: a primary key lookup on a 1.5x10^7 rows. > The issue is that we are looking up over 11,000 primary keys at once, > causing the db to consume a lot of CPU. It looks like most of the runtime is probably going into checking the c.key = ANY (ARRAY[...]) construct. PG isn't especially smart about that if it fails to optimize the construct into an index operation --- I think it's just searching the array linearly for each row meeting the other restrictions on c. You could try writing the test like this: c.key = ANY (VALUES (1), (17), (42), ...) to see if the sub-select code path gives better results than the array code path. In a quick check it looked like this might produce a hash join, which seemed promising anyway. 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