Mysql 性能优化,常见问题

mysql 创建的优化就是加索引,可是有时候会遇到加索引都没法达到想要的效果的情况,

 加上了所以,却还是搜索的全数据,原因是sql

    EXPLAIN    SELECT
            cs.sid,
            -- c.courseFrontTitle,
            -- c.imgBig,
            cs.studyStatus,
            coi.fee,
            -- act.PROC_INST_ID_ AS processId,
            cs.createDTM,
            cs.payStatus,
            cs.isCompleted,
            cs.saleChannel,
cs.isDelete
        FROM
            Biz_CourseStudy cs

        LEFT JOIN Biz_CourseOrderItem coi  ON     cs.sid =  coi.CourseStudyID 
        
        WHERE
            cs.studentID = 00001 and cs.payStatus not in(0) 

通过看索引,原因是因为sid为bigint ,   CourseStudyID  的类型确实varchar,原因就是在这里,修改类型为bigint后,查询速度瞬间提升.

遇到过这样一种情况,分析extra,去掉order by 0.6s速度OK,加上order by 6s

解决方法,给order by 创建索引,这里我的order by是两个字段

order by endTime   desc ,isDelete desc 

 为a b 创建联合索引, index_a_b

SELECT xxx FROM manage a FORCE INDEX(index_a_b)

LEFT JOIN f_name f ON f.user_id = a.user_id

ORDER BY a.endTime desc,a.isDelete desc  

此时看性能,Using filesort已经消失

速度直接变成0.6s

 

原文地址:https://www.cnblogs.com/sunxun/p/12463679.html