避免在内部选择中进行全表扫描

时间:2014-11-15 05:04:28

标签: mysql sql select left-join inner-join

我在MySQL中有以下选择,它产生正确的结果,但执行时间不必要很长:

SELECT tblGPSDevices.Email, tblLoc.Lat, tblLoc.Lon, tblLoc.Radius, tblLoc.CreationTimeStamp, tblTrackedUsers.ID, tblTrackedUsers.TrackerDeviceID, tblTrackedUsers.TrackedDeviceID
    FROM tblTrackedUsers
    INNER JOIN tblGPSDevices ON tblTrackedUsers.TrackedDeviceID = tblGPSDevices.ID
    LEFT OUTER JOIN (
            SELECT A.DeviceID, A.Lat, A.Lon, A.Radius, A.CreationTimeStamp, A.ID 
            FROM tblLocations A 
            INNER JOIN (
                    SELECT DeviceID, MAX(CreationTimeStamp) AS CreationTimeStamp, MAX(ID) AS ID
                    FROM tblLocations
                    GROUP BY DeviceID
            ) AS B ON A.DeviceID = B.DeviceID
                AND A.CreationTimeStamp = B.CreationTimeStamp
                AND A.ID = B.ID                         
    ) AS tblLoc ON tblLoc.DeviceID = tblGPSDevices.ID
    WHERE tblGPSDevices.Validated = 0x01
    AND tblGPSDevices.Enabled = 0x01
    AND tblTrackedUsers.Validated = 0x01
    AND tblTrackedUsers.TrackerDeviceID = 1
    ORDER BY tblTrackedUsers.ID;

此查询运行速度比它应该慢得多,因为它在tblLocations上执行全表扫描。

这是真正减慢查询速度的部分:

SELECT A.DeviceID, A.Lat, A.Lon, A.Radius, A.CreationTimeStamp, A.ID 
        FROM tblLocations A 
        INNER JOIN (
            SELECT DeviceID, MAX(CreationTimeStamp) AS CreationTimeStamp, MAX(ID) AS ID
            FROM tblLocations
            GROUP BY DeviceID
        ) AS B ON A.DeviceID = B.DeviceID
            AND A.CreationTimeStamp = B.CreationTimeStamp
            AND A.ID = B.ID 

以下是解释计划:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   PRIMARY     tblTrackedUsers     ref     TrackerDeviceID,TrackedDeviceID     TrackerDeviceID     9   const   14  Using where; Using temporary; Using filesort
1   PRIMARY     tblGPSDevices   eq_ref  PRIMARY     PRIMARY     8   tblTrackedUsers.TrackedDeviceID     1   Using where
1   PRIMARY     <derived2>  ALL     NULL    NULL    NULL    NULL    2073    
2   DERIVED     <derived3>  ALL     NULL    NULL    NULL    NULL    2073    
2   DERIVED     A   eq_ref  PRIMARY,DeviceID,CreationTimeStampIndex     PRIMARY     8   B.ID    1   Using where
3   DERIVED     tblLocations    index   NULL    DeviceID    8   NULL    174058  

它在tblLocations上进行全表扫描,即使我只需要该表中的一小部分DeviceID。

我只需要查看从这部分返回的DeviceID:

INNER JOIN tblGPSDevices ON tblTrackedUsers.TrackedDeviceID = tblGPSDevices.ID
WHERE tblTrackedUsers.TrackerDeviceID = 1

但遗憾的是tblTrackedUsers.TrackedDeviceID在内部选择中不可见。所以,如果我添加

WHERE DeviceID = tblTrackedUsers.TrackedDeviceID

正上方

GROUP BY DeviceID

它不起作用。

如何优化此查询?

仅涉及相关字段的表格结构:

tblGPSDevices:


  

ID |电子邮件|验证|启用


tblLocations:


  

ID | DeviceID | Lat | Lon |半径| CreationTimeStamp


tblTrackedUsers:


  

ID | TrackerDeviceID | TrackedDeviceID |验证


tblLocations.DeviceID,tblTrackedUsers.TrackerDeviceID和tblTrackedUsers.TrackedDeviceID是指向tblGPSDevices.ID的外键

此查询的作用:

查询应返回用户正在跟踪的tblGPSDevices中的所有设备及其来自tblLocations的最后位置。确定用户正在跟踪哪些设备的方法由tblTrackedUsers确定:从tblTrackedUsers中选择TrackedDeviceID,其中TrackerDeviceID = some_value

1 个答案:

答案 0 :(得分:0)

我确实找到了答案,我将发布以供将来参考。这从2秒加快了查询速度。到0.0179秒这是一个巨大的收获。

关键是要在其中添加一个内部选择:

SELECT DeviceID, MAX(CreationTimeStamp) AS CreationTimeStamp, MAX(ID) AS ID
FROM tblLocations
GROUP BY DeviceID

为了避免全表扫描,因为我们只对那些= to tblTrackedUsers.TrackedDeviceID的DeviceID感兴趣。现在这个选择看起来像这样:

 SELECT C.DeviceID, MAX(C.CreationTimeStamp) AS CreationTimeStamp, MAX(C.ID) AS ID
 FROM tblLocations C
         INNER JOIN (
              SELECT ID, TrackerDeviceID, TrackedDeviceID,  TrackedName, AccessCode, Validated FROM tblAllowedUsers WHERE TrackerDeviceID = 1 AND Validated=0x01
          ) AS D ON D.TrackedDeviceID = C.DeviceID
  GROUP BY DeviceID

以下是完整选择:

SELECT tblGPSDevices.Email, tblLoc.Lat, tblLoc.Lon, tblLoc.Radius, tblLoc.CreationTimeStamp, tblTrackedUsers.ID, tblTrackedUsers.TrackerDeviceID, tblTrackedUsers.TrackedDeviceID
    FROM tblTrackedUsers
    INNER JOIN tblGPSDevices ON tblTrackedUsers.TrackedDeviceID = tblGPSDevices.ID
    LEFT OUTER JOIN (
            SELECT A.DeviceID, A.Lat, A.Lon, A.Radius, A.CreationTimeStamp, A.ID 
            FROM tblLocations A 
            INNER JOIN (
                SELECT C.DeviceID, MAX(C.CreationTimeStamp) AS CreationTimeStamp, MAX(C.ID) AS ID
            FROM tblLocations C
                INNER JOIN (
                    SELECT ID, TrackerDeviceID, TrackedDeviceID,  TrackedName, AccessCode, Validated FROM tblAllowedUsers WHERE TrackerDeviceID = 1 AND Validated=0x01
                ) AS D ON D.TrackedDeviceID = C.DeviceID
            GROUP BY DeviceID
            ) AS B ON A.DeviceID = B.DeviceID
                AND A.CreationTimeStamp = B.CreationTimeStamp
                AND A.ID = B.ID                         
    ) AS tblLoc ON tblLoc.DeviceID = tblGPSDevices.ID
    WHERE tblGPSDevices.Validated = 0x01
    AND tblGPSDevices.Enabled = 0x01
    AND tblTrackedUsers.Validated = 0x01
    AND tblTrackedUsers.TrackerDeviceID = 1
    ORDER BY tblTrackedUsers.ID;