title: Efficiently match a database of phone number prefixes to a phone number?
body: <p>I've got a database table full of phone number prefixes and how much per minute each one costs. The phone prefixes can vary in length a lot, from one digit to lots (5-6 or more).</p>
  
  <p>Some of the prefixes will overlap, for example there is usually a default country per minute rate, so the more specific mobile rate must override the less specific rate.</p>
  
  <p>Currently the way I am thinking of doing it is doing a while loop checking for the number in the prefix table, and each loop removing the last digit off the end. This is very accurate however it does do a lot of queries since a number could be 12 digits long and the relevant prefix is ~4 digits.</p>
  
  <p>Is there a better way?</p>
  
  <p>Update: Here is a screenshot of the table, the first column is the description, 2nd is the call rate and the third is a phone prefix.
  <a href="https://i.stack.imgur.com/NPfxX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NPfxX.png" alt="Phone cost table"></a></p>
  
  <p>The challenge is to match a real phone number like say 61412345678 to the most applicable prefix without needing as many queries as my current method.</p>