我有一张桌子,大约有360万条记录。该表有2个INT列,表示IPv4“范围”(从和到)
没有任何索引的查询如:
SELECT * FROM tablename WHERE INET_ATON("an ip address") BETWEEN fromi AND toi;
在大约4秒内返回(5行)。
在fromi和toi(ADD INDEX foo USING BTREE (fromi ASC, toi ASC)
)
重复相同的查询需要更长时间(实际上我不知道多少因为我在6分钟后中止了)。
我想知道原因。
据我所知,mysql中没有详细的“解释计划”。这些是EXPLAIN EXTENDED
:
EXPLAIN EXTENDED SELECT * FROM tablename
WHERE INET_ATON("an ip address") BETWEEN fromi AND toi;
select_type: SIMPLE
table: tablename
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 3634094
filtered: 100.00
Extra: Using where
#takes 4 seconds to query
EXPLAIN EXTENDED SELECT * FROM tablename
WHERE INET_ATON("an ip address") BETWEEN fromi AND toi;
select_type: SIMPLE
table: tablename
type: ALL
possible_keys: foo
key: NULL
key_len: NULL
ref: NULL
rows: 3634094
filtered: 50.00
Extra: Using where
#takes really long to query
####################################################
EXPLAIN EXTENDED SELECT * FROM tablename FORCE INDEX(foo)
WHERE INET_ATON("an ip address") BETWEEN fromi AND toi;
select_type: SIMPLE
table: tablename
type: ALL
possible_keys: foo
key: foo
key_len: 5
ref: NULL
rows: 1817047
filtered: 100.00
Extra: Using where
#also takes really long to query
奇怪的是,索引只是因为存在而使查询变慢,mysql甚至不使用它(key:NULL)。