Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0Vuw-0003IP-K6 for pgsql-general@arkaria.postgresql.org; Wed, 30 Jan 2013 11:33:26 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U0Vuv-0004mB-33 for pgsql-general@arkaria.postgresql.org; Wed, 30 Jan 2013 11:33:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0Vut-0004m5-PF for pgsql-general@postgresql.org; Wed, 30 Jan 2013 11:33:23 +0000 Received: from mailout02.t-online.de ([194.25.134.17]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U0Vur-0005Fz-EY for pgsql-general@postgresql.org; Wed, 30 Jan 2013 11:33:23 +0000 Received: from fwd07.aul.t-online.de (fwd07.aul.t-online.de ) by mailout02.t-online.de with smtp id 1U0Vuq-0001XI-3D; Wed, 30 Jan 2013 12:33:20 +0100 Received: from musicman.homeip.net (EIZ0qkZBwhM7saFdzd01Xt-ibUHTaivoq8fRpK3PcaiPsU+y6Q1jHWQKOjwKmQewEM@[93.233.100.235]) by fwd07.aul.t-online.de with esmtp id 1U0Vuo-1zlNlA0; Wed, 30 Jan 2013 12:33:18 +0100 Received: (qmail 5105 invoked by uid 1000); 30 Jan 2013 11:33:17 -0000 From: hamann.w@t-online.de Date: Wed, 30 Jan 2013 12:33:17 +0100 To: pgsql-general@postgresql.org Subject: optimize query? Message-ID: <510904FD.mailx3XR19NPI6@amadeus3.local> User-Agent: nail 10.5 4/27/03 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-ID: EIZ0qkZBwhM7saFdzd01Xt-ibUHTaivoq8fRpK3PcaiPsU+y6Q1jHWQKOjwKmQewEM X-TOI-MSGID: 5b44fff0-05f6-4d78-8287-a18c0f92c685 X-Pg-Spam-Score: -2.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 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 = 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])'); both take an enormous time. In the better case that I can subset (e.g. all 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. 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