Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id F148A64FCAC for ; Mon, 24 Nov 2008 15:23:14 -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 03550-08 for ; Mon, 24 Nov 2008 15:23:12 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from qb-out-0506.google.com (qb-out-0506.google.com [72.14.204.228]) by mail.postgresql.org (Postfix) with ESMTP id 249C064FC6F for ; Mon, 24 Nov 2008 15:23:12 -0400 (AST) Received: by qb-out-0506.google.com with SMTP id d8so2257284qbc.23 for ; Mon, 24 Nov 2008 11:23:11 -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:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=wsxOh3z2dayZECGSjTmwTcgXvagR1aKlOO7LJfJZV0s=; b=j3gqmPB7pL8UwwswBEE5r107ObPFXLVPvHRe5yQXbAObc17XHyVcaMbFGqyzdQxdiY e7dBRmjDuCYb9VxsxdTPRWj+UlRcZycEwCJ3WDUj2ZP2YEtQ1awk7IcQDnsJa2Q8agWT qstsXo6aeNVWajupw/9+4R27Cydj1x04s9mDQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=amjKPexYALwe3zkW62VV/k9RNlIKwyr1YJzm+SalmDgxGxCf2PvHuSUL0qkDXB0LAG /eYWoHhw6n9m+6OaPT+71oepQLl+TVeLQ1e4epPqIhMQteOnMVvHONxH2/jKwDbqxAHo GeggpGDwg+2r2eINO0va/+ILt5Sp8V/CiRU/E= Received: by 10.114.181.6 with SMTP id d6mr2083865waf.8.1227554590758; Mon, 24 Nov 2008 11:23:10 -0800 (PST) Received: by 10.114.137.17 with HTTP; Mon, 24 Nov 2008 11:23:10 -0800 (PST) Message-ID: Date: Mon, 24 Nov 2008 12:23:10 -0700 From: "Scott Marlowe" To: Andrus Subject: Re: limit clause produces wrong query plan Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <0e6967e0-fb33-49aa-a745-a1d11ef6a9e0@q9g2000yqc.googlegroups.com> 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/238 X-Sequence-Number: 31969 On Mon, Nov 24, 2008 at 10:26 AM, Andrus wrote: >> it was veery fast. To be honest I do not know what is happening?! > > This is really weird. > It seems that PostgreSql OFFSET / LIMIT are not optimized and thus typical > paging queries 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 pageno*100 LIMIT 100 This will get progressively slower as pageno goes up. > SELECT ... FROM bigtable ORDER BY intprimarykey OFFSET 0 LIMIT 100 That should be plenty fast. > cannot be used in PostgreSql at all for big tables. Can't be used in any real database with any real complexity to its query either. > Do you have any idea how to fix this ? 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;