2008-11-15 85 views
1

爲什麼我看到下面的行爲可以有人向我解釋:MySQL的真VS假優化

mysql> show index from history_historyentry; 
+----------------------+------------+------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table    | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+----------------------+------------+------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| history_historyentry |   0 | PRIMARY      |   1 | id   | A   |  48609 |  NULL | NULL |  | BTREE  |   | 
+----------------------+------------+------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
1 row in set (0.00 sec) 

mysql> explain SELECT COUNT(*) FROM `history_historyentry` WHERE `history_historyentry`.`is_deleted` = False; 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
| id | select_type | table    | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
| 1 | SIMPLE  | history_historyentry | ALL | NULL   | NULL | NULL | NULL | 48612 | Using where | 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
1 row in set (0.00 sec) 

mysql> explain SELECT COUNT(*) FROM `history_historyentry` WHERE `history_historyentry`.`is_deleted` = True; 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
| id | select_type | table    | type | possible_keys | key | key_len | ref | rows | Extra  | 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
| 1 | SIMPLE  | history_historyentry | ALL | NULL   | NULL | NULL | NULL | 48613 | Using where | 
+----+-------------+----------------------+------+---------------+------+---------+------+-------+-------------+ 
1 row in set (0.00 sec) 

mysql> create index deleted on history_historyentry (is_deleted) ; 
Query OK, 48627 rows affected (0.38 sec) 
Records: 48627 Duplicates: 0 Warnings: 0 

mysql> explain SELECT COUNT(*) FROM `history_historyentry` WHERE `history_historyentry`.`is_deleted` = False; 
+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+--------------------------+ 
| id | select_type | table    | type | possible_keys | key  | key_len | ref | rows | Extra     | 
+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+--------------------------+ 
| 1 | SIMPLE  | history_historyentry | index | deleted  | deleted | 1  | NULL | 36471 | Using where; Using index | 
+----+-------------+----------------------+-------+---------------+---------+---------+------+-------+--------------------------+ 
1 row in set (0.00 sec) 

mysql> explain SELECT COUNT(*) FROM `history_historyentry` WHERE `history_historyentry`.`is_deleted` = True; 
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+-------------+ 
| id | select_type | table    | type | possible_keys | key  | key_len | ref | rows | Extra  | 
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+-------------+ 
| 1 | SIMPLE  | history_historyentry | ref | deleted  | deleted | 1  | const | 166 | Using index | 
+----+-------------+----------------------+------+---------------+---------+---------+-------+------+-------------+ 
1 row in set (0.00 sec) 

爲什麼在使用索引的descrepancy爲真VS假?具體來說,在假的情況下,ref列是NULL,額外的列是使用where;使用索引。但在真實情況下,ref列是const,額外的列是使用索引。

回答

2

大概是因爲一個提供了很好的選擇性,另一個沒有提供,即只有一小部分的行被刪除。

基於成本的優化器只有在提供了良好的選擇性(通常爲10%)或者可能是覆蓋索引(無需進一步表或書籤查詢)的情況下才會使用索引。

+0

可以憑經驗驗證與具有行相同數量的同IS_DELETED =真,並與IS_DELETED =假 – 2008-11-15 09:49:46