保存参数查询
按批次250501SA001查661360700C件号ST20 RECEIVE CRIMPING DATA
select '661360700C' as Part_number, et_stocpf,cartra_valeur,t_carath.et_unite,t_carath.carath_libelle,t_cartra.datemodif,t_cartra.heuremodif from t_cartra,t_carath
where et_stocpf in(select ID_STOCPF from t_stocpf where ET_LOTPF='250501SA001')
and t_cartra.et_carath=t_carath.id_carath and carath_libelle='ST20 RECEIVE CRIMPING DATA'
这个 SQL 查询在大数据量下运行缓慢,通常是因为 隐式笛卡尔积(缺少连接条件)、子查询效率低 以及 索引缺失 造成的。
以下是针对SQL 进行的几个优化步骤:
1. 改用显式 JOIN 代替子查询
当前的 IN (SELECT ...) 结构在某些数据库引擎中会导致外部查询的每一行都去执行一遍子查询。将其改为 JOIN 可以让优化器更好地利用索引。
2. 使用标准的 INNER JOIN 语法
将 FROM t_cartra, t_carath 这种逗号分隔的写法改为 JOIN ... ON。这不仅提高可读性,也能防止漏写关联条件导致的性能灾难。
优化后查询:
SELECT
tc.et_stocpf,
tc.cartra_valeur,
th.et_unite,
th.carath_libelle,
tc.datemodif,
tc.heuremodif
FROM t_cartra tc
INNER JOIN t_carath th ON tc.et_carath = th.id_carath
INNER JOIN t_stocpf ts ON tc.et_stocpf = ts.ID_STOCPF
WHERE ts.datemodif = '日期'
AND ts.nommodif = '线体用户'
AND th.carath_libelle IN (
'St05_st10 Press force',
'St05_st10 Press stroke',
'ST15_st12_t_bar_AssyPress',
'ST15_st12_t_bar_AssyPress_Stroke'
);
浙公网安备 33010602011771号