多列索引比mysql中的单列索引慢

时间:2011-10-28 05:01:12

标签: mysql indexing

我有一个myisam表,其主键跨越5列。我在这5列ANDed的每一列上使用WHERE进行选择。使用主键(多列索引)需要25秒,在其中一列中使用单个索引需要1秒。我做了一个分析,25个中的大多数都是在“发送数据”阶段。主键的基数大约为7M,单列大约为80.我错过了什么?

CREATE TABLE `mytable` (
  `a` int(11) unsigned NOT NULL,
  `b` varchar(2) NOT NULL,
  `c` int(11) unsigned NOT NULL,
  `d` varchar(560) NOT NULL,
  `e` varchar(45) NOT NULL,
  PRIMARY KEY (`a`,`e`,`d`,`b`,`c`),
  KEY `d` (`d`),
  KEY `e` (`e`),
  KEY `b` (`b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;


EXPLAIN SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY PRIMARY 4   const   5912231 Using where


EXPLAIN SELECT * FROM mytable
WHERE a=12 AND e=1319677200 AND d='69.171.242.53' AND b='*' AND c=0;

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  i   ref PRIMARY,d,e,b   d   562 const   158951  Using where

1 个答案:

答案 0 :(得分:2)

问题是由铸造造成的,
尝试引用每个varchar列b,d,e

SELECT * FROM mytable USE INDEX (PRIMARY)
WHERE a=12 AND e='1319677200' AND d='69.171.242.53' AND b='*' AND c=0;