X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B9D21D956C for ; Mon, 26 Sep 2005 12:29:24 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15594-05 for ; Mon, 26 Sep 2005 15:29:15 +0000 (GMT) Received: from mail.clickdiario.com (mail.clickdiario.com [70.85.167.114]) by svr1.postgresql.org (Postfix) with ESMTP id 4DF38D9564 for ; Mon, 26 Sep 2005 12:29:14 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by mail.clickdiario.com (Postfix) with ESMTP id 9258F10329 for ; Mon, 26 Sep 2005 10:32:45 -0500 (CDT) Received: from mail.clickdiario.com ([127.0.0.1]) by localhost (mail.clickdiario.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 26101-04 for ; Mon, 26 Sep 2005 10:32:45 -0500 (CDT) Received: from cristian1 (unknown [216.230.158.50]) by mail.clickdiario.com (Postfix) with ESMTP id C336210149 for ; Mon, 26 Sep 2005 10:32:44 -0500 (CDT) From: "Cristian Prieto" To: Subject: Index use in BETWEEN statement... Date: Mon, 26 Sep 2005 09:26:43 -0600 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook, Build 11.0.5510 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 In-Reply-To: <20050923220857.3A0841001E@mail.clickdiario.com> Thread-Index: AcXAipbGc98Fe8XwQlKGyZnlcfBNQQCJAatw Message-Id: <20050926153244.C336210149@mail.clickdiario.com> X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at example.com X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.016 required=5 tests=[AWL=0.016] X-Spam-Level: X-Archive-Number: 200509/1016 X-Sequence-Number: 84031 Hello pals, I have the following table in Postgresql 8.0.1 Mydb# \d geoip_block Table "public.geoip_block" Column | Type | Modifiers -------------+--------+----------- locid | bigint | start_block | inet | end_block | inet | mydb# explain analyze select locid from geoip_block where '216.230.158.50'::inet between start_block and end_block; QUERY PLAN ---------------------------------------------------------------------------- ------------------------------------------- Seq Scan on geoip_block (cost=0.00..142772.86 rows=709688 width=8) (actual time=14045.384..14706.927 rows=1 loops=1) Filter: (('216.230.158.50'::inet >= start_block) AND ('216.230.158.50'::inet <= end_block)) Total runtime: 14707.038 ms Ok, now I decided to create a index to "speed" a little the query Mydb# create index idx_ipblocks on geoip_block(start_block, end_block); CREATE INDEX clickad=# explain analyze select locid from geoip_block where '216.230.158.50'::inet between start_block and end_block; QUERY PLAN ---------------------------------------------------------------------------- ------------------------------------------ Seq Scan on geoip_block (cost=0.00..78033.96 rows=230141 width=8) (actual time=12107.919..12610.199 rows=1 loops=1) Filter: (('216.230.158.50'::inet >= start_block) AND ('216.230.158.50'::inet <= end_block)) Total runtime: 12610.329 ms (3 rows) I guess the planner is doing a sequential scan in the table, why not use the compound index? Do you have any idea in how to speed up this query? Thanks a lot!