Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D4C21B5DBD6 for ; Mon, 16 May 2011 11:16:07 -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 24363-05 for ; Mon, 16 May 2011 14:16:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id BDE43B5D942 for ; Mon, 16 May 2011 11:16:00 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p4GEFxEb015164 for ; Mon, 16 May 2011 10:15:59 -0400 (EDT) To: "pgsql-performance@postgresql.org" Subject: Re: Why query takes soo much time In-reply-to: <630076.87313.qm@web112408.mail.gq1.yahoo.com> References: <4DD0B8AD.3020906@orkash.com> <4DD0F5A3.1060007@postnewspapers.com.au> <630076.87313.qm@web112408.mail.gq1.yahoo.com> Comments: In-reply-to Denis de Bernardy message dated "Mon, 16 May 2011 04:16:44 -0700" Date: Mon, 16 May 2011 10:15:59 -0400 Message-ID: <15163.1305555359@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-10 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/240 X-Sequence-Number: 43749 Denis de Bernardy writes: > An alternative plan could have been to hash join the tables together, > to sort the result set, and to apply the limit/offset on the resulting > set. Indeed. I rather wonder why the planner didn't do that to start with. This plan looks to me like it might be suffering from insufficient work_mem to allow use of a hash join. Or possibly the OP changed some of the cost_xxx or enable_xxx settings in a misguided attempt to force it to use indexes instead. As a rule of thumb, whole-table joins probably ought not be using nestloop plans, and that frequently means that indexes are worthless for them. But in any case, as Craig noted, the real elephant in the room is the huge OFFSET value. It seems likely that this query is not standing alone but is meant as one of a series that's supposed to provide paginated output, and if so the total cost of the series is just going to be impossible no matter what. The OP needs to think about using a cursor or some such to avoid repeating most of the work each time. regards, tom lane