Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 4B17A6338CA for ; Fri, 3 Jul 2009 10:40:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 15285-07 for ; Fri, 3 Jul 2009 10:40:53 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-vw0-f173.google.com (mail-vw0-f173.google.com [209.85.212.173]) by mail.postgresql.org (Postfix) with ESMTP id B05596338F1 for ; Fri, 3 Jul 2009 10:40:53 -0300 (ADT) Received: by vwj3 with SMTP id 3so1347759vwj.15 for ; Fri, 03 Jul 2009 06:40:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=GUKgBhNtYYH7v3oDmbGklc68zt+hqiI1ZHRxFtf1Ycs=; b=wahOwGl9fcW3qwQsr7XDt1EPG6YFkuHF6xpL88pxFq8uKOSwDtTTMssSBI2uR/Yx98 h9pEX9dMzYinEeZADL3IrghahTuk8o9aPnF4s4mFvck21sIbbwuS+eZrCUNkgwLFCOw/ jd+kkhUBmIpbsGlq9LHRW0xU719kEBxbd4K+0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=BT3gqlnPa4hI+a4pQU0URJ4M6I3gGPChLXfwMGaB28oOVeHD1v442w4/gFto4NSWt5 5hFoJ+Ae8/p1saiDYvYE3jebDK9OApzf7HOc1MOJRdwGWGQ1p0PGcuOtOvS1vj7WegS1 gRbyLEaBPcMH5U2JCFRVYbZzf4awU0vcf0JUo= MIME-Version: 1.0 Received: by 10.220.94.21 with SMTP id x21mr2626073vcm.88.1246628452225; Fri, 03 Jul 2009 06:40:52 -0700 (PDT) In-Reply-To: <1b795e7b0907030639r562a9006g4c3b286bce3e5d7b@mail.gmail.com> References: <1b795e7b0907030546o7e44ebcdteb1168c9d6680380@mail.gmail.com> <1b795e7b0907030601x53ad69efk5412acc1a2595c61@mail.gmail.com> <4A4E04D1.5010905@opencloud.com> <1b795e7b0907030639r562a9006g4c3b286bce3e5d7b@mail.gmail.com> Date: Fri, 3 Jul 2009 14:40:52 +0100 Message-ID: <1b795e7b0907030640m347931c1v64e6039b9b8590b0@mail.gmail.com> Subject: Cursor-based results: bafflingly slow From: Robin Houston To: pgsql-jdbc@postgresql.org Content-Type: multipart/alternative; boundary=001636283db80fd447046dcd4e64 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200907/6 X-Sequence-Number: 19774 --001636283db80fd447046dcd4e64 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 2009/7/3 Oliver Jowett : > They're not really equivalent. A portal with a limited fetchsize is more > like DECLARE CURSOR, not LIMIT. > > What does the plan look like if you plan it without the LIMIT? Hmm, well... If I do the same as before, but without the limit, then the plan is: Sort (cost=353822.56..353835.72 rows=5264 width=612) Sort Key: polypeptide.feature_id InitPlan -> Seq Scan on organism (cost=0.00..1.95 rows=1 width=4) Filter: ((common_name)::text = $1) -> Nested Loop (cost=88664.31..352053.24 rows=5264 width=612) -> Hash Join (cost=88663.00..351946.66 rows=188 width=96) Hash Cond: (featureprop.feature_id = polypeptide.feature_id) -> Hash Join (cost=2790.36..264059.63 rows=51268 width=96) Hash Cond: (featureprop.type_id = featureprop_type.cvterm_id) -> Seq Scan on featureprop (cost=0.00..222284.97 rows=10259097 width=56) -> Hash (cost=2785.51..2785.51 rows=388 width=52) -> Seq Scan on cvterm featureprop_type (cost=0.00..2785.51 rows=388 width=52) Filter: (cv_id = cv_id) -> Hash (cost=85344.24..85344.24 rows=32192 width=4) -> Bitmap Heap Scan on feature polypeptide (cost=827.57..85344.24 rows=32192 width=4) Recheck Cond: ((organism_id = $0) AND (type_id = 191)) -> Bitmap Index Scan on feature_genedb_idx1 (cost=0.00..819.52 rows=32192 width=0) Index Cond: ((organism_id = $0) AND (type_id = 191)) -> Materialize (cost=1.31..1.59 rows=28 width=516) -> Seq Scan on cv featureprop_type_cv (cost=0.00..1.28 rows=28 width=516) Surely the server is not so stupid as to use an unnecessary server-side sort, if I've explicitly asked to fetch the rows 100 at a time? Is there any easy way to find out what execution plan the server is actually using? I suppose I could ask the DBAs to enable auto_explain on the dev database. If this *is* the problem, do you know any way round it. Obviously I want the plan that *doesn't* involve sorting everything before returning anything. If I explicitly create a cursor, with no limit, then it does return the first 100 rows very quickly. On the other hand, I have to hard-code the parameter this way (because I don't know any way, in psql, to use a bind variable with an explicit cursor.) begin; declare polypeptide_props_c no scroll cursor for [...query...] fetch forward 100 from polypeptide_props_c; rollback; This fetch completes in 31.313 ms. Thanks for your help so far! Robin --001636283db80fd447046dcd4e64 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
2009/7/3 Oliver Jowett <oliver@opencloud.com>:

> They're not really equivalent. A portal with a limite= d fetchsize is more
> like DECLARE CURSOR, not LIMIT.
>
> What does the plan look like if you plan it without the LIMIT?=

Hmm, well... If I do the same as before, but without the limi= t, then the plan is:

Sort =A0(cost=3D353822.56..353835.72 rows= =3D5264 width=3D612)
=A0 Sort Key: polypeptide.feature_id

=A0 InitPlan =A0 =A0 -> =A0Seq Scan on organism =A0(cost=3D0.00..1.95 rows=3D1 widt= h=3D4)
=A0 =A0 =A0 =A0 =A0 Filter: ((common_nam= e)::text =3D $1)
=A0 -> =A0Nested Loop =A0(cost=3D88664.31..35= 2053.24 rows=3D5264 width=3D612)
=A0 =A0 =A0 =A0 -> =A0Hash Join =A0(cost=3D88663.00..351946.66 rows=3D1= 88 width=3D96)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 Hash Cond: (featureprop.feat= ure_id =3D polypeptide.feature_id)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 -> = =A0Hash Join =A0(cost=3D2790.36..264059.63 rows=3D51268 width=3D96)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Hash Cond: (featureprop.type_id = =3D featureprop_type.cvterm_id)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= -> =A0Seq Scan on featureprop =A0(cost=3D0.00..222284.97 rows=3D1025909= 7 width=3D56)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Hash =A0= (cost=3D2785.51..2785.51 rows=3D388 width=3D52)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Seq Scan on c= vterm featureprop_type =A0(cost=3D0.00..2785.51 rows=3D388 width=3D52)
= =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Filter: (cv= _id =3D cv_id)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Hash =A0(cost=3D853= 44.24..85344.24 rows=3D32192 width=3D4)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Bitmap Heap Scan on featu= re polypeptide =A0(cost=3D827.57..85344.24 rows=3D32192 width=3D4)
=A0 = =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Recheck Cond: ((organism_id= =3D $0) AND (type_id =3D 191))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0= =A0 =A0 =A0 -> =A0Bitmap Index Scan on feature_genedb_idx1 =A0(cost=3D0= .00..819.52 rows=3D32192 width=3D0)

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Index Cond= : ((organism_id =3D $0) AND (type_id =3D 191))
=A0 =A0 =A0 =A0 -&= gt; =A0Materialize =A0(cost=3D1.31..1.59 rows=3D28 width=3D516)

=A0 =A0 =A0 =A0 =A0 =A0 =A0 -> =A0Seq Scan on cv featurepro= p_type_cv =A0(cost=3D0.00..1.28 rows=3D28 width=3D516)


Surely the server is not so stupid= as to use an unnecessary server-side sort, if I've explicitly asked to= fetch the rows 100 at a time?

Is there any easy w= ay to find out what execution plan the server is actually using? I suppose = I could ask the DBAs to enable auto_explain on the dev database.

If this *is* the problem, do you know any way round it.= Obviously I want the plan that *doesn't* involve sorting everything be= fore returning anything.

If I explicitly create a = cursor, with no limit, then it does return the first 100 rows very quickly.= On the other hand, I have to hard-code the parameter this way (because I d= on't know any way, in psql, to use a bind variable with an explicit cur= sor.)

begin;
declare polypeptide_props_c no sc= roll cursor for [...query...]
fetch forward 100 from polypeptide_= props_c;
rollback;

This fetch compl= etes in 31.313 ms.

Thanks for your help so far!

=
Robin

--001636283db80fd447046dcd4e64--