Today, I helped developers optimize SQL and found a problem that I didn't care much about before.
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‘ ) ;
The query requires 2 s, bill_order_num_dtl has more than 2 W data records, and meetorder_staff_dtl has about 50 records. After replacing exists with inner join, it only takes 0.02 s, which is 100 times worse, when the hit rate of exists is high, the query speed is faster. For example, the amount of data in the table to be judged is large, and it is not suitable to use a small amount of data in the condition table. Inner join is relatively stable and does not affect performance as the hit rate changes. After it is changed to the following, it only takes 0.02 s
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‘
Exists and inner join