Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 59834B5D8B2 for ; Mon, 23 May 2011 18:38:39 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 26807-01 for ; Mon, 23 May 2011 21:38:32 +0000 (UTC) X-Greylist: delayed 00:07:57.944738 by SQLgrey-1.7.6 Received: from mail-yx0-f174.google.com (mail-yx0-f174.google.com [209.85.213.174]) by mail.postgresql.org (Postfix) with ESMTP id 71550B5DBD3 for ; Mon, 23 May 2011 18:38:32 -0300 (ADT) Received: by yxs7 with SMTP id 7so2318730yxs.19 for ; Mon, 23 May 2011 14:38:32 -0700 (PDT) Received: by 10.150.115.6 with SMTP id n6mr3401538ybc.43.1306186234377; Mon, 23 May 2011 14:30:34 -0700 (PDT) Received: from AnibalVAIO ([190.128.222.90]) by mx.google.com with ESMTPS id x14sm2937000ybg.14.2011.05.23.14.30.31 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 May 2011 14:30:33 -0700 (PDT) From: "Anibal David Acosta" To: Subject: postgres not use index, IN statement Date: Mon, 23 May 2011 17:30:34 -0400 Message-ID: <006801cc1990$a8b34e10$fa19ea30$@devshock.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Outlook 14.0 Thread-Index: AcwZj/ZbdY4Qnu25TSiA99EiLYow9A== Content-Language: es-py X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201105/314 X-Sequence-Number: 43823 I have a strange situation. I have a table of detail with millones of rows and a table of items with thousands of rows When I do.. select count(*) from wiz_application_response where application_item_id in (select id from wiz_application_item where application_id=110) This query NOT use the index on column application_item_id, instead is doing a sequential scan BUT, when I add the range of min and max id of the subquery, the postgres uses the INDEX This is the second query... select count(*) from wiz_application_response where application_item_id between 908 and 1030 and application_item_id in(select id from wiz_application_item where application_id=110) 908 and 1030 are limits (lower and upper) of the subquery, the subquery returns 100 elements aprox. So, this is some bug? Thanks! Anibal