帮我优化一段sql:SELECT SUM(vin) AS vin_sum FROM (SELECT vin FROM hryt_gca_vehicle_audit_defect WHERE TO_CHAR(create_date,'yyyy mm iw') = TO_CHAR(Sysdate, 'yyyy mm iw') GROUP BY VIN ORDER BY dbms_random.value) WHERE rownum <= 10;
时间: 2023-05-24 14:01:49 浏览: 130
很高兴为您服务!这个SQL查询可以优化,您可以通过以下代码实现:
SELECT SUM(vin) AS vin_sum FROM
(
SELECT vin FROM
(
SELECT vin, rownum rn FROM
(
SELECT vin
FROM hryt_gca_vehicle_audit_defect
WHERE TO_CHAR(create_date,'yyyy mm iw') = TO_CHAR(Sysdate, 'yyyy mm iw')
GROUP BY VIN
ORDER BY dbms_random.value
)
)
WHERE rn <= 10
)
这个优化方案的思路是为内部查询添加行号,并且将ROWNUM筛选条件从外部查询中移除。这样效率更高,能够更快地获取结果。希望这个答案能帮助到您!
相关问题
帮我优化下这个sql : SELECT t4.vin, t3.msg_id AS trip_id, t3.trip_start_time, t4.switch_value AS has_trip FROM ( SELECT * FROM dds.dwd_driving_calendar t1 WHERE t1.vin = 'LSJA24396MS179029' AND 1681745400001 > t1.trip_start_time AND 1681745400001 < t1.trip_end_time ) t3 LEFT JOIN ( SELECT * FROM dds.vehicle_journey_switch_history t2 WHERE t2.vin IN ( 'LSJA24396MS179029' ) ) t4 ON t3.vin = t4.vin AND t4.switch_key = 'vehicle_journey_data' AND '2023-04-17 23:30:00' > t4.create_time AND '2023-04-17 23:30:00' < t4.update_time
可以尝试优化以下几个方面:
1. 使用EXISTS替换LEFT JOIN
可以使用EXISTS来代替LEFT JOIN进行连接查询,因为EXISTS在查询到一条匹配的数据后就会停止,而LEFT JOIN需要查询完整个表再返回结果。
```
SELECT t3.vin, t3.msg_id AS trip_id, t3.trip_start_time,
t4.switch_value AS has_trip
FROM dds.dwd_driving_calendar t3
WHERE t3.vin = 'LSJA24396MS179029'
AND 1681745400001 > t3.trip_start_time
AND 1681745400001 < t3.trip_end_time
AND EXISTS (
SELECT 1
FROM dds.vehicle_journey_switch_history t4
WHERE t3.vin = t4.vin
AND t4.switch_key = 'vehicle_journey_data'
AND '2023-04-17 23:30:00' > t4.create_time
AND '2023-04-17 23:30:00' < t4.update_time
)
```
2. 添加索引
在vin、trip_start_time、trip_end_time、switch_key、create_time和update_time列上添加索引,可以加速查询。
3. 不要使用SELECT *
在子查询中不要使用SELECT *,而是明确指定需要查询的列,可以减少不必要的数据读取和传输,提高查询效率。例如:
```
SELECT vin, msg_id AS trip_id, trip_start_time
FROM dds.dwd_driving_calendar
WHERE vin = 'LSJA24396MS179029'
AND 1681745400001 > trip_start_time
AND 1681745400001 < trip_end_time
```
```
SELECT vin, switch_value
FROM dds.vehicle_journey_switch_history
WHERE vin IN ('LSJA24396MS179029')
AND switch_key = 'vehicle_journey_data'
AND '2023-04-17 23:30:00' > create_time
AND '2023-04-17 23:30:00' < update_time
```
MySQL优化:select brandName,have_vin_count,(select count(1) from epc_carmodel_skucount where ifnull(vin,'') <> '') as have_vin_sum_count, concat(round(have_vin_count/(select count(1) from epc_carmodel_skucount where ifnull(vin,'') <> '')*100, 2),'%') as have_vin_percent from (select b.brandName,count(distinct a.vehicleUUID) as have_vin_count from (select distinct vehicleUUID from epc_carmodel_skucount where ifnull(vin,'') <> '') a inner join ( select b.brandName,b.vehicleUUID from epc_material_sence_model a join ( select * from epc_bom_sence where brandName not in ('演示','测试','客服内测') and ifnull(brandName,'') <> '' and brandName not like '火星%' and brandName not like '原厂%' ) b on a.oeId=b.oeId where a.categoryName='火花塞' ) b on a.vehicleUUID=b.vehicleUUID group by b.brandName
这段 SQL 语句是查询汽车品牌的火花塞车型中,有 VIN 码的车型数量及占比。其中使用了子查询和连接查询,可能会有性能问题。建议优化如下:
1. 尽量避免在子查询中使用聚合函数,可以将子查询的结果存储到临时表中,以提高查询效率。
2. 将子查询中的 ifnull(vin,'') <> '' 转换为 vin IS NOT NULL AND vin <> '',可以利用索引优化查询。
3. 使用 JOIN 操作时,尽量通过 ON 子句指定连接条件,避免使用 WHERE 子句进行过滤,提高查询效率。
优化后的 SQL 语句如下:
```
SELECT
b.brandName,
COUNT(DISTINCT a.vehicleUUID) AS have_vin_count,
COUNT(DISTINCT c.vehicleUUID) AS have_vin_sum_count,
CONCAT(ROUND(COUNT(DISTINCT a.vehicleUUID) / COUNT(DISTINCT c.vehicleUUID) * 100, 2), '%') AS have_vin_percent
FROM
epc_carmodel_skucount a
INNER JOIN (
SELECT
b.brandName,
b.vehicleUUID
FROM
epc_material_sence_model a
JOIN (
SELECT *
FROM epc_bom_sence
WHERE brandName NOT IN ('演示', '测试', '客服内测')
AND IFNULL(brandName, '') <> ''
AND brandName NOT LIKE '火星%'
AND brandName NOT LIKE '原厂%'
) b
ON a.oeId = b.oeId
WHERE a.categoryName = '火花塞'
) b
ON a.vehicleUUID = b.vehicleUUID
AND a.vin IS NOT NULL
AND a.vin <> ''
LEFT JOIN epc_carmodel_skucount c
ON c.vin IS NOT NULL
AND c.vin <> ''
GROUP BY
b.brandName
```
这样,可以避免子查询中的聚合函数,使用 INNER JOIN 操作指定连接条件,同时使用 LEFT JOIN 操作获取所有有 VIN 码的车型数量,从而提高查询效率。
阅读全文