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 DB4E2D1B8BB for ; Fri, 29 Aug 2003 03:59:32 +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 77860-06 for ; Fri, 29 Aug 2003 00:59:22 -0300 (ADT) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id A31E3D1B8BC for ; Fri, 29 Aug 2003 00:59:21 -0300 (ADT) Received: (qmail 5814 invoked by uid 500); 29 Aug 2003 04:05:19 -0000 Date: Thu, 28 Aug 2003 23:05:19 -0500 From: Bruno Wolff III To: Ken Geis Cc: pgsql-performance@postgresql.org Subject: Re: bad estimates Message-ID: <20030829040519.GA5746@wolff.to> Mail-Followup-To: Ken Geis , pgsql-performance@postgresql.org 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> <3F4ECC78.7090502@speakeasy.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3F4ECC78.7090502@speakeasy.org> User-Agent: Mutt/1.5.4i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/465 X-Sequence-Number: 3281 On Thu, Aug 28, 2003 at 20:46:00 -0700, Ken Geis wrote: > > 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. If you want both the max and the min, then things are going to be a bit more work. You are either going to want to do two separate selects or join two selects or use subselects. If there aren't enough prices per stock, the sequential scan might be fastest since you only need to go through the table once and don't have to hit the index blocks. It is still odd that you didn't get a big speed up for just the min though. You example did have the stock id and the date as the primary key which would make sense since the stock id and stock price on a day wouldn't be guarenteed to be unique. Are you absolutely sure you have a combined key on the stock id and the stock price?