Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 91947650331 for ; Mon, 24 Nov 2008 18:20:08 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 45421-05 for ; Mon, 24 Nov 2008 18:20:05 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ti-out-0910.google.com (ti-out-0910.google.com [209.85.142.190]) by mail.postgresql.org (Postfix) with ESMTP id A86C26502BB for ; Mon, 24 Nov 2008 18:20:04 -0400 (AST) Received: by ti-out-0910.google.com with SMTP id y8so1442370tia.5 for ; Mon, 24 Nov 2008 14:20:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=1lhv+RzpF4x8IDnFQAKr26daY9Wp2ygqcivl53NKCpc=; b=ZdJeIWtzBaMmbjCnxHW34sCQc6Lrwm5lfyV5RU/BtabBICc2ob7OUybYwgm8ZSsXRd UI6MlZ5avVwjXz8WU+WvcjdgbKbfuE8YrnzOmHUVVamRhZyCwfzpSuS6JRVqY12z8MfN u1OuMERem54nf+ore6U01cQdftYSxb6jZ9KRU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=DV+Y8d0nOvpffjr+1fhrlukCnp6uUfKBdK83gAjOTWHuNjDMVdROByV1YRKKUFJ4/v Xi37BBOvjlogUodQ/6qFi/69d+mxsSYdS5dUOIPHrIVMffko1rLgnQzlb7adT8op9VJy ihVh7zo0Im8h1BBJXlGZEoPzd9QnHrSJYBNHQ= Received: by 10.110.41.17 with SMTP id o17mr5560575tio.10.1227565201951; Mon, 24 Nov 2008 14:20:01 -0800 (PST) Received: from ?192.168.0.70? (office.squiz.net [203.206.128.174]) by mx.google.com with ESMTPS id d1sm2664643tid.15.2008.11.24.14.19.59 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 24 Nov 2008 14:20:01 -0800 (PST) Message-ID: <492B288B.9070308@gmail.com> Date: Tue, 25 Nov 2008 09:19:55 +1100 From: Chris User-Agent: Thunderbird 2.0.0.17 (X11/20080925) MIME-Version: 1.0 To: Andrus CC: Scott Marlowe , pgsql-performance@postgresql.org Subject: Re: limit clause produces wrong query plan References: <0e6967e0-fb33-49aa-a745-a1d11ef6a9e0@q9g2000yqc.googlegroups.com> <372C245439584A6CBF49F996503BF162@andrusnotebook> In-Reply-To: <372C245439584A6CBF49F996503BF162@andrusnotebook> Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/246 X-Sequence-Number: 31977 Andrus wrote: > Scott, > >> And how exactly should it be optimized? If a query is even moderately >> interesting, with a few joins and a where clause, postgresql HAS to >> create the rows that come before your offset in order to assure that >> it's giving you the right rows. > > SELECT ... FROM bigtable ORDER BY intprimarykey OFFSET 100 LIMIT 100 > > It should scan primary key in index order for 200 first keys and > skipping first 100 keys. ... which if you have a lot of table joins, unions/intersects/whatever else, should be done on which field and how? For a query like: select * t1 join t2 using (id) where t1.id='x' order by t1.id limit 100; it has to join the tables first (may involve a seq scan) to make sure the id's match up, reduce the number of rows to match the where clause (may/may not be done first, I don't know) - the limit is applied last. it can't grab the first 100 entries from t1 - because they might not have a matching id in t2, let alone match the where clause. -- Postgresql & php tutorials http://www.designmagick.com/