Received: from sss.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.9.3/8.9.3) with ESMTP id QAA31464 for ; Sat, 16 Oct 1999 16:22:15 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.sss.pgh.pa.us (8.9.1/8.9.1) with ESMTP id QAA09461; Sat, 16 Oct 1999 16:20:31 -0400 (EDT) To: Ryszard Kurek cc: pgsql-interfaces@postgreSQL.org Subject: Re: [INTERFACES] Table viewer for big sorted tables In-reply-to: Your message of Sat, 16 Oct 1999 21:02:15 +0200 <19991016210215.A4354@fnet.pl> Date: Sat, 16 Oct 1999 16:20:30 -0400 Message-ID: <9459.940105230@sss.pgh.pa.us> From: Tom Lane Ryszard Kurek writes: > The slow version is: > SELECT min(product_symbol) FROM products WHERE product_name > 'current_product'; Try SELECT ... FROM products WHERE product_name > 'current_product' ORDER BY product_name LIMIT 1; You will need to have an index on product_name to make this fast --- without the index there will be a sort step. The ideal solution to your problem is probably SELECT ... FROM products ORDER BY product_name LIMIT 1 OFFSET n; which avoids issues like what happens when there is more than one row with the same product_name. However, 6.5.* is not bright enough to use an index for this kind of query (it only considers an index if there is a matching WHERE clause). 7.0 will be smarter. regards, tom lane