例如:
mysql> drop index idx_t3_id on t3; Query OK, 1000 rows affected (0.03 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> create index idx_t3_id on t3(id) ; Query OK, 1000 rows affected (0.04 sec) Records: 1000 Duplicates: 0 Warnings: 0 mysql> explain select * from t3,t4 where t3.id=t4.accountid; +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ | 1 | SIMPLE | t4 | ALL | NULL | NULL | NULL | NULL | 1000 | | | 1 | SIMPLE | t3 | ref | PRIMARY,idx_t3_id | idx_t3_id | 4 | dbatest.t4.accountid | 1 | | +----+-------------+-------+------+-------------------+-----------+---------+----------------------+------+-------+ 2 rows in set (0.00 sec) |
(5). ref_or_null
该联接类型如同ref,但是添加了MySQL可以专门搜索包含NULL值的行。在解决子查询中经常使用该联接类型的优化。
在下面的例子中,MySQL可以使用ref_or_null联接来处理ref_tables:
select * FROM ref_table where key_column=expr OR key_column IS NULL; |
(6). index_merge
该联接类型表示使用了索引合并优化方法。在这种情况下,key列包含了使用的索引的清单,key_len包含了使用的索引的最长的关键元素。
例如:
mysql> explain select * from t4 where id=3952602 or accountid=31754306 ; +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ | 1 | SIMPLE | t4 | index_merge | idx_t4_id,idx_t4_accountid | idx_t4_id,idx_t4_accountid | 4,4 | NULL | 2 | Using union(idx_t4_id,idx_t4_accountid); Using where | +----+-------------+-------+-------------+----------------------------+----------------------------+---------+------+------+------------------------------------------------------+ 1 row in set (0.00 sec) |
(7). unique_subquery
该类型替换了下面形式的IN子查询的ref:
value IN (select primary_key FROM single_table where some_expr) |
unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。