実行に約20秒かかるSQLクエリを最適化しようとしています。
これが私のテーブルの構造です。
last_login
id | ip_address |when
1 2130706433 2012-05-04 12:00:36
と
country_by_ip
ip_from | ip_to | country
16843008 | 16843263 | CN
私が使用するクエリは次のとおりです。
SELECT
ll.ip_address,
ll.when,
cbi.country
FROM last_login ll
LEFT JOIN `country_by_ip` cbi on ll.ip_address BETWEEN cbi.ip_from AND cbi.ip_to
フィールド ip_from および ip_to は索引付けされています。
このクエリを高速化する方法を教えてください。
//編集
CREATE TABLE `last_login` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip_address` int(11) unsigned NOT NULL,
`when` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=32 DEFAULT CHARSET=utf8
CREATE TABLE `country_by_ip` (
`ip_from` int(20) unsigned NOT NULL,
`ip_to` int(20) DEFAULT NULL,
`country` varchar(255) DEFAULT NULL,
KEY `ip_from` (`ip_from`),
KEY `ip_to` (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8