Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id E5AE89F9F7B for ; Sat, 2 Dec 2006 18:35:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 84106-01 for ; Sat, 2 Dec 2006 18:35:20 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from mail01.enterprisedb.com (mail01.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id 8A7009F9EED for ; Sat, 2 Dec 2006 18:35:29 -0400 (AST) thread-index: AccWYi0v6YH2nbRcTaiwpGby6qylXw== Received: from [192.168.1.34] ([82.27.225.36]) by mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.1830); Sat, 2 Dec 2006 17:35:31 -0500 Message-ID: <4571FFA6.6070804@enterprisedb.com> Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.2757 Date: Sat, 02 Dec 2006 22:35:18 +0000 From: "Heikki Linnakangas" User-Agent: Thunderbird 1.5.0.7 (X11/20060927) MIME-Version: 1.0 To: "Alexandru Coseru" Cc: Subject: Re: Regex performance issue References: <030b01c71651$984577c0$8300a8c0@tridecap.com> <01ea01c71653$438254e0$3cb16956@alex> <4571F856.7090808@enterprisedb.com> <022c01c7165f$17f49e80$3cb16956@alex> In-Reply-To: <022c01c7165f$17f49e80$3cb16956@alex> Content-Type: text/plain; format=flowed; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 02 Dec 2006 22:35:32.0296 (UTC) FILETIME=[2D1EFC80:01C71662] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/19 X-Sequence-Number: 22075 Alexandru Coseru wrote: > Hello.. > > I cannot use the first advice , because i'm not aware of the prefix > length in the database... > This is why i'm ordering after length(prefix).. > > > On the 2nd one , i'm not sure that i can follow you.. Ok, let me try again :) > asterisk=> select * from destlist LIMIT 10; > id | id_ent | dir | prefix | country | network | tip > ----+--------+-----+------------+-------------+--------------------+----- > 1 | -1 | 0 | (^0093) | AFGHANISTAN | AFGHANISTAN | 6 > 2 | -1 | 0 | (^00937) | AFGHANISTAN | AFGHANISTAN Mobile | 5 > 3 | -1 | 0 | (^00355) | ALBANIA | ALBANIA | 6 > 4 | -1 | 0 | (^0035538) | ALBANIA | ALBANIA Mobile | 5 > 5 | -1 | 0 | (^0035568) | ALBANIA | ALBANIA Mobile | 5 > 6 | -1 | 0 | (^0035569) | ALBANIA | ALBANIA Mobile | 5 > 7 | -1 | 0 | (^00213) | ALGERIA | ALGERIA | 6 > 8 | -1 | 0 | (^0021361) | ALGERIA | ALGERIA Mobile | 5 > 9 | -1 | 0 | (^0021362) | ALGERIA | ALGERIA Mobile | 5 > 10 | -1 | 0 | (^0021363) | ALGERIA | ALGERIA Mobile | 5 Store the prefix in a character column, without the regex stuff. Like below. I've removed the columns that are not relevant, in fact it would make sense to store them in another table, and have just the id and prefix in this table. id | prefix | network ---+---------+-------------------- 1 | 0093 | AFGHANISTAN 2 | 00937 | AFGHANISTAN Mobile 3 | 00355 | ALBANIA 4 | 0035538 | ALBANIA Mobile 5 | 0035568 | ALBANIA Mobile 6 | 0035569 | ALBANIA Mobile 7 | 00213 | ALGERIA 8 | 0021361 | ALGERIA Mobile 9 | 0021362 | ALGERIA Mobile 10 | 0021363 | ALGERIA Mobile Now, add the rows marked with start below: id | prefix | network ----+---------+-------------------- 1 | 0093 | AFGHANISTAN 2 | 00937 | AFGHANISTAN Mobile * 1 | 00938 | AFGHANISTAN 3 | 00355 | ALBANIA 4 | 0035538 | ALBANIA Mobile * 3 | 0035539 | ALBANIA 5 | 0035568 | ALBANIA Mobile 6 | 0035569 | ALBANIA Mobile * 3 | 003557 | ALBANIA 7 | 00213 | ALGERIA 8 | 0021361 | ALGERIA Mobile 9 | 0021362 | ALGERIA Mobile 10 | 0021363 | ALGERIA Mobile * 7 | 0021364 | ALGERIA The added rows make it unnecessary to use regex for the searches. You can use just the >= operator like this: (using the example number you gave) SELECT id FROM destlist WHERE '00213618833' >= prefix ORDER BY prefix LIMIT 1 Which would return id 7. As another example, a query of "00213654321" would match the last row, which again has id 7. I'm too tired to figure out the exact algorithm for adding the rows, but I'm pretty sure it can be automated... The basic idea is that when there's a row with id A and prefix XXXX, and another row with id B and prefix XXXXY, we add another row with id A and prefix XXXX(Y+1). -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com