鉴于以下 -
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 |
+-------------------------+---------------------+