X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id E5515D1E0BB for ; Wed, 25 Feb 2004 20:36:47 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 21389-05 for ; Wed, 25 Feb 2004 16:36:46 -0400 (AST) Received: from anchor-post-37.mail.demon.net (anchor-post-36.mail.demon.net [194.217.242.86]) by svr1.postgresql.org (Postfix) with ESMTP id 7E839D1B4DE for ; Wed, 25 Feb 2004 16:36:44 -0400 (AST) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-37.mail.demon.net with esmtp (Exim 3.35 #1) id 1Aw5ls-0004R4-0b for pgsql-sql@postgresql.org; Wed, 25 Feb 2004 20:36:44 +0000 Received: by mainbox.archonet.com (Postfix, from userid 529) id 3986716A1D; Wed, 25 Feb 2004 20:36:44 +0000 (GMT) Received: from client17.archonet.com (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 441EC16A1B for ; Wed, 25 Feb 2004 20:36:42 +0000 (GMT) From: Richard Huxton To: pgsql-sql@postgresql.org Subject: Re: Scalar in a range (but textual not numeric) Date: Wed, 25 Feb 2004 20:36:40 +0000 User-Agent: KMail/1.5 References: <200402251918.20289.dev@archonet.com> In-Reply-To: <200402251918.20289.dev@archonet.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200402252036.40747.dev@archonet.com> X-Bogosity: No, tests=bogofilter, spamicity=0.000000, version=0.15.3 X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200402/347 X-Sequence-Number: 17037 On Wednesday 25 February 2004 19:18, Richard Huxton wrote: > Large table representing non-overlapping blocks: > > blocks(id int4, min varchar, max varchar) > > SELECT * FROM blocks WHERE 'ABCDE' BETWEEN min AND max; > > The estimator gets the wrong plan because it doesn't realise there's (at > most) only one block that can match. Well, replying to myself (just one of my many bad habits) the best I've come up with so far is to add another column with a trimmed string and do a direct comparison against that too: SELECT * FROM blocks WHERE substring('ABCDE',1,3)=block_segment AND 'ABCDE' BETWEEN min AND max This gives the planner something to work with, and on 7.4 it even renders it down to 'ABC' first too (nice :-) That's not quite the same though, because it means I need to split ABCAA..ABDBB into ABCAA..ABCZZ and ABDAA..ABDZZ but it's close enough unless someone is feeling clever this evening. -- Richard Huxton Archonet Ltd