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 959E0D1B4E2 for ; Wed, 24 Sep 2003 02:33:42 +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 86879-01 for ; Tue, 23 Sep 2003 23:33:14 -0300 (ADT) Received: from grouse.mail.pas.earthlink.net (grouse.mail.pas.earthlink.net [207.217.120.116]) by svr1.postgresql.org (Postfix) with ESMTP id 14033D1B897 for ; Tue, 23 Sep 2003 23:33:10 -0300 (ADT) Received: from 0-1pool116-234.nas15.indianapolis1.in.us.da.qwest.net ([65.128.116.234] helo=nick) by grouse.mail.pas.earthlink.net with smtp (Exim 3.33 #1) id 1A1zSq-0007OV-00 for pgsql-performance@postgresql.org; Tue, 23 Sep 2003 19:33:13 -0700 Reply-To: From: "Nick Fankhauser" To: "Pgsql-Performance@Postgresql. Org" Subject: Re: How to make n_distinct more accurate. Date: Tue, 23 Sep 2003 21:32:24 -0500 Message-ID: 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 IMO, Build 9.0.2416 (9.0.2911.0) In-reply-to: X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Importance: Normal X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/299 X-Sequence-Number: 3665 The performance list seemed to be off-line for a while, so I posed the same question on the admin list and Tom Lane has been helping in that forum. -Nick > -----Original Message----- > From: pgsql-performance-owner@postgresql.org > [mailto:pgsql-performance-owner@postgresql.org]On Behalf Of Nick > Fankhauser > Sent: Monday, September 22, 2003 3:42 PM > To: Pgsql-Performance@Postgresql. Org > Subject: [PERFORM] How to make n_distinct more accurate. > > > Hi- > > I have a table- called "event" with a field event_date_time that > is indexed. > There are 1,700,000 rows in the table and 92,000 distinct values of > event_date_time with anywhere from 1 to 400 rows sharing the same > value. (I > did a count grouped by event_date_time & scanned it to get this info.) > > When I look at the pg_stats on this table, I always see 15,000 or lower in > the n_distinct column for event_date_time. (I re-ran analyze > several times & > then checked pg_stats to see if the numbers varied significantly.) > > Since this is off by about a factor of 6, I think the planner is > missing the > chance to use this table as the "driver" in a complex query plan that I'm > trying to optimize. > > So the question is- how can I get a better estimate of n_distinct from > analyze? > > If I alter the stats target as high as it will go, I get closer, but it > still shows the index to be about 1/2 as selective as it actually is: > > alpha=# alter table event alter column event_date_time set > statistics 1000; > ALTER TABLE > alpha=# analyze event; > ANALYZE > alpha=# select n_distinct from pg_stats where tablename='event' and > attname='event_date_time'; > n_distinct > ------------ > 51741 > (1 row) > > This number seems to be consistently around 51,000 if I re-run > analyze a few > times. > > I guess my question is two-part: > > (1)Is there any tweak to make this estimate work better? > > (2)Since I'm getting numbers that are consistent but way off, is > there a bug > here? > > (2-1/2) Or alternately, am I totally missing what n-distinct is > supposed to > denote? > > Thanks! > -Nick > > --------------------------------------------------------------------- > Nick Fankhauser > > nickf@doxpop.com Phone 1.765.965.7363 Fax 1.765.962.9788 > doxpop - Court records at your fingertips - http://www.doxpop.com/ > > > > ---------------------------(end of broadcast)--------------------------- > TIP 7: don't forget to increase your free space map settings >