Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id A412764FCAC for ; Mon, 24 Nov 2008 16:05:09 -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 16153-01 for ; Mon, 24 Nov 2008 16:05:06 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtp-out.neti.ee (smtp-out.neti.ee [194.126.126.44]) by mail.postgresql.org (Postfix) with ESMTP id A697964FC6F for ; Mon, 24 Nov 2008 16:05:06 -0400 (AST) Received: from localhost (localhost [127.0.0.1]) by MXR-4.estpak.ee (Postfix) with ESMTP id BE70523F5CC; Mon, 24 Nov 2008 22:05:02 +0200 (EET) X-Virus-Scanned: Debian amavisd-new at Relay4.estpak.ee Received: from smtp-out.neti.ee ([127.0.0.1]) by localhost (MXR-4.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Uixk7R-6wLbC; Mon, 24 Nov 2008 22:05:02 +0200 (EET) Received: from Relayhost3.neti.ee (relayhost3.estpak.ee [88.196.174.169]) by MXR-4.estpak.ee (Postfix) with ESMTP id 2FF2923ECFE; Mon, 24 Nov 2008 22:05:02 +0200 (EET) X-SMTP-Auth-NETI-Businesmail: no Received: from andrusnotebook (unknown [195.50.198.194]) by Relayhost3.neti.ee (Postfix) with SMTP id 07EEAD4CDE; Mon, 24 Nov 2008 22:05:00 +0200 (EET) Message-ID: <372C245439584A6CBF49F996503BF162@andrusnotebook> From: "Andrus" To: "Scott Marlowe" Cc: References: <0e6967e0-fb33-49aa-a745-a1d11ef6a9e0@q9g2000yqc.googlegroups.com> In-Reply-To: Subject: Re: limit clause produces wrong query plan Date: Mon, 24 Nov 2008 22:04:54 +0200 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Windows Mail 6.0.6001.18000 X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6001.18049 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=STOX_REPLY_TYPE=0.001 X-Spam-Level: X-Archive-Number: 200811/242 X-Sequence-Number: 31973 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. >> SELECT ... FROM bigtable ORDER BY intprimarykey OFFSET 0 LIMIT 100 > > That should be plenty fast. The example which I posted shows that SELECT ... FROM bigtable ORDER BY intprimarykey LIMIT 100 this is extremely *slow*: seq scan is performed over whole bigtable. > A standard workaround is to use some kind of sequential, or nearly so, > id field, and then use between on that field. > > select * from table where idfield between x and x+100; Users can delete and insert any rows in table. This appoarch requires updating x in every row in big table after each insert, delete or order column change and is thus extremely slow. So I do'nt understand how this can be used for large tables. Andrus.