强制时SQL索引速度较慢

时间:2013-10-06 07:09:43

标签: php mysql sql windows

如果我有下表:

+------------+---------------+------+-----+---------+-------+
| 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'(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 |
+----+-------------+-----------+------+---------------+------------+---------+-------+------+-----------------------+

定时结果:

no index:
2.7541568279266

index - forced:
2.827162027359

index - no force: 
2.721155166626

0 个答案:

没有答案