Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0ytY-0006vC-PS for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2013 18:29:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U0ytX-0006s9-K7 for pgsql-general@arkaria.postgresql.org; Thu, 31 Jan 2013 18:29:55 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0ytG-0006cs-61 for pgsql-general@postgresql.org; Thu, 31 Jan 2013 18:29:38 +0000 Received: from mailout07.t-online.de ([194.25.134.83]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0ytC-0008CQ-WA for pgsql-general@postgresql.org; Thu, 31 Jan 2013 18:29:37 +0000 Received: from fwd16.aul.t-online.de (fwd16.aul.t-online.de ) by mailout07.t-online.de with smtp id 1U0ytC-0005Br-20; Thu, 31 Jan 2013 19:29:34 +0100 Received: from musicman.homeip.net (XpM-NsZlrh6NnR+andIPwvj+GXSC5a9Dn7WpUPGKWhBxulaOzaIWnlcSHW9BUfNgu9@[217.246.8.201]) by fwd16.aul.t-online.de with esmtp id 1U0yt7-0hXmAi0; Thu, 31 Jan 2013 19:29:29 +0100 Received: (qmail 9244 invoked by uid 1000); 31 Jan 2013 18:29:28 -0000 Date: 31 Jan 2013 18:29:28 -0000 Subject: Re: Optimizing query? To: pgsql-general@postgresql.org From: hamann.w@t-online.de Message-Id: X-Mailer: TkMail 4.0beta9 Content-type: text/plain; charset=iso-8859-1 In-Reply-To: X-ID: XpM-NsZlrh6NnR+andIPwvj+GXSC5a9Dn7WpUPGKWhBxulaOzaIWnlcSHW9BUfNgu9 X-TOI-MSGID: 6083c3f9-a04d-4215-af70-10a9be5b8533 X-Pg-Spam-Score: -2.0 (--) 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 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 can try use a functional index. >> >> create index on tab2 ((substring(tab1.code from 1 for 5)) >> What kind of trick is that - mixing two tables into a functional index? What would the exact syntax be for that? Regards Wolfgang Hamann -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general