X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 6017DD1B8AB for ; Fri, 29 Aug 2003 03:46:34 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 77874-04 for ; Fri, 29 Aug 2003 00:46:02 -0300 (ADT) Received: from mail.speakeasy.net (mail13.speakeasy.net [216.254.0.213]) by svr1.postgresql.org (Postfix) with ESMTP id 06613D1B8EA for ; Fri, 29 Aug 2003 00:46:02 -0300 (ADT) Received: (qmail 18314 invoked from network); 29 Aug 2003 03:46:00 -0000 Received: from unknown (HELO speakeasy.org) (kgeis@[66.93.130.165]) (envelope-sender ) by mail13.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 29 Aug 2003 03:46:00 -0000 Message-ID: <3F4ECC78.7090502@speakeasy.org> Date: Thu, 28 Aug 2003 20:46:00 -0700 From: Ken Geis User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030612 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bruno Wolff III Cc: pgsql-performance@postgresql.org Subject: Re: bad estimates References: <3F4E99F7.7080604@speakeasy.org> <20030829025109.GD4373@wolff.to> <3F4EBF7E.8020602@speakeasy.org> <20030829030156.GF4373@wolff.to> <3F4EC1D0.5010503@speakeasy.org> <20030829033818.GA5577@wolff.to> In-Reply-To: <20030829033818.GA5577@wolff.to> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/464 X-Sequence-Number: 3280 Bruno Wolff III wrote: > On Thu, Aug 28, 2003 at 20:00:32 -0700, > Ken Geis wrote: > >>Bruno Wolff III wrote: >> >>>>Not according to the optimizer! Plus, this is not guaranteed to return >>>>the correct results. >>> >>>For it to be fast you need an index on (stock_id, price_date) so that >>>you can use an index scan. >> >>I already said that such an index existed. In fact, it is the primary >>key of the table. And yes, I *am* analyzed! > > > Your original example didn't actually match that of the table you are showing > examples from. In that example the second half of the primary key was the > date not the end of the day price. If this is the case for the real table, > then that is the reason the distinct on doesn't help. I had obfuscated the table in the example and forgot to do the same with the query. Serves me right for thinking I care about that. A big problem is that the values I am working with are *only* the primary key and the optimizer is choosing a table scan over an index scan. That is why I titled the email "bad estimates." The table has (stock_id, price_date) as the primary key, and a bunch of other columns. What I *really* want to do efficiently is select stock_id, min(price_date), max(price_date) from day_ends group by stock_id; It is not the table or the query that is wrong. It is either the db parameters or the optimizer itself. Ken