如何让MySQL查询尽可能快地运行?

时间:2014-10-14 18:03:37

标签: mysql

我写了一个查询来获取值并检查来自两个表的条​​件。燃料(时间戳,燃料水平,地址)和DrivingTime(启动时间,停止时间)。我想根据条件timestamp = starttimetimestamp = stoptime显示Fuel表格中的所有字段。查询运行正常,但速度很慢。我预计它运行得更快,但我对如何做到这一点很困惑。这是我的问题:

SELECT FROM_UNIXTIME(F.timestamp), 
       F.fuellevel, 
       F.address 
FROM   gtse.tblfuel F, 
       gtse.tbldrivingtime DT 
WHERE  ( DT.starttime = F.timestamp 
          OR DT.stoptime = F.timestamp ) 
       AND ( FROM_UNIXTIME(DT.starttime) BETWEEN 
                 '2014-04-01 00:00:41' AND '2014-04-10 23:22:39' ) 
       AND F.accountid = 'vinhnghia' 
       AND F.deviceid = '14C-00262' 
       AND F.accountid = DT.accountid 
ORDER  BY F.timestamp; 

有人可以提出一些想法吗?

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

SELECT F.deviceID,
        F.timestamp, 
        F.address, F.fuelLevel
 FROM gtse.tblFuel F 
 INNER JOIN gtse.tblDrivingTime DT 
 ON F.accountID=DT.accountID 
      and F.deviceID=DT.deviceID 
      and (DT.startTime = F.timestamp or DT.stopTime = F.timestamp)
 where (DT.`startTime` BETWEEN  1412874000 and 1413306000 ) 
      and F.accountID = 'vinhnghia' and F.deviceID = '14C-00262' 
      order by F.`timestamp` desc;

如果您仍然遇到问题,请向我发送反馈。