0

我有这个查询:

select * from 

当我执行它时,大约需要 45 秒和 35k 条记录。每天我都会向gps_unit_location表中添加 5k+ 条新记录。所以桌子会变大。我当前对所有 id 的索引。添加任何其他索引会帮助我提高此查询的性能吗?

谢谢。

4

2 回答 2

2

所以,

  1. 确保你有NOT NULL列和索引:

    INDEX ON gps_unit_location.idgps_unit_location
    INDEX ON user.iduser
    INDEX ON user_to_gps_unit.iduser
    INDEX ON user_to_gps_unit.idgps_unit
    INDEX ON gps_unit.idgps_unit
    INDEX ON gps_unit_location.idgps_unit
    
  2. 确保您确实需要选择带有该星的所有字段*

  3. 试试这个查询:

    SELECT 
        `gps_unit`.`idgps_unit`, 
        `gps_unit`.`name as name`, 
        `gps_unit`.`notes as notes`, 
        `gps_unit`.`serial`, 
        `gps_unit_location`.`dt` as dt, 
        `gps_unit_location`.`idgps_unit_location`,
        `gps_unit_location`.`lat`, 
        `gps_unit_location`.`long`,
        `ip`,
        `unique_id`,
        `loc_age`,
        `reason_code`,
        `speed_kmh`,
        `VehHdg`,
        `Odometer`,
        `event_time_gmt_unix`,
        `switches`,
        `engine_on_off`
    FROM user 
    INNER JOIN user_to_gps_unit  ON user.iduser = user_to_gps_unit.iduser 
    INNER JOIN gps_unit          ON user_to_gps_unit.idgps_unit = gps_unit.idgps_unit
    INNER JOIN gps_unit_location ON gps_unit.idgps_unit = gps_unit_location.idgps_unit
    INNER JOIN
            (SELECT
                 `gps_unit_location`.`idgps_unit`,
                 MAX(`gps_unit_location`.`dt`) dtmax
             FROM `gps_unit_location`
             GROUP BY 1
             ) r1 ON r1.idgps_unit = gps_unit_location.idgps_unit AND r1.dtmax = gps_unit_location.dt
    WHERE 
        user.iduser = 14
    

在旁注中,我认为您不需要定义为主键的列上的唯一索引,这会导致插入/更新语句的写入开销。

于 2013-02-16T02:46:50.310 回答
1

通用的答案是索引那些用于连接和约束的列(ON 和 WHERE 子句)。使用复合索引(首先连接,然后是具有最低基数约束的约束)。

哦,让你所有的身份证都“未签名”。

于 2013-02-16T02:15:49.043 回答