如何减少mysql查询中的扫描总数?

时间:2013-02-08 10:47:10

标签: mysql

我想从以下查询中显示一些有限的记录,但是如何使用连接减少在查询中执行的扫描总数并使执行更快?

查询::

select ml.*,m.title as medianame,mt.name as media_type,d.device_name as devicename,
SUBTIME(ml.end_time,ml.start_time) as playback_duration,d.client_id
from device_media_log ml left join media m on m.media_id = ml.media_id left join device d on d.device_id = ml.device_id
left join media_type mt on m.media_type_id = mt.media_type_id
order by ml.play_date desc,ml.start_time desc,ml.end_time desc;

所有表都包含必需的索引。

1 个答案:

答案 0 :(得分:0)

我建议最好的方法是尽可能使用where子句。

选择ml。*,m.title作为medianame,mt.name作为media_type,d.device_name作为devicename,SUBTIME(ml.end_time,ml.start_time)作为playback_duration,d.client_id来自device_media_log ml left join media m on m.media_id = ml.media_id左连接设备d在d.device_id = ml.device_id左连接media_type mt on m.media_type_id = mt.media_type_id WHERE SOMETHING = SOMETHING AND SOMETHING = SOMETHING order by ml.play_date desc,ml.start_time desc ,ml.end_time desc;