Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id 078112E003E for ; Tue, 13 May 2008 14:17:14 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 39342-06 for ; Tue, 13 May 2008 14:17:06 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from relay-1.mail.nethere.net (relay-1.mail.nethere.net [66.63.128.161]) by developer.postgresql.org (Postfix) with ESMTP id C687C2E003A for ; Tue, 13 May 2008 14:17:07 -0300 (ADT) Received: from scan-1.mail.nethere.net (scan-1.mail.nethere.net [66.63.128.132]) by relay-1.mail.nethere.net (Postfix) with ESMTP id 36D972905E; Tue, 13 May 2008 10:17:06 -0700 (PDT) Received: from mta-1.mail.nethere.net by scan-1.mail.nethere.net with LMTP id aPnMSD9-cgw9; Tue, 13 May 2008 10:17:06 -0700 (PDT) Received: from lisa-james-computer.local (wsip-68-224-174-233.sd.sd.cox.net [68.224.174.233]) by mta-1.mail.nethere.net (Postfix) with ESMTP id 0805EF1831; Tue, 13 May 2008 10:17:05 -0700 (PDT) Message-ID: <4829CD10.5070304@emolecules.com> Date: Tue, 13 May 2008 10:17:04 -0700 From: Craig James User-Agent: Thunderbird 2.0.0.14 (Macintosh/20080421) MIME-Version: 1.0 To: idc danny CC: pgsql-performance@postgresql.org Subject: Re: Problem with 11 M records table References: <684043.20585.qm@web35307.mail.mud.yahoo.com> In-Reply-To: <684043.20585.qm@web35307.mail.mud.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200805/170 X-Sequence-Number: 30138 idc danny wrote: > Hi everybody, > > I'm fairly new to PostgreSQL and I have a problem with > a query: > > SELECT * FROM "LockerEvents" LIMIT 10000 OFFSET > 10990000 > > The table LockerEvents has 11 Mlillions records on it > and this query takes about 60 seconds to complete. The OFFSET clause is almost always inefficient for anything but very small tables or small offsets. In order for a relational database (not just Postgres) to figure out which row is the 11000000th row, it has to actually retrieve the first 10999999 rows and and discard them. There is no magical way to go directly to the 11-millionth row. Even on a trivial query such as yours with no WHERE clause, the only way to determine which row is the 11 millionths is to scan the previous 10999999. There are better (faster) ways to achieve this, but it depends on why you are doing this query. That is, do you just want this one block of data, or are you scanning the whole database in 10,000-row blocks? Craig