X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (neptune.hub.org [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 794C9D1B440 for ; Mon, 5 Jan 2004 10:07:28 +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 42813-05 for ; Mon, 5 Jan 2004 06:06:41 -0400 (AST) Received: from pns.mm.eutelsat.org (pns.mm.eutelsat.org [194.214.173.227]) by svr1.postgresql.org (Postfix) with ESMTP id C4A42D1B4CD for ; Mon, 5 Jan 2004 06:06:36 -0400 (AST) Received: from nts-03.mm.eutelsat.org (localhost [127.0.0.1]) by pns.mm.eutelsat.org (8.11.6/linuxconf) with ESMTP id i05AAfw01114; Mon, 5 Jan 2004 11:10:42 +0100 Received: from bigfoot.com (accesspoint.mm.eutelsat.org [194.214.173.4]) by nts-03.mm.eutelsat.org (8.11.6/linuxconf) with ESMTP id i04Bo4R16449; Sun, 4 Jan 2004 12:50:04 +0100 Message-ID: <3FF800C9.4030903@bigfoot.com> Date: Sun, 04 Jan 2004 13:02:17 +0100 From: Gaetano Mendola User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6b) Gecko/20031206 Thunderbird/0.4 X-Accept-Language: en-us, en MIME-Version: 1.0 To: "pgsql-admin@postgresql.org" Cc: Joseph Lemm Subject: Re: what is the cause that scan type is showing as 'seq scan' after References: <20040104053020.59145.qmail@web40909.mail.yahoo.com> In-Reply-To: <20040104053020.59145.qmail@web40909.mail.yahoo.com> 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: 200401/28 X-Sequence-Number: 11891 Joseph Lemm wrote: > IN RELATION TO THIS POST: > > On Sun, Jan 26, 2003 at 06:31:50PM +0530, shreedhar wrote: > >>Hi All, >> >>Before indexing query plan was showing cost as 40.00, after indexing query >>plan again showing as 'seq scan' and cost as 3060.55. >>The field which i indexed is primary key to this table. >>May i know >>1) what is the cause that scan type is showing as 'seq scan' after indexing >>also >>2) why it is showing cost as high value compare to previous. > > > TO WHICH ROSS REPLIED: > > >>You trimmed out the other parts of the EXPLAIN, so I'm just guessing, >>but that cost seems suspiciously round: I'm guessing that you haven't >>run VACUUM ANALYZE at all. One thing indexing does is update the 'number >>of tuples' statistic. See the archives for why sequential scans still >>show up (short answer: index scans aren't free, so at some point, it's >>cheaper to scan the entire table than to scan both the index and the >>subset of the table returned) > > > > > OK, so then what is the explanation for this: > > Table "public.post" > Column | Type | Modifiers > --------+-----------------------------+----------- > id | integer | > author | character varying(80) | > text | text | > hidden | boolean | > date | timestamp without time zone | > host | character varying(80) | > Indexes: idx_post_id unique btree (id), > post_author_index btree (author) > > > VACUUM ANALYZE; > VACUUM > > EXPLAIN ANALYZE select host from post where author='George'; > QUERY PLAN > > -------------------------------------------------------------------------------------------------------- > Seq Scan on post (cost=0.00..2869.30 rows=1768 width=27) (actual > time=0.23..520.65 rows=1774 loops=1) > Filter: (author = 'George'::character varying) > Total runtime: 525.77 msec > (3 rows) > > So the optimizer decided it's less costly to do a sequential scan here than use > the index, right? > > > Now: > > SET ENABLE_SEQSCAN=OFF; > > EXPLAIN ANALYZE select host from post where author='George'; > QUERY PLAN > ----------------------------------------------------------------------------------------------------------------------------------- > Index Scan using post_author_index on post (cost=0.00..5253.63 rows=1768 > width=27) (actual time=28.92..210.25 rows=1774 loops=1) > Index Cond: (author = 'George'::character varying) > Total runtime: 215.00 msec > (3 rows) > > > So if I force an index scan, I get much better performance (215 vs 525 msec). > Does this mean that the optimizer screwed up when it recommended a sequential > scan? No this mean that you are instructing your optimizer in a wrong way. Show us your configuration file and in particular these parameters: effective_cache_size random_page_cost cpu_tuple_cost cpu_index_tuple_cost cpu_operator_cost I use these value, that are good enough for a medium HW: effective_cache_size = 20000 random_page_cost = 2.5 cpu_tuple_cost = 0.005 cpu_index_tuple_cost = 0.0005 cpu_operator_cost = 0.0025 Regards Gaetano Mendola