案例:MySQL优化器如何选择索引和JOIN顺序(3)

发表于:2013-05-06来源:不祥作者:不详点击数: 标签:MySQL
for i in `seq 1 1000` ; do mysql -uroot test -e insert into department values (27760,repeat(char(65+rand()*58),rand()*20)); done explain select * from employee as A,department as B where A.LastName =

  for i in `seq 1 1000` ; do mysql -uroot test -e 'insert into department values (27760,repeat(char(65+rand()*58),rand()*20))';

  done

  explain

  select *

  from

  employee as A,department as B

  where

  A.LastName = 'zhou'

  and B.DepartmentID = A.DepartmentID

  and B.DepartmentName = 'TBX';

  +----+-------------+-------+------+-----------------+---------+---------+---------------------+------+-------------+

  | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  +----+-------------+-------+------+-----------------+---------+---------+---------------------+------+-------------+

  | 1 | SIMPLE | A | ref | IND_L_D,IND_DID | IND_L_D | 43 | const | 1 | Using where |

  | 1 | SIMPLE | B | ref | IND_D,IND_DN | IND_D | 5 | test.A.DepartmentID | 1 | Using where |

  +----+-------------+-------+------+-----------------+---------+---------+---------------------+------+-------------+

  可以看到这里,MySQL执行计划对表department使用了索引IND_D,那么A表命中一条记录为(zhou,27760);根据B.DepartmentID=27760将返回1010条记录,然后根据条件DepartmentName = 'TBX'进行过滤。

  这里可以看到如果B表选择索引IND_DN,效果要更好,因为DepartmentName = 'TBX'仅仅返回10条记录,再根据条件A.DepartmentID=B.DepartmentID过滤之。

  这个案例中因为数据量很小,性能还相差不大,但如果生产环境中数据是千万或者亿级别的时候性能就会差非常非常非常大。通过简单的Hint可以解决这个问题。

原文转自:http://www.orczhou.com/index.php/2013/04/how-mysql-choose-index-in-a-join/