MYSQL查询需要很长时间才能加载

时间:2015-03-22 21:52:48

标签: mysql sql performance

我有以下查询,虽然它有效但需要很长时间才能加载

SELECT name, t1.*
FROM user, driver_tracker t1 
LEFT JOIN driver_tracker t2
ON (t1.phone = t2.phone AND t1.id < t2.id)
WHERE user.phone = t1.phone
AND companyid = 'asus'
AND t2.id IS NULL
ORDER BY name

1 个答案:

答案 0 :(得分:1)

以下是您正确表达的查询:

SELECT u.name, t1.*
FROM user u JOIN
     driver_tracker t1 
     ON u.phone = t1.phone LEFT JOIN
     driver_tracker t2
     ON (t1.phone = t2.phone AND t1.id < t2.id)
WHERE u.companyid = 'asus' AND t2.id IS NULL
ORDER BY name;

(注意用适当的显式连接替换from子句中的逗号。简单规则:永远不要在from子句中使用逗号。)

推荐索引:user(companyid, phone, name)driver_tracker(phone, id)