Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U1ycy-0007JM-C4 for pgsql-general@arkaria.postgresql.org; Sun, 03 Feb 2013 12:24:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U1ycx-00035C-Sw for pgsql-general@arkaria.postgresql.org; Sun, 03 Feb 2013 12:24:55 +0000 Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U1ycw-000357-UA for pgsql-general@postgresql.org; Sun, 03 Feb 2013 12:24:55 +0000 Received: from plane.gmane.org ([80.91.229.3]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U1ycu-0001AE-PK for pgsql-general@postgresql.org; Sun, 03 Feb 2013 12:24:54 +0000 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1U1yd7-0004aa-Cj for pgsql-general@postgresql.org; Sun, 03 Feb 2013 13:25:05 +0100 Received: from ip-118-90-34-183.xdsl.xnet.co.nz ([118.90.34.183]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 03 Feb 2013 13:25:05 +0100 Received: from jasen by ip-118-90-34-183.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 03 Feb 2013 13:25:05 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-general@postgresql.org From: Jasen Betts Subject: Re: Optimizing query? Date: 3 Feb 2013 05:33:50 GMT Organization: Dis (not Dat) Organisation Lines: 63 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: ip-118-90-34-183.xdsl.xnet.co.nz X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Pg-Spam-Score: -0.4 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org On 2013-01-31, hamann.w@t-online.de wrote: > > Pavel Stehlule wrote: > >>> >> Hi, >>> >> >>> >> I am trying to match items from 2 tables based on a common string. >>> >> One is a big table which has one column with entries like XY123, ABC44, = >>> etc >>> >> The table has an index on that column. >>> >> The second table is, typically, much smaller >>> >> >>> >> select .... from tab1, tab2 where tab1.code =3D tab2.code; >>> >> >>> >> This works fine and fast. >>> >> Now, as a variant, I have some entries like XY423A, XY423B, GF55A, GF55D= >>> in the >>> >> big table and want them to match XY423, GF55 in the second table >>> >> >>> >> Variants I have tried >>> >> >>> >> select .... from tab1, tab2 where tab1.code ~ (tab2.code||'($|[A-Z])'); >>> >> select .... from tab1, tab2 where tab1.code ~ ('^'||tab2.code||'($|[A-Z= >>> ])'); >>> >> >>> > >>> > Have you tried the substring function? >>> > >>> > select .... from tab1, tab2 where substring(tab1.code from 1 for 5) =3D >>> > tab2.code >>> > > > Hi Pavel, it was just by chance that a fixed size substring would match the > data at hand. It is more common to have a digit/letter (or vice versa) boundary > or a hyphen there > >>> > >>> >> both take an enormous time. In the better case that I can subset (e.g. a= >>> ll candidates in table 2 >>> >> share initial "AX") I get back to manageable times by adding >>> >> and tab1.code ~ '^AX' >>> >> into the recipe. Actual runtime with about a million entries in tab1 and= >>> 800 entries in tab2 >>> >> is about 40 seconds. >>> >>> any join where result is related to some function result can be very >>> slow, because estimation will be out and any repeated function >>> evaluation is just expensive. >>> > I see the problem since obviously every the ~ operator with a non-constant > pattern is constantly recompiling the pattern. > > I wonder whether it would be possible to invent a prefix-match operator that approaches > the performance of string equality. I noted in the past (not sure whether anything > has changed in regex matching) that a constant leading part of regex would improve > performance, i.e. use an index scan to select possible candidates. > you could write a set returning function that opens cursors on both tables using "ORDER BY code" and merges the results -- ⚂⚃ 100% natural -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general