Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 2D99864FCEA for ; Wed, 12 Nov 2008 09:54:11 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 16234-03 for ; Wed, 12 Nov 2008 09:54:03 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from rv-out-0506.google.com (rv-out-0506.google.com [209.85.198.224]) by mail.postgresql.org (Postfix) with ESMTP id 6DB7D64FCE9 for ; Wed, 12 Nov 2008 09:54:03 -0400 (AST) Received: by rv-out-0506.google.com with SMTP id b25so395892rvf.43 for ; Wed, 12 Nov 2008 05:54:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:date:from:to:cc:subject :message-id:references:mime-version:content-type:content-disposition :in-reply-to:user-agent; bh=q7p57FtXEYJtB8+G8gh+r16gli8Ws+34w8hnWIlZwdM=; b=kTbEYoPRR9++TsphsmrSB7dhnLn9GigveBPeY672UQkMTYFamV8/y1JFOb0iI7CR8f M77PNK1l6ng1VkGPIJ1dq3yzVQBG6IQHuorfHfmX5wLbI8w5Uyc2PD8ULy6oUUPjqLg5 xBmJPpHrasM/5kHM3eKJqcFxmeI9peruZlCp0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; b=YbNwyivX6n4mHuDeR8wCwf58eQRPAk/uDTb5tBBuq8xkfMxKvT5WoqJSB/zbSObxiY 8Sjf+PGKrVNFD/tmkF1FlDkLsEAQAw4eckOQYIELIq7SjX4pwAy0+2WhWduSHV8X+42+ f4bOC7xoO3xHp9eRfH+us0xt7Jf9D8owpuyEE= Received: by 10.140.158.4 with SMTP id g4mr4895786rve.160.1226498040960; Wed, 12 Nov 2008 05:54:00 -0800 (PST) Received: from polonium.part.net (ip67-88-113-150.z113-88-67.customer.algx.net [67.88.113.150]) by mx.google.com with ESMTPS id k41sm15312432rvb.4.2008.11.12.05.53.58 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 12 Nov 2008 05:53:59 -0800 (PST) Date: Wed, 12 Nov 2008 06:54:05 -0700 From: Joshua Tolley To: Julien Theulier Cc: pgsql-performance@postgresql.org Subject: Re: Index usage with sub select or inner joins Message-ID: <20081112135405.GD22032@polonium.part.net> References: <4919F2C6.4000303@fuzzy.cz> <003c01c944c9$c1d92ac0$458b8040$@com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="wULyF7TL5taEdwHz" Content-Disposition: inline In-Reply-To: <003c01c944c9$c1d92ac0$458b8040$@com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200811/88 X-Sequence-Number: 31819 --wULyF7TL5taEdwHz Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Nov 12, 2008 at 02:22:47PM +0100, Julien Theulier wrote: > QUESTION: Why the planner choose seq scan in the first case & indexes scan > in the second case? In a more general way, I observed that the planner has > difficulties to select index scans & does in almost all the cases seq scan, > when doing join queries. After investigations, it looks like when you join > table a with table b on a column x and y and you have an index on column x > only, the planner is not able to choose the index scan. You have to build > the index corresponding exactly to the join statement btw the 2 tables Short, general answer: index scans aren't always faster than sequential scans, and the planner is smart enough to know that. Googling "Why isn't postgresql using my index" provides more detailed results, but in short, if it scans an index, it has to read pages from the index, and for all the tuples it finds in the index, it has to read once again from the heap, whereas a sequential scan requires reading once from the heap. If your query will visit most of the rows of the table, pgsql will choose a sequential scan over an index scan. - Josh / eggyknap --wULyF7TL5taEdwHz Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkka3/0ACgkQRiRfCGf1UMPMqQCdG0+zZcjdt6p5mabrpmiLFGpb GooAoI+LM6DwlyZ6QJmCA0gUBregk6tX =Qbui -----END PGP SIGNATURE----- --wULyF7TL5taEdwHz--