鑑於以下 -的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