今天帮开发人员优化SQL,发现一个以前不怎么在意的问题
SELECT bond.store_no FROM bill_order_num_dtl bond WHERE EXISTS ( select 1 from meetorder_staff_dtl b , meetorder_staff a where b.operate_area = bond.store_no and a.seq_no = b.seq_no and a.user_code = ‘lym‘ ) ;
查询需要2S,bill_order_num_dtl 数据量大概2W多条,meetorder_staff_dtl 50条左右,将EXISTS换为INNER JOIN后只需0.02S,差了100倍,EXISTS在命中率高的情况下查询速度较快,像这种需要判断的表的数据量较大,而条件表的数据量较少时不宜使用。INNER JOIN相对较稳定一些,不会随命中率的变化而影响性能。改成下面后,只需0.02S
SELECT bond.store_no FROM bill_order_num_dtl bond INNER JOIN meetorder_staff_dtl b on b.operate_area = bond.store_no INNER JOIN meetorder_staff a on a.seq_no = b.seq_no WHERE a.user_code = ‘lym‘
时间: 2024-12-23 23:17:11