Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id E98CE2E003B for ; Mon, 7 Apr 2008 13:45:49 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 15651-06 for ; Mon, 7 Apr 2008 13:45:42 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from ulysses.lattica.com (lattica.com [204.14.19.114]) by developer.postgresql.org (Postfix) with ESMTP id E06F32E0039 for ; Mon, 7 Apr 2008 13:45:43 -0300 (ADT) Received: from localhost (ulysses.lattica.com [127.0.0.1]) by ulysses.lattica.com (Postfix) with ESMTP id 4539F7383D6; Mon, 7 Apr 2008 12:45:43 -0400 (EDT) X-Virus-Scanned: amavisd-new at lattica.com Received: from ulysses.lattica.com ([127.0.0.1]) by localhost (ulysses.lattica.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ptLvnisyhBxi; Mon, 7 Apr 2008 12:45:42 -0400 (EDT) Received: from [192.168.3.11] (CPE00a0cc63452d-CM0011aec8bfb4.cpe.net.cable.rogers.com [99.229.154.251]) by ulysses.lattica.com (Postfix) with ESMTP id 7AF3E7383A9; Mon, 7 Apr 2008 12:45:42 -0400 (EDT) Subject: Re: Severe performance problems for simple query From: Dimi Paun To: Matthew Cc: pgsql-performance@postgresql.org In-Reply-To: References: <1207581719.5399.119.camel@dimi.lattica.com> Content-Type: text/plain Organization: Lattica, Inc. Date: Mon, 07 Apr 2008 12:45:42 -0400 Message-Id: <1207586742.5399.144.camel@dimi.lattica.com> Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 (2.12.3-3.fc8) Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/62 X-Sequence-Number: 29555 On Mon, 2008-04-07 at 17:27 +0100, Matthew wrote: > Oh yes, if you can guarantee that no two entries overlap at all, then > there is a simpler way. Just create a B-tree index on ipFrom as usual, > sort by ipFrom, and LIMIT to the first result: > > SELECT blah FROM table_name > WHERE ipFrom <= 42 ORDER BY ipFrom DESC LIMIT 1 > > This should run *very* quickly. However, if any entries overlap at all > then you will get incorrect results. Thanks Matthew, this seems to be indeed a lot faster: perpedes_db=# CREATE INDEX temp1 ON static.ipligenceipaddress (ipFrom); CREATE INDEX perpedes_db=# explain ANALYZE select * from static.ipligenceipaddress where ipfrom <= 2130706433 ORDER BY ipFrom DESC LIMIT 1; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..0.03 rows=1 width=145) (actual time=0.060..0.060 rows=1 loops=1) -> Index Scan Backward using temp1 on ipligenceipaddress (cost=0.00..83453.92 rows=2685155 width=145) (actual time=0.057..0.057 rows=1 loops=1) Index Cond: (ipfrom <= 2130706433) Total runtime: 0.094 ms (4 rows) However, it is rather disappointing that the DB can't figure out how to execute such a simple query in a half decent manner (seq scan on an indexed table for a BETWEEN filter doesn't qualify :)). Many thanks! -- Dimi Paun Lattica, Inc.