大家有没遇到这问题。
字段的默认排序后在进行一次 order by 就会出现  filesortexplain extended 
SELECT
person.n_id id,
person.n_time time,
person.c_name NAME,
person.c_phone phone,
dept.n_id deptId,
dept.c_name deptName,
    dept.c_code,
person.c_account AS account
FROM
t_ecss_person person
left JOIN t_ecss_department dept ON dept.n_id = person.n_dept_id
WHERE
person.c_ec_code = 1
AND person.n_delete_flag = 1
AND dept.c_code LIKE '0000%'ORDER BY
dept.c_code执行计划 1 SIMPLE dept ALL PRIMARY,idx_t_ecss_department_c_code 11 72.73 Using where; Using filesort
1 SIMPLE person ref index_eccode_updatestatus,n_dept_id n_dept_id 9 cssp.dept.n_id 3 100.00 Using where不加order by 1 SIMPLE dept ALL PRIMARY,idx_t_ecss_department_c_code 11 72.73 Using where
1 SIMPLE person ref index_eccode_updatestatus,n_dept_id n_dept_id 9 cssp.dept.n_id 3 100.00 Using where2种写法 c_code排序是一样的。 但是加上了 order by 会出现 filesort现在疑问是  在原有的次序上在做次相同的排序,怎么会出现 filesort?
这个一直想不明白,为什么。有懂的帮忙解释下,谢谢。

解决方案 »

  1.   


    2次排序c_code顺序一样,这个只是巧合,你不写order by,那么就会排序,而如果c_code上没有索引,那么就会排序。
      

  2.   


    版主大大。在请教下。1. c_code上有索引,explain mysql为啥不用索引排序。2.  这跳语句怎么 优化,才能不出现文件层的排序  filesort 。 请指教。谢谢。
      

  3.   


    版主大大。在请教下。1. c_code上有索引,explain mysql为啥不用索引排序。2.  这跳语句怎么 优化,才能不出现文件层的排序  filesort 。 请指教。谢谢。
    1、可能mysql觉得不用索引更快
    2、 filesort 在排序的时候会出现,你表里还有其他索引?
      

  4.   


    版主大大。在请教下。1. c_code上有索引,explain mysql为啥不用索引排序。2.  这跳语句怎么 优化,才能不出现文件层的排序  filesort 。 请指教。谢谢。1.不是有索引,mysql就一定会用的,mysql衡量是否用索引的标准是成本,如果用了索引,发现成本更高,那么他就不用索引2.这个语句从表面看就是2表关联,然后加上关联条件,和过滤条件,基本上也没什么可以优化的了。不过可以考虑索引来优化,
    dept.c_code LIKE '0000%' 这样的记录有多少条