I have two tables.
First one is a table with prefixes
code name price
343 ek1 10
3435 nt 4
3432 ek2 2
Second is call records with phone numbers
number time
834353212 10
834321242 20
834312345 30
I need write a script which find longest prefix from prefixes for each record, and write all this data to third table, like this:
number code ....
834353212 3435
834321242 3432
834312345 343
For number 834353212 we must trim ’8′, and then find the longest code from prefix table, its 3435.
We must always drop first ’8′ and prefix must be in the beginning.
I solved this task long time ago, with very bad way.
Its was terrible perl script which do a lot of queries for each record.
This script:
-
Take a number from calls table, do substring from length(number) to
1 => $prefix in the loop -
Do the query : select count(*) from prefixes where code like ‘$prefix’
- If count>0 then take first prefixes and write into table
First problem is query counts – it’s call_records * length(number)
.
Second problem is LIKE
expressions. I am afraid those are slow.
I tried to solve the second problem by:
CREATE EXTENSION pg_trgm;
CREATE INDEX prefix_idx ON prefix USING gist (code gist_trgm_ops);
That speeds up each query, but did not solve problem in general.
I have 20k prefixes and 170k numbers now, and my old solution is bad. Looks like I need some new solution without loops.
Only one query for each call record or something like this.