为什么MySQL在没有ORDER BY时使用filesort?

时间:2011-10-06 16:38:33

标签: mysql optimization

所有

我正在尝试解决一些性能问题,并且我已经分析了这个查询:

EXPLAIN SELECT entry.entry_key, entry.node, entry.object_type, entry.object_id,
               entry.copy_id, entry.seq_number, entry.root_seq_number, entry.first_error,
               entry.last_error, entry.error_count, entry.error_id, entry.error_code,
               entry.status, entry.type, entry.sense, entry.reviewed 
       FROM eventlog me  JOIN eventlog_entry eventlog_entries 
            ON eventlog_entries.eventlog_key = me.eventlog_key  JOIN entry entry 
            ON entry.entry_key = eventlog_entries.entry_key 
       WHERE ( ( ( first_error >= '0' AND last_error <= '2147483640' ) AND 
                   me.eventlog_key = '1' ) ) 
       GROUP BY entry.entry_key\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: me
         type: const
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: const
         rows: 1
        Extra: Using index; Using temporary; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: SIMPLE
        table: eventlog_entries
         type: ref
possible_keys: eventlog_entry_idx_entry_key,eventlog_entry_idx_eventlog_key
          key: eventlog_entry_idx_eventlog_key
      key_len: 8
          ref: const
         rows: 1
        Extra:
*************************** 3. row ***************************
           id: 1
  select_type: SIMPLE
        table: entry
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 8
          ref: eventlog.eventlog_entries.entry_key
         rows: 1
        Extra: Using where
3 rows in set (0.00 sec)

原始查询中确实有ORDER BY,但我将其删除了。但是,EXPLAIN仍在报告文件排序,我无法弄清楚原因。 MySQL优化器是否在某处缓存了优化?或者fileort也用于其他原因吗?

1 个答案:

答案 0 :(得分:9)

默认情况下,只要您执行GROUP BY,MySQL就会进行排序。要避免这种情况,只需添加ORDER BY NULL