Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 10FAA11DF229 for ; Thu, 31 May 2012 20:34:44 -0300 (ADT) Received: from na3sys009aob106.obsmtp.com ([74.125.149.76] helo=na3sys009aog106.obsmtp.com) by makus.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1SaEt8-0000RK-GW for pgsql-performance@postgresql.org; Thu, 31 May 2012 23:34:43 +0000 Received: from mail-pz0-f54.google.com ([209.85.210.54]) (using TLSv1) by na3sys009aob106.postini.com ([74.125.148.12]) with SMTP ID DSNKT8gABRA4hyIxdYc4b91J4TFf2d/0iKKT@postini.com; Thu, 31 May 2012 16:34:42 PDT Received: by dadv36 with SMTP id v36so2102259dad.13 for ; Thu, 31 May 2012 16:34:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=F9bT5n/MAwL2z2FOMGb8i5rHvlJq66ihz2GxHeq0NIU=; b=aSD35/8biNad9H3ro4JYJr8PFnjaC5JYvVq3SMsZDZvI6HEXQPo9FROk21vFH/n0We bkcsGszJvwUIw/bcNBrl0fsZ4WFzc06v8gCa5/ttE0t3FgJiXfSWbg/CfmWMqployule T6SsTcrZaRLHMufVquIc1IDV50Irt/XDGqf2Iim7o+p4JpY4R9Qp9+tHI8K+q3dZZGXW JZXVNLjdCi6eBdhDgCxybQjQBWKhvbf9rfm/dC8a6FbKbHQz54db42B74ZH8UIOHGh4V JlFCfx2hn5WvlmcaVzFCFmhwSG54oP9voeau4YN9KuA7fqgGBeHwcS2+ItakD8OhAsqz Yb6A== Received: by 10.68.226.193 with SMTP id ru1mr4350881pbc.79.1338507268734; Thu, 31 May 2012 16:34:28 -0700 (PDT) Received: from [172.20.9.121] (eth59-167-133-99.static.internode.on.net. [59.167.133.99]) by mx.google.com with ESMTPS id qq2sm612144pbc.27.2012.05.31.16.34.26 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 31 May 2012 16:34:27 -0700 (PDT) Message-ID: <4FC80000.2000900@atlassian.com> Date: Fri, 01 Jun 2012 09:34:24 +1000 From: Trevor Campbell User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20120430 Thunderbird/12.0.1 MIME-Version: 1.0 To: Craig James CC: pgsql-performance@postgresql.org Subject: Re: Trouble with plan statistics for behaviour for query. References: <4FC7F0DB.40600@atlassian.com> <4FC7F844.8070205@atlassian.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gm-Message-State: ALoCoQk9aBpqZPK4IpaEreaH0p9XUBlaa5Q9kN2vF7k1ayngIvELn6tT2Amx0cbeuLJ9haZKs41f X-Pg-Spam-Score: -3.1 (---) X-Archive-Number: 201205/201 X-Sequence-Number: 47025 Thanks Craig, that certainly leads down the right path. The following is all done in pgAdmin3: Using an actual value we I get the plan I expect explain analyze select CG.ID, CG.ISSUEID, CG.AUTHOR, CG.CREATED, CI.ID, CI.FIELDTYPE, CI.FIELD, CI.OLDVALUE, CI.OLDSTRING, CI.NEWVALUE, CI.NEWSTRING from PUBLIC.CHANGEGROUP CG inner join PUBLIC.CHANGEITEM CI on CG.ID = CI.GROUPID where CG.ISSUEID=10006 order by CG.CREATED asc, CI.ID asc "Sort (cost=106.18..106.22 rows=13 width=434) (actual time=0.115..0.115 rows=12 loops=1)" " Sort Key: cg.created, ci.id" " Sort Method: quicksort Memory: 29kB" " -> Nested Loop (cost=0.00..105.94 rows=13 width=434) (actual time=0.019..0.067 rows=12 loops=1)" " -> Index Scan using chggroup_issue on changegroup cg (cost=0.00..19.73 rows=10 width=29) (actual time=0.009..0.013 rows=10 loops=1)" " Index Cond: (issueid = 10006::numeric)" " -> Index Scan using chgitem_chggrp on changeitem ci (cost=0.00..8.58 rows=3 width=411) (actual time=0.004..0.005 rows=1 loops=10)" " Index Cond: (groupid = cg.id)" "Total runtime: 0.153 ms" Using a prepared statement with a variable , I get a poor plan requiring a sequential scan prepare t2(real) as select CG.ID, CG.ISSUEID, CG.AUTHOR, CG.CREATED, CI.ID, CI.FIELDTYPE, CI.FIELD, CI.OLDVALUE, CI.OLDSTRING, CI.NEWVALUE, CI.NEWSTRING from PUBLIC.CHANGEGROUP CG inner join PUBLIC.CHANGEITEM CI on CG.ID = CI.GROUPID where CG.ISSUEID=$1 order by CG.CREATED asc, CI.ID asc; explain analyze execute t2 (10006); "Sort (cost=126448.89..126481.10 rows=12886 width=434) (actual time=1335.615..1335.616 rows=12 loops=1)" " Sort Key: cg.created, ci.id" " Sort Method: quicksort Memory: 29kB" " -> Nested Loop (cost=0.00..125569.19 rows=12886 width=434) (actual time=0.046..1335.556 rows=12 loops=1)" " -> Seq Scan on changegroup cg (cost=0.00..44709.26 rows=10001 width=29) (actual time=0.026..1335.460 rows=10 loops=1)" " Filter: ((issueid)::double precision = $1)" " -> Index Scan using chgitem_chggrp on changeitem ci (cost=0.00..8.05 rows=3 width=411) (actual time=0.007..0.008 rows=1 loops=10)" " Index Cond: (groupid = cg.id)" "Total runtime: 1335.669 ms" Using a prepared statement with a cast of the variable to the right type, I get the good plan back prepare t2(real) as select CG.ID, CG.ISSUEID, CG.AUTHOR, CG.CREATED, CI.ID, CI.FIELDTYPE, CI.FIELD, CI.OLDVALUE, CI.OLDSTRING, CI.NEWVALUE, CI.NEWSTRING from PUBLIC.CHANGEGROUP CG inner join PUBLIC.CHANGEITEM CI on CG.ID = CI.GROUPID where CG.ISSUEID=cast($1 as numeric) order by CG.CREATED asc, CI.ID asc; explain analyze execute t2 (10006); "Sort (cost=106.19..106.22 rows=13 width=434) (actual time=0.155..0.156 rows=12 loops=1)" " Sort Key: cg.created, ci.id" " Sort Method: quicksort Memory: 29kB" " -> Nested Loop (cost=0.00..105.95 rows=13 width=434) (actual time=0.048..0.111 rows=12 loops=1)" " -> Index Scan using chggroup_issue on changegroup cg (cost=0.00..19.73 rows=10 width=29) (actual time=0.031..0.042 rows=10 loops=1)" " Index Cond: (issueid = ($1)::numeric)" " -> Index Scan using chgitem_chggrp on changeitem ci (cost=0.00..8.58 rows=3 width=411) (actual time=0.006..0.006 rows=1 loops=10)" " Index Cond: (groupid = cg.id)" "Total runtime: 0.203 ms" Now the challenge is to get java/jdbc to get this done right. We make a big effort to ensure we always use prepared statements and variable bindings to help protect from SQL injection vulnerabilities. On 01/06/12 09:08, Craig James wrote: > I use Perl, not JDBC, but this thread may be relevant to your problem. > > http://postgresql.1045698.n5.nabble.com/Slow-statement-when-using-JDBC-td3368379.html > >