Mysql嵌套查询速度非常慢

时间:2016-12-25 09:38:53

标签: mysql

我有一张名为vehicle_plate的表格如下:

enter image description here

上表总记录 715210 我想在特定区域和省份按最新或更新的车型计算车辆,但是当我运行以下查询时,查询速度非常快慢。我的意思是我从来没有得到它刚加载的结果。

Hook<some_function> ...

注意:一辆车可能有一个以上的车牌,我想计算更新的或最新的车牌类型。

示例数据: enter image description here

1 个答案:

答案 0 :(得分:2)

有时内连接比in子句

更高效
      SELECT `pt`.`pt_dr` AS
        TYPE , COUNT( DISTINCT vp.vehicle_v_id ) AS vehicles, `p`.`province_dr` AS province_name, `p`.`id` AS id, vp.vehicle_v_id
        FROM (
        `vehicle_plate` AS vp
        )
        LEFT JOIN provinces AS p ON p.id = vp.provinces_province_id
        LEFT JOIN plate_types AS pt ON pt.id = vp.plate_types_pt_id
        INNER JOIN   (

        SELECT vp1.id as id
        FROM vehicle_plate AS vp1
        GROUP BY vp1.vehicle_v_id
        ) t on vp.id  = t.id 
        WHERE 1
        AND p.zone_zone_id = '1'
        AND p.id = '1'

        GROUP BY vp.vehicle_v_id, `vp`.`plate_types_pt_id`
        ORDER BY `p`.`zone_zone_id` , `vp`.`provinces_province_id` , `vehicles` DESC