phpmysqldatabase

MySQL insert country iso or country id from another table


I want to insert each visitor's country in my database. Maxmind returns the 2 letters country ISO that I could store in a VARCHAR(2), that would use 2 bytes, or, alternatively, I can use an UNSIGNED TINYINT that would use 1 byte, and would be the id from a table with all the countries.

However I hit a bump; I need MyISAM engine for fast insertions, but MyISAM does not support FOREIGN KEYS, so I guess that for each insertion, I will have to make a select in the countries table to retrieve the country id.

I don't know what is the best option, I absolutely need to use MyISAM as there will be lots of insertions but I don't want to constantly make SELECTs to retrieve the country id.


Solution

  • If you only need the 2-letter ISO country code (and not the country name, language, or other information) then I'd say that storing it as CHAR(2) with no external table would be less resource-intensive than storing it as SMALLINT (TINYINT wouldn't be enough to cover all countries) with a lookup to an additional table.

    Note: there is no need for VARCHAR(2) in this case, CHAR(2) would be more efficient.