已存在联合索引 idx_category_id_comments_views

    查询语句是

    1. select id from table where
    2. category_id = 1
    3. and
    4. comments > 1
    5. order by views desc;

    上面的sql 会产生 下图的 type全表查 All 和 Extra 的 Using filesort , 是大忌
    join 案例1 ,单表和联合索引的顺序问题 - 图1


    优化方案 : 1跟产品沟通, 功能的 sql 尽量使用定值 就是= ,少用 >

    2 将原来的联和索引改成 idx_category_id_views 不要中间的c2

    join 案例1 ,单表和联合索引的顺序问题 - 图2
    这样 type 是ref 级 和 Extra 是 Using where 大喜