Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XqR8P-0006ch-UI for pgsql-sql@arkaria.postgresql.org; Mon, 17 Nov 2014 18:34:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XqR8O-0004Tw-P3 for pgsql-sql@arkaria.postgresql.org; Mon, 17 Nov 2014 18:34:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XqR8O-0004Tq-0L for pgsql-sql@postgresql.org; Mon, 17 Nov 2014 18:34:44 +0000 Received: from mail-wg0-x236.google.com ([2a00:1450:400c:c00::236]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XqR8J-00020i-QX for pgsql-sql@postgresql.org; Mon, 17 Nov 2014 18:34:42 +0000 Received: by mail-wg0-f54.google.com with SMTP id y10so4200850wgg.27 for ; Mon, 17 Nov 2014 10:34:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=0p5rfJ4nNwurFPyjV29bGZn6rOkwTQT/er9SM4N1+9I=; b=aRsf7xAfoOCcTuvxXJuQj3ntLwEoe/Pij2Qy2aYkFSqIVOtYEViWKzal3CDx7eMvTI juOoXelnOzUbF8aAVvLw1oVAJJaT8/NblpYl0l+AFY+4BqBWwMvsq//OhmXkgJVgHt3Q 0cy2ZfATWKNkYG3cDe70dVH2BnSw23gRwkGKBnwmwRvDsMwGRlLhcGcUJ0NBDaPdw2to O8uewQnhUcQbdDA1fhIWJ2ipC0aqlCS8MORSnA1ZRxhHS77PbYzTlq2V/mu5zEEpbM+0 YiMXfO05yvjXS1kqsoaYzhxQF8DfKRjYkP1tW8UdOab+brOsFWosK+FiJKu/68o1S7Cy 91/A== X-Received: by 10.180.212.5 with SMTP id ng5mr33925144wic.50.1416249277478; Mon, 17 Nov 2014 10:34:37 -0800 (PST) Received: from [192.168.1.64] (host217-43-225-174.range217-43.btcentralplus.com. [217.43.225.174]) by mx.google.com with ESMTPSA id d5sm15880057wjb.34.2014.11.17.10.34.35 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 17 Nov 2014 10:34:36 -0800 (PST) Message-ID: <546A3FBA.9020901@gmail.com> Date: Mon, 17 Nov 2014 18:34:34 +0000 From: Tim Dudgeon User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: slow sub-query problem Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.7 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org I'm having problems optimising a query that's very slow due to a sub-query. The query is this: SELECT structure_id, batch_id, property_id, property_data FROM chemcentral.structure_props WHERE structure_id IN (SELECT structure_id FROM chemcentral.structure_props WHERE property_id = 643413) AND property_id IN (1, 643413, 1106201); and it takes 18s to execute. It I replace the sub-query with the inlined 369 values so that the 4th line looks like this: WHERE structure_id IN (1122687,309004,306064 ...) it takes a few ms. The plans are: 1. sub-query "Nested Loop (cost=1132.97..1182.28 rows=43 width=644) (actual time=70.926..18937.669 rows=381 loops=1)" " -> HashAggregate (cost=1091.73..1091.75 rows=2 width=4) (actual time=2.829..3.212 rows=366 loops=1)" " Group Key: structure_props_1.structure_id" " -> Index Scan using idx_sp_property_id on structure_props structure_props_1 (cost=0.43..1090.77 rows=382 width=4) (actual time=0.033..2.380 rows=369 loops=1)" " Index Cond: (property_id = 643413)" " -> Bitmap Heap Scan on structure_props (cost=41.24..45.26 rows=1 width=644) (actual time=51.726..51.727 rows=1 loops=366)" " Recheck Cond: ((structure_id = structure_props_1.structure_id) AND (property_id = ANY ('{1,643413,1106201}'::integer[])))" " Heap Blocks: exact=381" " -> BitmapAnd (cost=41.24..41.24 rows=1 width=0) (actual time=51.714..51.714 rows=0 loops=366)" " -> Bitmap Index Scan on idx_sp_structure_id (cost=0.00..6.80 rows=317 width=0) (actual time=0.046..0.046 rows=475 loops=366)" " Index Cond: (structure_id = structure_props_1.structure_id)" " -> Bitmap Index Scan on idx_sp_property_id (cost=0.00..33.90 rows=1146 width=0) (actual time=51.656..51.656 rows=811892 loops=366)" " Index Cond: (property_id = ANY ('{1,643413,1106201}'::integer[]))" "Planning time: 0.497 ms" "Execution time: 18937.868 ms" 2. inlined values "Bitmap Heap Scan on structure_props (cost=2600.48..2645.29 rows=10 width=644) (actual time=71.676..72.724 rows=381 loops=1)" " Recheck Cond: ((property_id = ANY ('{1,643413,1106201}'::integer[])) AND (structure_id = ANY ('{1122687,309004,306064,278852,234066,1122645,412925,280033,423990,568929,448302,278487,278955,40430,40430,467979,467508,288413,289746,306073,355352,265583,4779 (...)" " Heap Blocks: exact=381" " -> BitmapAnd (cost=2600.48..2600.48 rows=10 width=0) (actual time=71.608..71.608 rows=0 loops=1)" " -> Bitmap Index Scan on idx_sp_property_id (cost=0.00..33.90 rows=1146 width=0) (actual time=54.614..54.614 rows=811892 loops=1)" " Index Cond: (property_id = ANY ('{1,643413,1106201}'::integer[]))" " -> Bitmap Index Scan on idx_sp_structure_id (cost=0.00..2566.32 rows=117367 width=0) (actual time=14.487..14.487 rows=173867 loops=1)" " Index Cond: (structure_id = ANY ('{1122687,309004,306064,278852,234066,1122645,412925,280033,423990,568929,448302,278487,278955,40430,40430,467979,467508,288413,289746,306073,355352,265583,477941,326652,326602,233964,15338,397586,1122647,3088 (...)" "Planning time: 1.052 ms" "Execution time: 72.858 ms" Table is like this: CREATE TABLE chemcentral.structure_props ( id serial NOT NULL, source_id integer NOT NULL, structure_id integer NOT NULL, batch_id character varying(16), parent_id integer, property_id integer NOT NULL, property_data jsonb, CONSTRAINT structure_props_pkey PRIMARY KEY (id) ) All relevant columns are indexed and using PostgreSQL 9.4. Any clues how to re-write it to avoid the slow sub-query. Many thanks Tim -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql