优化代码insert into fr.fr_equipment_fill_out_all select date2 ,equipment ,team ,target_oee,theory_qty ,theory_ppm ,stop_time ,plan_downtime ,text1 ,text2 ,text3 ,text4 ,text5 from ( select ROW_NUMBER() over(partition by t1.equipment order by t1.date1) as num,neighbor(t1.date1,1) as date2,neighbor(t1.equipment,1) as equipment ,t1.date1,t1.equipment,t2.* from ( select DISTINCT a.date1 , b.equipment from (select toDate (`date`) as date1 from chuz_dim.dim_mes_date_all) a GLOBAL left join (select toDate(date_time) as date_time1, * from fr.fr_equipment_fill_out_all) b on 1=1 where date1 between (select min(date_time) from fr.fr_equipment_fill_out_all) and now() ) t1 GLOBAL left join ( select a.date1 , b.* from (select toDate (`date`) as date1 from chuz_dim.dim_mes_date_all) a GLOBAL left join (select toDate(date_time) as date_time1, * from fr.fr_equipment_fill_out_all) b on date1=date_time1 where date1 between (select MIN(date_time) from fr.fr_equipment_fill_out_all) and now() )t2 on t1.date1=t2.date1 and t1.equipment=t2.equipment order by t1.equipment ,t1.date1 ) t3 where date2 > date1 and date_time = (select max(date_time) from fr.fr_equipment_fill_out_all)
时间: 2024-04-09 17:32:50 浏览: 97
为了优化这段代码,可以考虑以下几点:
1. 使用JOIN代替子查询:将子查询中的表连接操作改为使用JOIN语句,可以提高查询效率。
2. 使用EXISTS代替IN:将子查询中的IN子句改为EXISTS子句,可以提高查询效率。
3. 使用变量存储重复的表达式:在查询中使用重复的表达式会增加计算成本,可以使用变量存储这些重复的表达式,以减少计算次数。
4. 使用索引:根据查询条件和连接条件,在关键列上创建索引,可以提高查询性能。
下面是优化后的代码:
```sql
INSERT INTO fr.fr_equipment_fill_out_all
SELECT date2, equipment, team, target_oee, theory_qty, theory_ppm, stop_time, plan_downtime, text1, text2, text3, text4, text5
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY t1.equipment ORDER BY t1.date1) AS num,
neighbor(t1.date1, 1) AS date2,
neighbor(t1.equipment, 1) AS equipment,
t1.date1,
t1.equipment,
t2.*
FROM (
SELECT DISTINCT a.date1, b.equipment
FROM chuz_dim.dim_mes_date_all a
LEFT JOIN fr.fr_equipment_fill_out_all b ON 1 = 1
WHERE date1 BETWEEN (SELECT MIN(date_time) FROM fr.fr_equipment_fill_out_all) AND NOW()
) t1
LEFT JOIN (
SELECT a.date1, b.*
FROM chuz_dim.dim_mes_date_all a
LEFT JOIN fr.fr_equipment_fill_out_all b ON a.date1 = toDate(b.date_time)
WHERE date1 BETWEEN (SELECT MIN(date_time) FROM fr.fr_equipment_fill_out_all) AND NOW()
) t2 ON t1.date1 = t2.date1 AND t1.equipment = t2.equipment
WHERE date2 > date1
AND date_time = (SELECT MAX(date_time) FROM fr.fr_equipment_fill_out_all)
) t3;
```
请注意,这只是一种可能的优化方法,具体的优化策略还需要根据数据量和表结构进行调整。在实际应用中,建议根据数据库的性能特点进行测试和调整,以获得最佳的查询性能。
阅读全文