为什么MySQL查询没有使用索引?

时间:2014-02-19 09:49:05

标签: mysql indexing

这是我的问题。使用in列表中的常量查询是使用索引

explain select date, serial from Statistics
where serial in ('qwe', 'asd');

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, SIMPLE, Statistics, range, serial, serial, 257, , 2, Using where

select列表中的in查询不是

explain select date, serial from Statistics
where serial in (select serial from tmp_stats);

# id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, PRIMARY, Statistics, ALL, , , , , 339541476, Using where
2, DEPENDENT SUBQUERY, tmp_stats, ALL, , , , , 10, Using where

可能导致此行为的原因是什么?

表格结构:

CREATE TABLE `Statistics` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `serial` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `date` (`date`),
  KEY `serial` (`serial`)
) ENGINE=MyISAM AUTO_INCREMENT=355592619 DEFAULT CHARSET=ascii;


CREATE TABLE `tmp_stats` (
  `date` timestamp NULL DEFAULT NULL,
  `soft_id` int(10) unsigned NOT NULL,
  `serial` varchar(255) CHARACTER SET ascii NOT NULL,
  KEY `serial` (`serial`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

0 个答案:

没有答案