使用 explain 如何判断二级索引使用后是否回表?

DDD
发布: 2024-11-12 16:06:02
原创
1055人浏览过

使用 explain 如何判断二级索引使用后是否回表?

如何使用 explain 判断二级索引使用后,是否存在回表操作?

对于给定的查询 sql:

select
    track_source_id,
    date_format(created_at, '%y-%m-%d') as day,
    count(*) as total_count,
    sum(case when len_parse_result_list = 0 then 1 else 0 end) as len_parse_result_list_zero_count,
    sum(case when len_parse_result_list is null then 1 else 0 end) as len_parse_result_list_null_count,
    sum(case when len_parse_result_list > 0 then 1 else 0 end) as len_parse_result_list_gte_zero_count
from
    keywordtask
where
    created_at >= now() - interval 30 day
group by
    track_source_id,
    day
order by
    track_source_id,
    day;
登录后复制

其 explain 输出:

| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
| --- | ----------- | ----- | ---------- | ---- | ------------- | --- | ------ | --- | ---- | -------- | ----- |
| 1   | SIMPLE      | keywordtask | NULL      | index | idx_created_at,idx_track_source_id_created_at_len_parse_result_list | idx_track_source_id_created_at_len_parse_result_list | 14 | NULL | 134324154 | 50.0 | Using where; Using index; Using temporary; Using filesort |
登录后复制

是否回表判断:

通过读取 extra 列,可以判断查询是否回表:

  • using index 表示索引覆盖,不需要回表。
  • using index condition 表示索引查找,不需要回表过滤。
  • using index & using where 表示索引查找,但需要回表过滤。
  • using where 表示回表查询数据。
  • 主键查询 不回表,但 extra 列无法反映。

对于给定的查询,extra 列为 using where; using index; using temporary; using filesort,表明查询使用了索引 (idx_track_source_id_created_at_len_parse_result_list),但需要回表过滤条件 created_at >= now() - interval 30 day。因此,该查询会发生回表操作。

以上就是使用 explain 如何判断二级索引使用后是否回表?的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号