Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
CREATE TABLE `worldip` (
`start` int(10) UNSIGNED NOT NULL default '0',
`end` int(10) UNSIGNED NOT NULL default '0',
`code` varchar(2) NOT NULL default '',
PRIMARY KEY (`start`,`end`)
) ENGINE=MyISAM;
SELECT code FROM `worldip` WHERE `start` <= INET_ATON('IP') AND `end` >= INET_ATON('IP') LIMIT 1
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE ip2country_copy range PRIMARY PRIMARY 4 NULL 1648 Using where
1 SIMPLE ip2country_copy range PRIMARY,idn2 PRIMARY 4 NULL 1579 Using where
mysql> EXPLAIN SELECT code FROM `worldip` USE INDEX (PRIMARY)
WHERE `start` <= INET_ATON('8.8.8.8') AND `end` >= INET_ATON('8.8.8.8') LIMIT 1;
+----+-------------+---------+--------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+--------+---------------+------+---------+------+------+-------+
| 1 | SIMPLE | worldip | system | PRIMARY | NULL | NULL | NULL | 1 | |
+----+-------------+---------+--------+---------------+------+---------+------+------+-------+
SELECT code FROM `worldip` WHERE `start` <= INET_ATON('IP') AND `end` >= INET_ATON('IP') LIMIT 1
Индекс должен быть сдвоенным в общем.
ORDER BY enables you to create the new table with the rows in a specific order. Note that the table does not remain in this order after inserts and deletes. This option is useful primarily when you know that you are mostly to query the rows in a certain order most of the time. By using this option after major changes to the table, you might be able to get higher performance. In some cases, it might make sorting easier for MySQL if the table is in order by the column that you want to order it by later.
WHERE `ipn1` <= INET_ATON('IP') ORDER BY `ipn1` DESC
Определение страны по IP: тестируем скорость алгоритмов