X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5C915567F7 for ; Wed, 16 Mar 2005 18:10:38 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 79540-06 for ; Wed, 16 Mar 2005 18:10:32 +0000 (GMT) Received: from davinci.ethosmedia.com (server227.ethosmedia.com [209.128.84.227]) by svr1.postgresql.org (Postfix) with ESMTP id A4B5D569CA for ; Wed, 16 Mar 2005 18:10:28 +0000 (GMT) X-EthosMedia-Virus-Scanned: no infections found Received: from [63.195.55.98] (account josh@agliodbs.com HELO spooky) by davinci.ethosmedia.com (CommuniGate Pro SMTP 4.1.8) with ESMTP id 7134725; Wed, 16 Mar 2005 10:12:20 -0800 From: Josh Berkus Organization: Aglio Database Solutions To: Daniel Crisan Subject: Re: multi-column index Date: Wed, 16 Mar 2005 10:09:24 -0800 User-Agent: KMail/1.6.2 Cc: PostgreSQL References: <42385A1B.9090305@ibcp.fr> In-Reply-To: <42385A1B.9090305@ibcp.fr> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200503161009.24713.josh@agliodbs.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.011 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200503/332 X-Sequence-Number: 11037 Daniel, > Table "public.descriptionprodftdiclnk" What is this, German? ;-) > explain analyze select * from descriptionprodftdiclnk where idword=44; > QUERY PLAN > --------------------------------------------------------------------------- >---------------------------------------------------- Seq Scan on > descriptionprodftdiclnk (cost=0.00..4788.14 rows=44388 width=8) (actual > time=87.582..168.041 rows=43792 loops=1) > Filter: (idword = 44) > Total runtime: 195.339 ms > (3 rows) > explain analyze select * from descriptionprodftdiclnk where idword=44; > > QUERY PLAN > --------------------------------------------------------------------------- >---------------------------------------------------------------------------- >------------ Index Scan using descriptionprodftdiclnk_pkey on > descriptionprodftdiclnk (cost=0.00..36720.39 rows=44388 width=8) > (actual time=0.205..73.489 rows=43792 loops=1) > Index Cond: (idword = 44) > Total runtime: 100.564 ms > (3 rows) > create index ix_tempIndex on descriptionprodftdiclnk(idword); > CREATE INDEX > explain analyze select * from descriptionprodftdiclnk where idword=44; > QUERY > PLAN > --------------------------------------------------------------------------- >---------------------------------------------------------------------- Index > Scan using ix_tempindex on descriptionprodftdiclnk > (cost=0.00..916.24 rows=44388 width=8) (actual time=0.021..79.879 > rows=43792 loops=1) > Index Cond: (idword = 44) > Total runtime: 107.081 ms > (3 rows) > > Could someone provide an explanation for the planner's behaviour? Pretty simple, really. Look at the cost calculations for the index scan for the multi-column index. PostgreSQL believes that: The cost of a seq scan is 4788.14 The cost of an 2-column index scan is 36720.39 The cost of a 1-column index scan is 916.24 Assuming that you ran each of these queries multiple times to eliminate caching as a factor, the issue is that the cost calculations are wrong. We give you a number of GUC variables to change that: effective_cache_size random_page_cost cpu_tuple_cost etc. See the RUNTIME-CONFIGURATION docs for more details. -- Josh Berkus Aglio Database Solutions San Francisco