Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XjYN6-0001E5-6u for pgsql-general@arkaria.postgresql.org; Wed, 29 Oct 2014 18:53:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XjYN5-0000xr-81 for pgsql-general@arkaria.postgresql.org; Wed, 29 Oct 2014 18:53:27 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XjYN3-0000xk-Qi for pgsql-general@postgresql.org; Wed, 29 Oct 2014 18:53:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XjYN0-0004dU-LP for pgsql-general@postgresql.org; Wed, 29 Oct 2014 18:53:24 +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 s9TIrI5C029613; Wed, 29 Oct 2014 14:53:18 -0400 From: Tom Lane To: Jorge Arevalo cc: David G Johnston , pgsql-general@postgresql.org Subject: Re: Query optimization In-reply-to: References: <1414547659900-5824746.post@n5.nabble.com> Comments: In-reply-to Jorge Arevalo message dated "Wed, 29 Oct 2014 19:06:03 +0100" Date: Wed, 29 Oct 2014 14:53:18 -0400 Message-ID: <29612.1414608798@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org Jorge Arevalo writes: > SELECT value1,value2,value3,value4, > value5, hstore(ARRAY['field9', 'field10', 'field11', 'field12', 'field13', > 'field14'], ARRAY[field9, field10, field11, field12, field13, field14) as > metadata, value7, (select array((select row(f1, f2) from table2 p where > p.f3 = field7))) as values_array FROM table1 The parentheses/brackets don't seem to match up real well here ... > This is the result of EXPLAIN ANALYZE > QUERY > PLAN > ------------------------------------------------------------------------------------------------------------------------------------------------- > Index Scan using table1_pkey on table1 (cost=67846.38..395773.45 > rows=8419127 width=88) (actual time=7122.704..22670.680 rows=8419127 > loops=1) > InitPlan 2 (returns $1) > -> Result (cost=67846.29..67846.29 rows=1 width=0) (actual > time=7009.063..7009.065 rows=1 loops=1) > InitPlan 1 (returns $0) > -> Seq Scan on table2 p (cost=0.00..67846.29 rows=12689 > width=20) (actual time=14.971..5069.840 rows=2537787 loops=1) > Filter: (f3 = field7) Hm. If I'm reading that right, you're building an array containing 2537787 entries, each of which is a composite datum containing two columns of unmentioned datatypes. I suspect a big chunk of your runtime is going into manipulating that array -- PG is not terribly efficient with big arrays containing variable-width values. I'm also a bit confused as to why the planner is saying that the (SELECT ARRAY(...)) bit is an InitPlan and not a SubPlan. That implies that "field7" in the innermost WHERE clause is not a reference to table1 but a reference to table2. Is that really what you meant? IOW, are you sure this query is performing the right calculation in the first place? regards, tom lane -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general