X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5679652828 for ; Tue, 17 May 2005 23:16:23 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 77948-07 for ; Wed, 18 May 2005 02:16:13 +0000 (GMT) Received: from cougar.unisys.com.br (smtp2.unisys.com.br [200.220.0.118]) by svr1.postgresql.org (Postfix) with ESMTP id BF72352811 for ; Tue, 17 May 2005 23:16:11 -0300 (ADT) Received: from [192.168.0.1] ([200.155.197.32]) by cougar.unisys.com.br ; Tue, 17 May 2005 23:16:11 -0300 EST Message-ID: <428AA569.3010708@pobox.com> Date: Tue, 17 May 2005 23:16:09 -0300 From: Alain User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) X-Accept-Language: pt-br, pt MIME-Version: 1.0 To: Sql-Postgre Subject: Re: Does Postgresql have a similar pseudo-column "ROWNUM" as References: <20050517170636.GC12405@phlogiston.dyndns.org> <428A2E81.2060501@pobox.com> <1116353539.26520.5.camel@localhost.localdomain> <428A3B54.4070902@pobox.com> <1116367382.9362.11.camel@localhost.localdomain> In-Reply-To: <1116367382.9362.11.camel@localhost.localdomain> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Rcpt-To: X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/145 X-Sequence-Number: 21458 Ragnar Hafstaš escreveu: >>[how to solve the get next 100 records problem] > >>I tried that. It does not work in the generic case: 6 MegaRec, telephone >>listing, alphabetical order. > > lets say pkey is your primary key and skey is your sort key, and > there exists an index on (skey,pkey) > > your first select is > select ... from tab ORDER by skey,pkey LIMIT 100; > > your subsequent selects are > select ... from tab WHERE skey>skey_last > OR (skey=skey_last AND pkey>pkey_last) > ORDER BY skey,pkey > LIMIT 100 OFFSET 100; I tied that, it is veeery slow, probably due to the OR operand :( BUT, I think that this is close to a final solution, I made some preliminary test ok. Please tell me what you think about this. Fisrt let's state that I am reading records to put on a screen (in a Table/Grid). I separated the problem is *3* parts -first select is as above: select ... from tab ORDER by skey,pkey LIMIT 100; -second method for next 100: select ... from tab WHERE skey>=skey_last ORDER BY skey,pkey LIMIT 100; but here I test for repetitions using pkey and discard them -now if I get all repetitions or the last 100 have the same skey with the second method, I use select ... from tab WHERE skey=skey_last AND pkey>pkey_last ORDER BY skey,pkey LIMIT 100; until I get an empty response, then I go back to the second method. All queries are extremely fast with 6000000 records and it looks like the few redundant or empty queries (but very fast) will not be a problem. What is your opinion about this (apart that it is a bit complex :) ?? Alain