views:

13

answers:

1

i am creating a mysql function which takes in a varchar and change it to a int based on a mapping table.

select name, convert_to_code(country) from users where sex = 'male'

here, the convert_to_code() function takes in a country name (e.g. Japan, Finland..) and change it to country code which is an integer (e.g. 1001, 2310..) based on a mapping table called country_maping like below:

country_name (varchar) | country_code (int)
Japan                  | 1001
Finland                | 2310
Canada                 | 8756

currently, the stored function need to select country_code from country_mapping where country_name = country_name and return the query result. is that possible to create a hash data struct in SP to optimize the process so that it will not need to perform the query for each row matches the where clause.

thanks in advance!

A: 

You should just use the table as you're doing.

If you create a covering index over those two columns, it should make it as efficient as possible.

Bill Karwin