1. Indexes: Make sure the tables have proper indexes on the columns used in the JOIN and WHERE clauses.

  2. Use INNER JOIN instead of LEFT JOIN: Since we are using a WHERE clause to filter out records from tt table, it's better to use INNER JOIN instead of LEFT JOIN.

  3. Use alias names: To make the query more readable and to avoid naming conflicts, use alias names for the tables.

  4. Avoid using RIGHT JOIN: Since we are looking for records in the PEA table, we should use LEFT JOIN instead of RIGHT JOIN with the PTB table.

  5. Use EXISTS instead of JOIN: Instead of joining all the tables, we can use EXISTS clause to check if a record exists in another table.

  6. Avoid using wildcard characters: Avoid using wildcard characters such as * to select all columns. Instead, select only the columns that are needed.

Optimized Query:

SELECT PEA.ID, PEA.NAME FROM PFQA_EVALUATION_ACTIVITY PEA WHERE EXISTS ( SELECT 1 FROM PFQA_TENANT_TEMPLATE TT JOIN PFQA_EVALUATION_TEMPLATE ET ON ET.TEMPLATE_CODE = TT.TEMPLATE_CODE JOIN PFQA_TEMPLATE_VERSION PTV ON PTV.TEMPLATE_CODE = TT.TEMPLATE_CODE WHERE TT.TENANT_ID = 'ce14722a826db28a81e1ac7bdc1442a0' AND PEA.TEMPLATE_VERSION_ID = PTV.ID

sql优化SELECT PEAID PEANAME FROM PFQA_EVALUATION_TEMPLATE et LEFT JOIN PFQA_TENANT_TEMPLATE tt ON etTEMPLATE_CODE = ttTEMPLATE_CODE RIGHT JOIN PFQA_TEMPLATE_VERSION ptv ON ttTEMPLATE_CODE = PTV TEMPLAT

原文地址: https://www.cveoy.top/t/topic/hwlz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录