优化以下SQL,给出优化后的SQL:SELECT un.*, sta.*, CASE WHEN COALESCE(un.lot_time, 0) > COALESCE(sta.dock_time_limit, 0) THEN COALESCE(un.lot_time, 0) - COALESCE(sta.dock_time_limit, 0) ELSE 0 END AS lotDuration FROM unload_over_time un LEFT JOIN ( SELECT parking_lot_code, dock_time_limit FROM stall_infor WHERE data_state = 0 AND delete_state = 1 AND industry_park_code='YQWQML' ) sta ON un.parking_lot_code = sta.parking_lot_code INNER JOIN ( SELECT DISTINCT plate_number FROM supplier_info WHERE data_state = 0 AND delete_state = 1 AND arrived_atetime IS NOT NULL AND left_datetime IS NULL AND industry_park_code='YQWQML' ) sup ON un.plate_number = sup.plate_number WHERE un.data_state = 0 AND un.delete_state = 1 AND un.is_out_of_time = 1 AND un.industry_park_code='YQWQML' AND un.parking_lot_code IN ( SELECT DISTINCT parking_lot_code FROM unload_over_time WHERE data_state = 0 AND delete_state = 1 AND is_out_of_time = 1 AND industry_park_code='YQWQML' )
时间: 2024-04-14 20:25:59 浏览: 183
RT5370_Linux_STA_V2.5.0.3.rar_5370_V2 _rt5370 linux driv
优化后的SQL如下:
```sql
SELECT un.*, sta.*,
CASE WHEN COALESCE(un.lot_time, 0) > COALESCE(sta.dock_time_limit, 0) THEN COALESCE(un.lot_time, 0) - COALESCE(sta.dock_time_limit, 0) ELSE 0 END AS lotDuration
FROM unload_over_time un
LEFT JOIN stall_infor sta ON un.parking_lot_code = sta.parking_lot_code
INNER JOIN supplier_info sup ON un.plate_number = sup.plate_number
WHERE un.data_state = 0
AND un.delete_state = 1
AND un.is_out_of_time = 1
AND un.industry_park_code='YQWQML'
AND EXISTS (
SELECT 1
FROM unload_over_time uot
WHERE uot.data_state = 0
AND uot.delete_state = 1
AND uot.is_out_of_time = 1
AND uot.industry_park_code='YQWQML'
AND uot.parking_lot_code = un.parking_lot_code
)
AND EXISTS (
SELECT 1
FROM stall_infor si
WHERE si.data_state = 0
AND si.delete_state = 1
AND si.industry_park_code='YQWQML'
AND si.parking_lot_code = un.parking_lot_code
)
AND EXISTS (
SELECT 1
FROM supplier_info sp
WHERE sp.data_state = 0
AND sp.delete_state = 1
AND sp.arrived_atetime IS NOT NULL
AND sp.left_datetime IS NULL
AND sp.industry_park_code='YQWQML'
AND sp.plate_number = un.plate_number
)
```
优化后的SQL做了以下几点改进:
1. 将子查询转化为 EXISTS 子句,避免使用 DISTINCT 关键字和 IN 子查询,提高查询性能。
2. 将 LEFT JOIN 子查询转化为直接的 LEFT JOIN 操作,避免嵌套查询,简化SQL结构。
3. 合并了相同的过滤条件,减少重复的条件判断。
优化后的SQL应该能够提高查询性能。但请注意,在实际应用中,最佳的优化方案还需要根据具体的数据量、索引等因素进行调整和测试。
阅读全文