Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 3C43B9FB5DA for ; Fri, 4 May 2007 12:43:08 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 96445-01 for ; Fri, 4 May 2007 12:43:04 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id E13559FB66D for ; Fri, 4 May 2007 12:43:02 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l44Fh1Tl003293; Fri, 4 May 2007 11:43:02 -0400 (EDT) To: Paul Smith cc: pgsql-performance@postgresql.org Subject: Re: Index not being used in sorting of simple table In-reply-to: References: Comments: In-reply-to Paul Smith message dated "Fri, 04 May 2007 15:36:19 +0100" Date: Fri, 04 May 2007 11:43:01 -0400 Message-ID: <3292.1178293381@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/47 X-Sequence-Number: 24437 Paul Smith writes: > If I do > EXPLAIN SELECT * FROM x ORDER BY a; > it says > Index Scan using y on x (cost=0.00..2903824.15 rows=1508057 width=152) > That's what I'd expect > However, if I do > EXPLAIN SELECT * FROM x ORDER BY b; > it says > Sort (cost=711557.34..715327.48 rows=1508057 > width=152) > Sort Key: > b > -> Seq Scan on x (cost=0.00..53203.57 rows=1508057 width=152) > Why doesn't it use the other index? You have the question backwards: given those cost estimates, I'd wonder why it doesn't do a sort in both cases. Offhand I think the sort cost estimate is pretty much independent of the data itself, so it should have come out with a cost near 715327 for sorting on A, so why's it using an indexscan that costs more than 4x as much? The indexscan cost estimate varies quite a bit depending on the estimated correlation (physical ordering) of the column, so seeing it do different things in the two cases isn't surprising in itself. But I think there's some relevant factor you've left out of the example. As for getting the estimates more in line with reality, you probably need to play with random_page_cost and/or effective_cache_size. > Any ideas? To me it looks like a bug in the planner. I can't think of > any logical reason not to use an existing index to retrieve a sorted > listing of the data. Sorry, but using a forced sort frequently *is* faster than a full-table indexscan. It all depends on how much locality of reference there is, ie how well the index order and physical table order match up. The planner's statistical correlation estimate and cost parameters may be far enough off to make it pick the wrong choice, but it's not a bug that it considers the options. regards, tom lane