X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id EF31AD1E0EF for ; Wed, 18 Feb 2004 15:21:53 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 55160-06 for ; Wed, 18 Feb 2004 11:21:50 -0400 (AST) Received: from sss.pgh.pa.us (unknown [192.204.191.242]) by svr1.postgresql.org (Postfix) with ESMTP id 393FED1E0D5 for ; Wed, 18 Feb 2004 11:21:46 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i1IFLV4J023563; Wed, 18 Feb 2004 10:21:31 -0500 (EST) To: "David Witham" Cc: pgsql-sql@postgresql.org Subject: Re: Indexes and statistics In-reply-to: References: Comments: In-reply-to "David Witham" message dated "Wed, 18 Feb 2004 17:23:27 +1100" Date: Wed, 18 Feb 2004 10:21:31 -0500 Message-ID: <23562.1077117691@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/251 X-Sequence-Number: 16941 "David Witham" writes: > Does this make it a "wide" table? Nope. A rough estimate is that your rows will be about 160 bytes wide, which means you can fit about 50 per 8K page. So a query that needs to select 8% of the table will *on average* need to hit about 4 rows per page. In the absence of any data clumping this would certainly mean that the scan would need to touch every page anyway, and thus that using the index could provide no I/O savings. However, > The data arrives ordered by service_num, day, time. This customer has > one primary service_num that most of the calls are made from. So you do have very strong clumping, which the planner is evidently failing to account for properly. Could we see the pg_stats rows for service_num and cust_id? I'm curious whether the ANALYZE stats picked up the effect at all. As far as actually solving the problem is concerned, you have a few options. I wouldn't recommend turning off enable_seqscan globally, but you could perhaps turn it off locally (just do a SET command) just for this query. Another possibility, if you care a lot about the speed of this particular type of query, is to make a partial index tuned to the query: create index my_idx on cdr (cust_id) WHERE bill_id IS NULL; I gather from your previously shown results that "bill_id IS NULL" covers only a small fraction of the table, so this index would be pretty small and should look quite attractive to the planner. regards, tom lane