Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 1E0C8182A958 for ; Mon, 6 Aug 2012 12:34:26 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SyPK5-0001Gq-95 for pgsql-performance@postgresql.org; Mon, 06 Aug 2012 15:34:25 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q76FY84i010967; Mon, 6 Aug 2012 11:34:08 -0400 (EDT) From: Tom Lane To: Ioannis Anagnostopoulos cc: pgsql-performance@postgresql.org Subject: Re: Sequential scan instead of index scan In-reply-to: <501FE1C9.6010209@anatec.com> References: <501FDDD5.2000106@anatec.com> <501FDFCD.2030806@oneit.com.au> <501FE1C9.6010209@anatec.com> Comments: In-reply-to Ioannis Anagnostopoulos message dated "Mon, 06 Aug 2012 16:24:57 +0100" Date: Mon, 06 Aug 2012 11:34:08 -0400 Message-ID: <10966.1344267248@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201208/27 X-Sequence-Number: 47565 Ioannis Anagnostopoulos writes: > I think this is a pretty good plan and quite quick given the > size of the table (88Million rows at present). However in real > life the parameter where I search for msg_id is not an array of > 3 ids but of 300.000 or more. It is then that the query forgets > the plan and goes to sequential scan. Is there any way around? If you've got that many, any(array[....]) is a bad choice. I'd try putting the IDs into a VALUES(...) list, or even a temporary table, and then writing the query as a join. It is a serious mistake to think that a seqscan is evil when you're dealing with joining that many rows, btw. What you should probably be looking for is a hash join plan. regards, tom lane