Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 086379FB4CB for ; Fri, 4 May 2007 12:26:49 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 78486-01 for ; Fri, 4 May 2007 12:26:45 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.175]) by postgresql.org (Postfix) with ESMTP id BFE259FB468 for ; Fri, 4 May 2007 12:26:45 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id 32so632508ugm for ; Fri, 04 May 2007 08:26:44 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=toPiBF5qO0Ug+4kYkN3Zeqr4t/qVOAzOj8Pn4BiiFNzoqOjzZqw1idSJcZbjMdb3Nidx/M4FEYYdIQ9zDb0oS3VdTmTrD8g6nxEFMgKVVJjbkCkbFhvl9hC8t3z2fbbyfUGtT+CeIbkEVBpgVjDpNcMNUxopvsvM410GzmjuWtc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=CG0O9+uBAxJBah68Yq6ppwSbbGkZLIIkT4TNw/lAoWDOwRXuAhJU/gbYC1CS4Il6C7ARXKs98QqA9HYH6uNuaABV9aEbD8dPgdFuBtqmf8dcNLKYfxWtR5FgpyKZrz+6wacFtJN4xyUbRysJ0TQWbza6rIdfFXFrLzfXY3WVAOA= Received: by 10.66.244.11 with SMTP id r11mr2790731ugh.1178292404214; Fri, 04 May 2007 08:26:44 -0700 (PDT) Received: from ?172.24.32.77? ( [62.232.55.118]) by mx.google.com with ESMTP id e23sm5695058ugd.2007.05.04.08.26.41; Fri, 04 May 2007 08:26:42 -0700 (PDT) Message-ID: <463B509F.5070406@enterprisedb.com> Date: Fri, 04 May 2007 16:26:23 +0100 From: Heikki Linnakangas User-Agent: Icedove 1.5.0.10 (X11/20070329) MIME-Version: 1.0 To: Paul Smith CC: pgsql-performance@postgresql.org Subject: Re: Index not being used in sorting of simple table References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200705/46 X-Sequence-Number: 24436 Paul Smith wrote: > Why doesn't it use the other index? If use 'set enable_seqscan=0' then > it does. Just a guess, but is the table clustered on column a? Maybe not explicitly, but was it loaded from data that was sorted by a? Analyzer calculates the correlation between physical order and each column. The planner will favor index scans instead of sorting when the correlation is strong, and it thinks the data doesn't fit in memory. Otherwise an explicitly sort will result in less I/O and be therefore more favorable. You can check the correlation stats with: SELECT tablename, attname, correlation FROM pg_stats where tablename='x'; > I tried using EXPLAIN ANALYZE to see how long it actually took: > - seq scan - 75 secs > - index scan - 13 secs > - seq scan - 77 secs > (I tried the seq scan version after the index scan as well to see if > disk caching was a factor, but it doesn't look like it) That won't flush the heap pages from cache... How much memory do you have and how large is the table? I suspect that the planner thinks it doesn't fit in memory, and therefore favors the seqscan+sort plan which would require less random I/O, but in reality it's in cache and the index scan is faster because it doesn't need to sort. Have you set your effective_cache_size properly? -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com