SQL 优化:SELECT DISTINCT PTTE.ID, PTTE.NAME - 提升查询效率
To optimize the given SQL query, you can try the following:
-
Use INNER JOIN instead of LEFT JOIN if possible. Since you are filtering on the 'PET.STATUS' column in the WHERE clause, using INNER JOIN will only include matching records and might improve performance.
-
Avoid using DISTINCT if not necessary. In some cases, using DISTINCT can negatively impact performance. If the combination of 'PTTE.ID' and 'PTTE.NAME' is already unique, you can remove the DISTINCT keyword.
-
Make sure you have proper indexing on the join columns ('PTTE.ID', 'pet.TEMPLATE_TYPE_ID', 'pet.TEMPLATE_CODE', 'ptt.TEMPLATE_CODE', and 'PET.STATUS'). This will help the database engine to retrieve the data more efficiently.
Here is the optimized SQL query:
SELECT PTTE.ID, PTTE.NAME
FROM PFQA_TEMPLATE_TYPE PTTE
INNER JOIN PFQA_EVALUATION_TEMPLATE PET ON PTTE.ID = PET.TEMPLATE_TYPE_ID
INNER JOIN PFQA_TENANT_TEMPLATE PTT ON PET.TEMPLATE_CODE = PTT.TEMPLATE_CODE
WHERE PET.STATUS = 1;
Please note that the effectiveness of these optimizations may vary depending on the size of the tables and the data distribution. It's recommended to test and benchmark the query performance after implementing these changes.
原文地址: https://www.cveoy.top/t/topic/o3qY 著作权归作者所有。请勿转载和采集!