鉴于以下 -的MySQL索引
drop table if exists learning_indexes;
create table learning_indexes (
id INT NOT NULL,
col1 CHAR(30),
col2 CHAR(30),
col3 CHAR(30),
PRIMARY KEY (id),
index idx_col1 (col1),
index idx_col1_col2 (col1,col2)
);
explain
select
col1,col2
from
learning_indexes
where
col1 = 'FOO'
and col2 = 'BAR'
为什么MySQL的挑idx_col1超过idx_col1_col2?
+----+-------------+------------------+------+------------------------+----------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+------+------------------------+----------+---------+-------+------+-------------+
| 1 | SIMPLE | learning_indexes | ref | idx_col1,idx_col1_col2 | idx_col1 | 91 | const | 1 | Using where |
+----+-------------+------------------+------+------------------------+----------+---------+-------+------+-------------+
这是我的版本信息 -
+-------------------------+---------------------+
| Variable_name | Value |
+-------------------------+---------------------+
| innodb_version | 1.1.8 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.29 |
| version_comment | Source distribution |
| version_compile_machine | i386 |
| version_compile_os | osx10.7 |
+-------------------------+---------------------+
谢谢。我认为“使用索引”意味着它从索引中检索选择值?尝试两种变体,注释掉idx_col1。另外,将“col1,col2”切换为*。你能分享你从哪里得到100行信息吗? – 2013-04-10 17:03:48
MySQL Doc说,如果Extra列使用where AND使用索引,则表示索引正在用于执行关键值查找(http://dev.mysql.com/doc/refman/5.5/en/explain-output的.html#讲解联接类型)。 – Marcellus 2013-04-10 17:48:09
对于100(或更多)行数据,我将id设为auto_increment主键。然后我添加了3行随机值。为了相乘,我做了一些像INSERT INTO'learning_indexes'(col1,col2,col3)SELECT CONCAT(col2,'q'),CONCAT(col1,'z'),CONCAT(col3,'c')FROM'learning_indexes' ;反复。这会使每次执行的行数加倍,并以某种方式洗牌并修改新插入的行值。 – Marcellus 2013-04-10 17:53:56