X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id CFACADB2BB for ; Thu, 10 Nov 2005 20:32:45 -0400 (AST) 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 11431-07 for ; Fri, 11 Nov 2005 00:32:42 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by svr1.postgresql.org (Postfix) with ESMTP id 175FCDB2ED for ; Thu, 10 Nov 2005 20:32:43 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.1/8.13.1) with ESMTP id jAB0Wkim013276; Thu, 10 Nov 2005 19:32:46 -0500 (EST) To: Charlie Savage Cc: pgsql-performance@postgresql.org Subject: Re: Index Scan Costs versus Sort In-reply-to: References: <10461.1131645881@sss.pgh.pa.us> <437398E3.2020900@interserv.com> Comments: In-reply-to Charlie Savage message dated "Thu, 10 Nov 2005 17:11:48 -0700" Date: Thu, 10 Nov 2005 19:32:46 -0500 Message-ID: <13275.1131669166@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.006 required=5 tests=[AWL=0.006] X-Spam-Score: 0.006 X-Spam-Level: X-Archive-Number: 200511/202 X-Sequence-Number: 15459 Charlie Savage writes: > Out of curiosity, how much longer would an index_scan expected to be > versus a seq scan? I was under the impression it would be about a facto > of 4, or is that not usually the case? No, it can easily be dozens or even hundreds of times worse, in the worst case. The factor of 4 you are thinking of is the random_page_cost which is the assumed ratio between the cost of randomly fetching a page and the cost of fetching it in a sequential scan of the whole table. Not only is the sequential scan fetch normally much cheaper (due to less seeking and the kernel probably catching on and doing read-ahead), but if there are N tuples on a page then a seqscan reads them all with one page fetch. In the worst case an indexscan might fetch the page from disk N separate times, if all its tuples are far apart in the index order. This is all on top of the extra cost to read the index itself, too. The planner's estimate of 50x higher cost is not out of line for small tuples (large N) and a badly-out-of-order table. What's puzzling is that you seem to be getting near best-case behavior in what does not seem to be a best-case scenario for an indexscan. regards, tom lane