# Prefix table query performance

**URL:** <https://forums.percona.com/t/prefix-table-query-performance/1127>\
**Category:** Other MySQL® Questions\
**Created:** [March 23, 2009, 11:55am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127 "2009-03-23T11:55:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![r00ps](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@r00ps](https://forums.percona.com/u/r00ps)\
**Post date:** [March 23, 2009, 11:55am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/1 "2009-03-23T11:55:19Z")

</div>

Hi all,

I’ve an interesting problem and could use some suggestions for improving the performance…

I have a MyISAM table containing about 650,000 text prefixes. These prefixes are telephone numbers such as international dialling codes, local exchange codes and also complete phone numbers.

create table numbers  
{  
number\_id bigint unsigned auto\_increment,  
number char(32),  
unique key (number\_id),  
key ( number, number\_id)  
}

There are some more columns of meta-info in the actual implementation but they are not important…

I am searching this table to find all matches to a complete telephone number, for example the search will return any matching prefixes and the complete number if present:

select number\_id from numbers where number = left(“12345678”, length(number))

This is taking too long, in the order of seconds (on a fairly underpowered 1 CPU server to be honest) but is using the index according to EXPLAIN.

I have tried adding an extra column containing the length of the number and using that instead of the length(number) calculation in the query which helped allot, but now its not using the index.

create table numbers  
{  
number\_id bigint unsigned auto\_increment,  
number char(32),  
number\_length int unsigned,  
unique key (number\_id),  
key ( number, number\_id)  
}

select number\_id from numbers where number = left(“12345678”, number\_length)

Any suggestions for speeding up this kind of wierd reverse-prefix lookup greatfuly received )

Cheers!

Rupert

---

<div class="post-metadata">

**Author:** ![aftabakhan](https://avatars.discourse-cdn.com/v4/letter/a/71c47a/32.png) [@aftabakhan](https://forums.percona.com/u/aftabakhan)\
**Post date:** [April 3, 2009, 5:42am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/2 "2009-04-03T05:42:32Z")

</div>

Questons-

1. Does The number field contain both CountryCode and Operator Code? e.g. 92 (Pakistan Contry Code) and Operator Code (333 UFone). 92333.  
2.Do you first identify vendor/customer (Using EndPoint or Prefix) before you search for a dialed destination?

---

<div class="post-metadata">

**Author:** ![r00ps](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@r00ps](https://forums.percona.com/u/r00ps)\
**Post date:** [April 3, 2009, 7:54am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/3 "2009-04-03T07:54:07Z")

</div>

Hi,

The number field contains the national code and any more digits required to identify the prefix, eg for Central London, UK the number field would contain ‘0207’ and for the example you gave it would contain ‘0092333’ ( based on UK dialled numbers )

There is no need for a search for vendor etc, its just a one-shot lookup on this table.

Cheers,

Rupert

---

<div class="post-metadata">

**Author:** ![MarkRose](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@MarkRose](https://forums.percona.com/u/MarkRose)\
**Post date:** [April 4, 2009, 2:10pm UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/4 "2009-04-04T14:10:35Z")

</div>

Try using varchar instead of char. It can affect how indexes perform.

---

<div class="post-metadata">

**Author:** ![vgatto](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.percona.com/vgatto/32/748_2.png) [@vgatto](https://forums.percona.com/u/vgatto)\
**Post date:** [April 6, 2009, 9:44am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/5 "2009-04-06T09:44:23Z")

</div>

You could try expressing your prefixes as an integer range. For example, the prefix 1-800 could be expressed as the integer range 18000000000 to 18009999999. Then your query would become SELECT \* FROM prefixes WHERE 18005555555 \>= range\_start AND 18005555555 \<= range\_end.

I don’t know enough about phone numbers to be able to say if you can express all your prefixes in the same integer space, but if you can, this should help.

---

<div class="post-metadata">

**Author:** ![sterin71](https://avatars.discourse-cdn.com/v4/letter/s/5e9695/32.png) [@sterin71](https://forums.percona.com/u/sterin71)\
**Post date:** [April 15, 2009, 5:49pm UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/6 "2009-04-15T17:49:08Z")

</div>

I have yet another alternative for you.

Keep your number\_length column and add an index on  
(number\_length, number, number\_id):

alter table numbers add index num\_ix\_len\_num\_id (number\_length, number, number\_id);

Then you query with a query like this:

SELECT number\_idFROM numbersWHERE number IN ([full length of number], … ,‘12345678’,‘1234567’,‘123456’,‘12345’,‘1234’,‘123’,‘12’)ORDER BY number\_length DESCLIMIT 1

That way it will perform a range scan of the index starting with the longest possible match and stop at the first matching row.

---

<div class="post-metadata">

**Author:** ![r00ps](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@r00ps](https://forums.percona.com/u/r00ps)\
**Post date:** [May 8, 2009, 4:31am UTC](https://forums.percona.com/t/prefix-table-query-performance/1127/7 "2009-05-08T04:31:49Z")

</div>

Hi guys

Thanks very much for the ideas, I’m looking at something else at the moment but hopefully I can get back to this and try these different approaches out and let you know how it goes.

Cheers!

Rupert
