Received: from localhost (unknown [200.46.204.184]) by developer.postgresql.org (Postfix) with ESMTP id 23C782E003A for ; Mon, 7 Apr 2008 13:32:33 -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 14810-04 for ; Mon, 7 Apr 2008 13:32:26 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wr-out-0506.google.com (wr-out-0506.google.com [64.233.184.226]) by developer.postgresql.org (Postfix) with ESMTP id 4BD762E0039 for ; Mon, 7 Apr 2008 13:32:27 -0300 (ADT) Received: by wr-out-0506.google.com with SMTP id 76so1199028wra.10 for ; Mon, 07 Apr 2008 09:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:organization:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; bh=D3IzQxe6V7roxLmlOzOFhZ83pWX9H0ymR5s/NI4W5cM=; b=f7Ug8xeTPEsNoc2HlCjJPUGQ6gYbVfioRj0RM8HDrncKBtETorL15WeXhY7TZ9G1K/pNpiAmSnllejqXxvi7te4pRk3nF7szlB+R1JJami4cx+iR2/hBRfjfKGfsFNzaB4MySbs7C/sDG93H9NEH3z0GgW/i6dTw5dRAEUYer+I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:organization:user-agent:mime-version:to:cc:subject:references:in-reply-to:content-type:content-transfer-encoding:sender; b=ZnX4aR++YHkiy8th8mbYknwnqRAlq3fVuSTYt4oX46+1bKiU/EGPxNDJIj4Wn2MoWYl5dMTbDA/bwTPc/HkjtzAIvbo+H/ZEBgLmpY3MylFhkEm+kC7GPrYzlx2lqX+5sB1mVRlZjZrl5bptvbz+PBCTyIB2IfA2oxBApqNiSTE= Received: by 10.140.177.15 with SMTP id z15mr1597245rve.128.1207585946034; Mon, 07 Apr 2008 09:32:26 -0700 (PDT) Received: from ?192.168.0.199? ( [80.222.108.74]) by mx.google.com with ESMTPS id i39sm7807444ugd.32.2008.04.07.09.32.23 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Apr 2008 09:32:25 -0700 (PDT) Message-ID: <47FA4C96.3090808@enterprisedb.com> Date: Mon, 07 Apr 2008 17:32:22 +0100 From: Heikki Linnakangas Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.9 (X11/20080110) MIME-Version: 1.0 To: Matthew CC: pgsql-performance@postgresql.org Subject: Re: Severe performance problems for simple query References: <1207581719.5399.119.camel@dimi.lattica.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200804/60 X-Sequence-Number: 29553 Matthew wrote: > On Mon, 7 Apr 2008, Dimi Paun wrote: >> * bad performance on queries of the form: >> select * from ipTable where ipFrom <= val and val <= ipTo > > This type of query is very hard for a normal B-tree index to answer. For > example, say val is half-way between min and max values. If you have an > index on ipFrom, it will be able to restrict the entries to about half > of them, which is no real benefit over a sequential scan. Likewise, an > index on ipTo will be able to restrict the entries to half of them, with > no benefit. The intersection of these two halves may be just one entry, > but finding that out is non-trivial. An index bitmap scan would do it if > you can persuade Postgres to do that, but really you want an R-tree > index on the two columns, like I have requested in the past. If I understood the original post correctly, the ipFrom and ipTo columns actually split a single linear ip address space into non-overlapping chunks. Something like this: ipFrom ipTo 1 10 10 20 20 50 50 60 ... In that case, a regular index on (ipFrom, ipTo) should work just fine, and that's what he's got. Actually, an index on just ipFrom would probably work just as well. The problem is that the planner doesn't know about that special relationship between ipFrom and ipTo. Perhaps it could be hinted by explicitly specifying "AND ipTo > ipFrom" in the query? I don't know why the single index lookup took > 300ms, though. That does seem high to me. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com