Received: from candle.pha.pa.us (216-55-132-35.dsl.san-diego.abac.net [216.55.132.35]) by postgresql.org (Postfix) with ESMTP id 5F79E47598F for ; Fri, 19 Jul 2002 14:28:11 -0400 (EDT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.10.1) id g6JIRw603603; Fri, 19 Jul 2002 14:27:58 -0400 (EDT) From: Bruce Momjian Message-Id: <200207191827.g6JIRw603603@candle.pha.pa.us> Subject: Re: Two Index Questions In-Reply-To: <200207191112.49885.josh@agliodbs.com> To: josh@agliodbs.com Date: Fri, 19 Jul 2002 14:27:58 -0400 (EDT) Cc: Bruno Wolff III , pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL99 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Archive-Number: 200207/308 X-Sequence-Number: 8686 Josh Berkus wrote: > Bruno, Tom, > > > Typo. I meant to say columns. The issue is that max doesn't use an index, > > but if there are a lot of different values of G for a given A, B and C, > > it may be better to use an index then to search through the applicable > > rows to find the maximum. > > That's odd ... you're correct. Tom, why doesn't MAX() use an index? I > understand why indexes are generally useless for SUM(), AVG, and COUNT, but > it seems that MAX() and MIN() should *always* use an index. Index access methods don't know about aggregates, and our type system makes such linkage difficult. The FAQ does have: However, LIMIT combined with ORDER BY often will use an index because only a small portion of the table is returned. In fact, though MAX() and MIN() don't use indexes, it is possible to retrieve such values using an index with ORDER BY and LIMIT:
	    SELECT col
	    FROM tab
	    ORDER BY col [ DESC ]
	    LIMIT 1
	
-- Bruce Momjian | http://candle.pha.pa.us pgman@candle.pha.pa.us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026