X-Original-To: pgsql-performance-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id B600C9FA4E0 for ; Wed, 26 Jul 2006 17:25:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 81058-01 for ; Wed, 26 Jul 2006 17:25:20 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 140859FA004 for ; Wed, 26 Jul 2006 17:24:50 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k6QKOlVR017742; Wed, 26 Jul 2006 16:24:47 -0400 (EDT) To: arnaulist@andromeiberica.com cc: pgsql-performance@postgresql.org Subject: Re: Is it possible to speed this query up? In-reply-to: <44C79FED.5080702@andromeiberica.com> References: <44C79FED.5080702@andromeiberica.com> Comments: In-reply-to Arnau message dated "Wed, 26 Jul 2006 19:01:33 +0200" Date: Wed, 26 Jul 2006 16:24:47 -0400 Message-ID: <17741.1153945487@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.04 tagged_above=0 required=5 tests=AWL, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200607/240 X-Sequence-Number: 20100 Arnau writes: > the explain analyze shouts the following: The expensive part appears to be this indexscan: > -> Index Scan using pk_agndusrgrp_usergroup on > agenda_users_groups ug (cost=0.00..123740.26 rows=2936058 width=30) > (actual time=0.101..61921.260 rows=2836638 loops=1) Since there's no index condition, the planner is evidently using this scan just to obtain sort order. I think ordinarily it would use a seqscan and then sort the final result, which'd be a lot faster if the whole result were being selected. But you have a LIMIT and it's mistakenly guessing that only a small part of the table will need to be scanned before the LIMIT is satisfied. Bottom line: try dropping the LIMIT. If you really need the limit to be enforced on the SQL side, you could try declaring the query as a cursor and only fetching 150 rows from it. regards, tom lane