需求:
explain
该关键字是查看该sql用的是哪个索引,如下sql
explain SELECT
a.sensor_number sensorNumber,
a.sync_time syncTime,
b.address address,
a.t_port tPort,
a.a_port aPort,
a.three_t_port threeTPort,
a.ambient_t_port ambientTPort,
a.s_port sPort,
a.`level` LEVEL
FROM
interface_data_list a
STRAIGHT_JOIN interface_list b ON a.sensor_number = b.sensor_number
STRAIGHT_JOIN user_project c ON c.project_list_id = b.project_list_id
WHERE
c.user_id = 41
AND a.sync_time>CURDATE()
AND a.sensor_number="009569000000a5da"
ORDER BY
a.sync_time DESC LIMIT 10;
如上表,我们之前在integerface_data_list中加了sensor_number和sync_time的索引,查询语句中的where条件中,既有sensor_number又有sync_time,所以造成mysql不知道该用哪个索引,
解决办法,添加联合索引,完美解决。