To optimize the provided SQL query and improve its performance, we'll focus on the following strategies:
-
Eliminate Unnecessary Subqueries and Joins: The subquery involving study_group_subgroup (sgs) can be simplified to reduce complexity and improve performance.
-
Avoid Using NVL and NVL2 Functions in Joins and WHERE Clauses: Rewriting the query to avoid these functions can enhance performance since NVL functions prevent the use of indexes.
-
Minimize Data Retrieval: Only select necessary columns and avoid bringing in unused data.
-
Optimize Aggregations: Simplify aggregation functions where possible.
Here's the optimized query with explanations:
-- Simplify the subquery by directly joining the necessary tables
WITH active_study_groups AS (
SELECT study_group_id, study_group_state_id
FROM study_group
WHERE study_group_state_id = 1
),
active_study_group_subgroups AS (
SELECT sgs.study_group_subgroup_id, sgs.study_group_id
FROM study_group_subgroup sgs
INNER JOIN study_group sg ON sgs.study_group_id = sg.study_group_id
WHERE sg.study_group_state_id = 1
),
study_flow_content_details AS (
SELECT sfc.study_flow_id,
sfc.study_flow_content_id,
sfc.study_group_id,
sfc.study_group_subgroup_id,
sfc.stud_cnt,
sf.discipline_id,
wgy.study_year_id,
ws.session_type_id,
COALESCE(sfc.study_group_id, sfc.study_group_subgroup_id) AS group_id
FROM study_flow_contents sfc
INNER JOIN study_flow sf ON sf.study_flow_id = sfc.study_flow_id
LEFT JOIN active_study_groups sg ON sfc.study_group_id = sg.study_group_id
LEFT JOIN active_study_group_subgroups sgs ON sfc.study_group_subgroup_id = sgs.study_group_subgroup_id
LEFT JOIN wnp_by_group_year wgy ON wgy.study_group_id = COALESCE(sg.study_group_id, sgs.study_group_id)
LEFT JOIN WNP_SEMESTER ws ON ws.wnp_semester_id = wgy.wnp_semester_id
WHERE sg.study_group_id IS NOT NULL OR sgs.study_group_subgroup_id IS NOT NULL
)
SELECT sfc.study_flow_id,
sfc.discipline_id,
sfc.study_year_id,
sfc.session_type_id,
MIN(CASE WHEN sfc.stud_cnt IS NULL THEN 0 ELSE 1 END) || MAX(CASE WHEN sfc.stud_cnt IS NULL THEN 0 ELSE 1 END) AS partial_flag,
SHA256.ENCRYPT(LISTAGG(sfc.group_id, ',') WITHIN GROUP (ORDER BY sfc.group_id)) AS groups_hash
FROM study_flow_content_details sfc
GROUP BY sfc.study_flow_id,
sfc.discipline_id,
sfc.study_year_id,
sfc.session_type_id;
Explanation:
-
Common Table Expressions (CTEs):
active_study_groups: Retrieves active study groups.
active_study_group_subgroups: Retrieves active study group subgroups by joining with study_group to ensure the parent group is active.
study_flow_content_details: Combines data from study_flow_contents, study_flow, and other necessary tables, while avoiding NVL functions and bringing in only the necessary columns.
-
Eliminating NVL and NVL2:
- Replaced
NVL and NVL2 with COALESCE, which can be more efficient.
- In
partial_flag, used a CASE statement to compute the flag without NVL2.
-
Simplifying Joins:
- Removed unnecessary columns and joins that are not used in the final output.
- Ensured that joins are using efficient join conditions and leveraging indexes by using direct
INNER JOIN or LEFT JOIN where appropriate.
-
Optimizing Aggregations:
- Used
CASE statements in MIN and MAX functions to compute partial_flag.
- Ensured that the
LISTAGG function only includes necessary data and ordered by group_id.
-
Overall Performance Improvement:
- By restructuring the query, we reduce the complexity and the amount of data processed.
- The use of CTEs can improve readability and may help the query optimizer generate a better execution plan.
Note: This optimization assumes that:
- The
study_group_state_id indicates the active state when it is 1.
- The
study_group_id and study_group_subgroup_id are mutually exclusive in study_flow_contents.
- The necessary indexes are in place on key columns used in joins (e.g.,
study_group_id, study_flow_id, etc.).
By applying these optimizations, the query should perform faster due to reduced complexity, avoidance of functions that inhibit index usage, and minimized data retrieval.