V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
kikione
V2EX  ›  程序员

mysql 优化 filtered 和 rows 问题

  •  
  •   kikione · Aug 3, 2021 · 1661 views
    This topic created in 1737 days ago, the information mentioned may be changed or developed.

    EXPLAIN SELECT * FROM t_request

    WHERE merchant_id = 1507 ORDER BY id DESC LIMIT 10

    merchant_id 有普通索引

    1 SIMPLE requesttra0_ index idx_merchantid_status_createtime_amount PRIMARY 4 292 3.42 Using where

    这张表 700W,麻烦帮我解释一下这个 sql 执行过程, 是否扫描了索引树? 为什么 rows 是 292 filtered 是 3.42 , mysql 执行语句是查了 所有行,还是只查了前 10 行?

    2 replies    2021-08-05 17:42:24 +08:00
    ywlvs
        1
    ywlvs  
       Aug 3, 2021
    你用 10 / 292,不就是 3.42% 嘛
    Tsukihime
        2
    Tsukihime  
       Aug 5, 2021
    merchant_id 有索引会走

    Extra 没有 Using filesort,应该没用 sort_buffer 排序

    Extra 没有 Using idnex,因为 SELECT *,应该需要回表拿数据

    Extra 有 Using where,说明存储引擎返回的数据不全满足查询条件,在 server 层进行了过滤

    从 explain 结果看应该是返回 292 行数据到 server,然后排序后取 10 条返回 client

    我猜因为按照 id 排序,所以执行计划是在 server 层排序,而没用 sort_buffer 或者 rowid 排序
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1170 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 23:08 · PVG 07:08 · LAX 16:08 · JFK 19:08
    ♥ Do have faith in what you're doing.