21.2.8 连接查询匹配索引
使用JOIN连接语句查询多个数据表中的数据,并且当实现JOIN连接的字段上添加了索引时,MySQL会使用索引查询数据。
mysql> EXPLAIN SELECT goods.t_name, category.t_category
-> FROM t_goods goods JOIN t_goods_category category
-> ON goods.t_category_id = category.id \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: category
partitions: NULL
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 4
filtered: 100.00
Extra: NULL
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: goods
partitions: NULL
type: ref
possible_keys: category_name_index
key: category_name_index
key_len: 5
ref: goods.category.id
rows: 4
filtered: 100.00
Extra: Using index
2 rows in set, 1 warning (0.00 sec)
连接查询时使用了t_goods数据表中的category_name_index索引。