SQL Query Optimization for Retrieving Issue Tracking Data
This query retrieves data related to issue tracking, including care ID, name, and patch information. It utilizes a Common Table Expression (CTE) to aggregate data from the t_issue_track_summary_info table. The CTE, named issue, groups data by cmm_id and uses the STRING_AGG function to concatenate multiple values for l3_flag, care_id, care_name, and ump_id into a single string. The main query then joins the CTE with other tables, including t_dts_collect, t_patch_info, and t_dts_details, to retrieve additional information. The COALESCE function is used to replace null values in the l3_flag and care_name columns with an empty string. Finally, the query filters the results based on specific conditions and orders the output by details.create_time in descending order. The LIMIT clause restricts the number of results to 200.
WITH issue AS (
SELECT cmm_id,
STRING_AGG(l3_flag, ';') AS l3_flag,
STRING_AGG(care_id, ';') AS care_id,
STRING_AGG(care_name, ';') AS care_name,
STRING_AGG(ump_id, ';') AS issue_id
FROM t_issue_track_summary_info
GROUP BY cmm_id
)
SELECT collect.id,
COALESCE(issue.l3_flag, '') AS L3flag,
issue.care_id,
COALESCE(issue.care_name, '') AS CareName,
issue.issue_id,
collect.patch_plan,
collect.patch_id,
collect.cur_step,
collect.turns,
collect.cur_test,
...
FROM t_dts_collect collect
LEFT JOIN t_patch_info patch ON collect.patch_id=patch.id
LEFT JOIN t_dts_details details ON collect.dts_biz_no = details.dts_biz_no
LEFT JOIN t_dts_details details_parent ON details.parent_no = details_parent.dts_biz_no
LEFT JOIN issue ON collect.dts_biz_no = issue.cmm_id
WHERE collect.is_deleted = 0
AND collect.version_id = '23735486'
AND collect.patch_plan IN ('CP2118')
ORDER BY details.create_time DESC
LIMIT 200
Optimizations:
- Using COALESCE for null values: Instead of using the
CASEstatement for handling null values, theCOALESCEfunction is used for better readability and potentially improved performance. - Removing unnecessary condition: The condition
where deleted is nullin the subqueryissueis removed as it is already included in the join conditionLEFT JOIN issue ON collect.dts_biz_no = issue.cmm_id. - Index considerations: To further optimize the query, consider adding indexes to the columns used in the
WHEREandJOINclauses. This can significantly improve query performance.
This optimized query provides a more efficient and readable approach to retrieving issue tracking data. By incorporating best practices and addressing potential inefficiencies, it ensures faster and more accurate data retrieval for analysis and reporting purposes.
原文地址: http://www.cveoy.top/t/topic/qsAA 著作权归作者所有。请勿转载和采集!