这条sql如何优化 select sumadvance_gold from artb_charge_record where company_code = atrcompany_code and serial_number = atrserial_number and archive = 0 and state = 0 and atrheating_year heating_year and
-
Index Optimization: Create indexes on columns used in the WHERE clause, such as company_code, serial_number, archive, state, and heating_year.
-
Subquery Optimization: Instead of using a subquery, join the artb_charge_record table with the atr table and use GROUP BY to calculate the sum.
-
Use COALESCE instead of IS NOT NULL and != 0: Replace "advance_gold is not null and advance_gold != 0" with "COALESCE(advance_gold, 0) <> 0" for better performance.
-
Reduce Redundant Conditions: Remove the condition "and atr.heating_year > heating_year" if it is not necessary.
-
Use INNER JOIN instead of WHERE clause: Use INNER JOIN to join the artb_charge_record table with the atr table instead of using conditions in the WHERE clause.
Overall, optimizing the indexes and rewriting the query using join and group by could significantly improve the performance of the query
原文地址: https://www.cveoy.top/t/topic/eN43 著作权归作者所有。请勿转载和采集!