Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id B9D10B5DBBC for ; Mon, 16 May 2011 08:16:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 11304-02 for ; Mon, 16 May 2011 11:16:47 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from nm6.bullet.mail.sp2.yahoo.com (nm6.bullet.mail.sp2.yahoo.com [98.139.91.76]) by mail.postgresql.org (Postfix) with SMTP id 51E48B5DBD7 for ; Mon, 16 May 2011 08:16:45 -0300 (ADT) Received: from [98.139.91.65] by nm6.bullet.mail.sp2.yahoo.com with NNFMP; 16 May 2011 11:16:45 -0000 Received: from [98.139.91.13] by tm5.bullet.mail.sp2.yahoo.com with NNFMP; 16 May 2011 11:16:45 -0000 Received: from [127.0.0.1] by omp1013.mail.sp2.yahoo.com with NNFMP; 16 May 2011 11:16:45 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 378052.55051.bm@omp1013.mail.sp2.yahoo.com Received: (qmail 93959 invoked by uid 60001); 16 May 2011 11:16:44 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1305544604; bh=lJdRP7rPw5CjTqTNKuVZSAFmZd6JVoCpyR+BtBbbwJY=; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=mJk9pBSxULT7HnmMWZ7oSh5O/M8w7ifRErv5yVDjJVjBMBrBpHx6tMH1z0dkbvrp91jm1W2chRKP3ghf7nA4hggLtYrIv6/zrNftyTFo9P/+Kkclt8e1/bYqku77pwC/aOvU1ZNF+1PeR+/M06D6ttwliArxo6/ozJSunTKKeUM= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=Message-ID:X-YMail-OSG:Received:X-Mailer:References:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type; b=vGPRV5JTKYQpytviNeSstQ2N0cSRbBhqnyufWWh7xUZSVP4AZpaC8ywn/z4AyLWmKASNDvccG7CiLjieTm/Qlo7Ulkm/jV1Vg38tKKOVEwSqS0QhfgEHYqWDdfEklGvdhTlQo30S+qE3YtdFLZW1KczKyx+o3hOmmbVzH/Ze6Ag=; Message-ID: <630076.87313.qm@web112408.mail.gq1.yahoo.com> X-YMail-OSG: 2bAB8.kVM1kTSEm1sYvUOCL_3OCekZtjWg8.sbaRvTpsOyi n1KPvayKwvWOQQk7j6t2SBTcISVdWK7PgrVRUZxP57rKg3Mi72j2bpavt8ym S5poHVq8mKGJtCQycoAjURV2KA.VojiLSyyYrlPnFJm4EPm3_Daq_ksBSiW7 wtTVgfppYR.ytzvLuayqbLum9IaIApSfunL0LTv.EoOzNt80RqJ.XBSmt35u n1LsgMtgZo0pHz15rMdCrCWrujEN.jJbt7yWPKRLkIAtH5j9Og47ZgVHjuHO zgeGQFpRBUMZopRoXqIv4GpZZQ0tP77sYtKH1c38DryR601gFksJ9LpIifBz j197mnXgUzvA8DU3wxf6SbACdhGBZSmezSYY823aY7GiB3Z87Vt_p1rJ0ke6 PIvy25ig88zPQBg-- Received: from [88.203.16.8] by web112408.mail.gq1.yahoo.com via HTTP; Mon, 16 May 2011 04:16:44 PDT X-Mailer: YahooMailWebService/0.8.111.303096 References: <4DD0B8AD.3020906@orkash.com> <4DD0F5A3.1060007@postnewspapers.com.au> Date: Mon, 16 May 2011 04:16:44 -0700 (PDT) From: Denis de Bernardy Reply-To: Denis de Bernardy Subject: Re: Why query takes soo much time To: "pgsql-performance@postgresql.org" In-Reply-To: <4DD0F5A3.1060007@postnewspapers.com.au> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="0-1443786998-1305544604=:87313" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.907 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RFC_ABUSE_POST=0.001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/237 X-Sequence-Number: 43746 --0-1443786998-1305544604=:87313 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable [big nestloop with a huge number of rows]=0A=0AYou're in an edge case, and = I doubt you'll get things to run much faster: you want the last 1k rows out= of an 18M row result set... It will be slow no matter what you do.=0A=0AWh= at the plan is currently doing, is it's going through these 18M rows using = a for each loop, until it returns the 1k requested rows. Without the offset= , the plan is absolutely correct (and quite fast, I take it). With the enor= mous offset, it's a different story as you've noted.=0A=0AAn alternative pl= an could have been to hash join the tables together, to sort the result set= , and to apply the limit/offset on the resulting set.=A0You can probably fo= rce the planner to do so by rewriting your statement using a with statement= , too:=0A=0AEXPLAIN ANALYZE=0AWITH rows AS (=0ASELECT c.clause, s.subject ,= s.object , s.verb, s.subject_type, s.object_type ,s.doc_id ,s.svo_id=A0=0AF= ROM clause2 c INNER JOIN svo2 s ON (c.clause_id=3Ds.clause_id AND c.source_= id=3Ds.doc_id AND c.sentence_id=3Ds.sentence_id)=0A=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 INNER JOIN page_content p ON (s.doc_id=3Dp.crawled_page_= id)=0A)=0ASELECT *=0AFROM rows=0AORDER BY svo_id limit 1000 offset 17929000= =0A=0A=0AI've my doubts that it'll make much of a different, though: you'll= still be extracting the last 1k rows out of 18M.=0A=0AD=0A --0-1443786998-1305544604=:87313 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable
[big nestl= oop with a huge number of rows]

You're in an edge case, and I doubt you'll get things to run much f= aster: you want the last 1k rows out of an 18M row result set... It will be= slow no matter what you do.

<= span>What the plan is currently doing, is it's going through these 18M rows= using a for each loop, until it returns the 1k requested rows. Without the= offset, the plan is absolutely correct (and quite fast, I take it). With t= he enormous offset, it's a different story as you've noted.

An alternative plan could have been to hash j= oin the tables together, to sort the result set, and to apply the limit/off= set on the resulting set. You can probably force the planner to do so by rewriting your statement using a with statement, too:
<= span>
EXPLAIN ANALYZE
WITH rows = AS (
SELECT c.clause, s.subject ,s.object , s.verb, s.subject_type, s.ob= ject_type ,s.doc_id ,s.svo_id 
FROM clause2 c INNER JOIN svo2 s ON = (c.clause_id=3Ds.clause_id AND c.source_id=3Ds.doc_id AND c.sentence_id=3Ds= .sentence_id)
         &nbs= p;     INNER JOIN page_content p ON (s.doc_id=3Dp.crawl= ed_page_id)
)
SELECT *=
FROM rows
ORDER BY svo_id limit 1000 offset 17929000

=

I've my doubts that it'll make much = of a different, though: you'll still be extracting the last 1k rows out of = 18M.

D
=
--0-1443786998-1305544604=:87313--