试题答案

更新日期:2023-10-22

MySQL中使用explain命令可以模拟优化器执行SQL查询语句,是否使用索引可以通过返回内容中的key值查看,另外返回内容中的访问类型type也是一个重要指标,显示了查询性能,性能从高到低依次是:system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL,一般来说,需要保证查询至少到达range级别,最好能达到ref。

试题解析

mysql> explain select * from departments where dept_name = 'Sales'\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: departments
         type: const
possible_keys: dept_name
          key: dept_name
      key_len: 122
          ref: const
         rows: 1
        Extra: Using index
1 row in set (0.00 sec)

以上查询使用了索引dept_name,因为dept_name是唯一索引,所以查询性能达到const级别。


发表评论

评论数量:0