Received: from sd.tpf.co.jp (sd.tpf.co.jp [210.161.239.34]) by hub.org (8.8.8/8.8.8) with ESMTP id EAA25814 for ; Tue, 20 Oct 1998 04:21:22 -0400 (EDT) (envelope-from Inoue@tpf.co.jp) Received: from cadzone ([126.0.1.40]) by sd.tpf.co.jp (2.0 Build 2131 (Berkeley 8.8.4)/8.8.4) with SMTP id RAA03272; Tue, 20 Oct 1998 17:12:15 +0900 From: "Hiroshi Inoue" To: "Bruce Momjian" , Cc: Subject: RE: [HACKERS] What about LIMIT in SELECT ? Date: Tue, 20 Oct 1998 17:24:09 +0900 Message-ID: <000601bdfc03$02e67100$2801007e@cadzone.tpf.co.jp> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 In-Reply-To: <199810160534.BAA29942@candle.pha.pa.us> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Importance: Normal > > I know the good part of the posting is coming. > > > Last with the btree index on key and the patched backend. > > This time the plan is a plain IndexScan because the ORDER BY > > clause exactly matches the sort order of the chosen index. > > S1 needs 13 seconds and S2 less than 0.2! This dramatic > > speedup comes from the fact, that this time the index scan is > > the toplevel executor node and the executor run is stopped > > after 20 tuples have been selected. > > OK, seems like in the S1 case, the use of the psort/ORDER BY code on top > of the index was taking and extra 3 seconds, which is 23%. That is a > lot more than I thought for the psort code, and shows we could gain a > lot by removing unneeded sorts from queries that are already using > matching indexes. > > Just for clarity, added to TODO. I think everyone is clear on this one, > and its magnitude is a surprise to me: > > * Prevent psort() usage when query already using index matching ORDER BY > > I can't find the reference to descending order cases except my posting. If we use an index scan to remove sorts in those cases,backward positioning and scanning are necessary. > > Analysis of the above timings: > > > > If there is an ORDER BY clause, using an index scan is the > > clever way if the indexqual dramatically reduces the the > > amount of data selected and sorted. I think this is the > > normal case (who really selects nearly all rows from a 5M row > > table?). So choosing the index path is correct. This will > > hurt if someone really selects most of the rows and the index > > scan jumps over the disc. But here the programmer should use > > an unqualified query to perform a seqscan and do the > > qualification in the frontend application. > > Fortunately, the optimizer already does the index selection for us, and > guesses pretty well if the index or sequential scan is better. Once we > implement the above removal of psort(), we will have to change the > timings because now you have to compare index scan against sequential > scan AND psort(), because in the index scan situation, you don't need > the psort(), assuming the ORDER BY matches the index exactly. > Let t be a table with 2 indices, index1(key1,key2), index2(key1,key3). i.e. key1 is common to index1 and index2. And for the query select * from t where key1>....; If PosgreSQL optimizer choose [ index scan on index1 ] we can't remove sorts from the following query. select * from t where key1>... order by key1,key3; Similarly if [ index scan on index2 ] are chosen we can't remove sorts from the following query. select * from t where key1>... order by key1,key2; But in both cases (clever) optimizer can choose another index for scan. Thanks. Hiroshi Inoue Inoue@tpf.co.jp