mysql中多列索引比单列索引慢
我有一个 myisam 表,其主键跨越 5 列。我对这 5 列中的每一列都使用 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
I have a myisam table with a primary key spanning 5 columns. I do a select using a WHERE on every of those 5 columns ANDed. Using the primary key (multicolumn index) it takes 25s, using a single index in one of the columns it takes 1 sec. I did a profiling and most of the 25s is taken in “Sending data” stage. The primary key has cardinality of about 7M and the single column about 80. Am i missing somehting?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题是由铸造引起的,
尝试引用每个 varchar 列
b,d,e
The problem is caused by casting,
try quote every varchar column
b,d,e