如果我有下表:
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| birth_date | date | NO | | NULL | |
| first_name | varchar(14) | NO | MUL | NULL | |
| last_name | varchar(16) | NO | | NULL | |
| gender | enum('M','F') | NO | | NULL | |
| hire_date | date | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
我做:
SELECT * FROM employees FORCE INDEX (first_name) WHERE first_name = 'Shem'
对带有 first_name 索引的表的此查询比运行语句慢:
SELECT * FROM employees WHERE first_name = 'Shem'
在下表中:
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int(11) | NO | PRI | NULL | |
| birth_date | date | NO | | NULL | |
| first_name | varchar(14) | NO | | NULL | |
| last_name | varchar(16) | NO | | NULL | |
| gender | enum('M','F') | NO | | NULL | |
| hire_date | date | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
为什么使用索引并强制 mySQL 使用该索引比在 first_name 上根本没有任何索引要慢?这不应该更快吗?
解释
SELECT * FROM employees WHERE first_name = 'Shem'(无索引)
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref |rows | Extra |
+----+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | NULL | NULL | NULL | NULL |299733 | Using where |
+----+-------------+-----------+------+---------------+------+---------+------+-
SELECT * FROM employees FORCE INDEX(first_name) WHERE first_name = 'Shem' (index)
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | employees | ref | first_name | first_name | 44 | const | 235 | Using index condition |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
SELECT * FROM employees WHERE first_name = 'Shem'(索引)
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
| 1 | SIMPLE | employees | ref | first_name | first_name | 44 | const | 235 | Using index condition |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+
定时结果:
no index:
2.7541568279266
index - forced:
2.827162027359
index - no force:
2.721155166626