Hi All,
I am tuning the below stored procedure. The response time for this stored procedure is around 2 seconds. It is nested up to 5 levels. I don't see any missing index on the corresponding tables. I don't see anything wrong with the execution plan. Any help here will be highly appreciated.
ALTER PROCEDURE [dbo].[p_get_dep] @a_mship_id NUMERIC(9, 0) AS BEGIN SET NOCOUNT ON; --Declare Local Variables DECLARE @person_id NUMERIC(9, 0) ,@wo_hosp_days NUMERIC(9, 0) ,@days_wo_hospital NUMERIC(9, 0); --Create Temp Tables CREATE TABLE #stud_year ( person_id NUMERIC(9, 0) ,given_names VARCHAR(40) NULL ,surname VARCHAR(40) NULL ,gender CHAR(1) NULL ,date_of_birth DATETIME NULL ,age INT NULL ,title NUMERIC(5) NULL ,deceased_flag CHAR(1) NULL ,clearance_status CHAR(1) NULL ,student_year NUMERIC(4) NULL ,termination_date DATETIME NULL ,termination_code CHAR(1) NULL ,join_date DATETIME NULL ,membership_id NUMERIC(9, 0) ,status_flag CHAR(1) NULL ,relationship CHAR(10) NULL ,create_operator CHAR(16) NULL ,create_datetime DATETIME NULL ,waiting_period CHAR(60) NULL ,age_at_entry NUMERIC(3, 0) NULL ,no_hospital_days INT NULL ,update_operator CHAR(16) NULL ,update_datetime DATETIME NULL ,promotion CHAR(60) NULL ,remove_lhc CHAR(1) NULL ,relationship_sort_order INT NULL ,status_flag_sort_order INT NULL ,paid_lhc_since DATETIME NULL ); CREATE TABLE #wo_hospday ( person_id NUMERIC(9, 0) ,no_hospital_days INT ); --Insert Data into Temp tables INSERT #stud_year SELECT p.person_id ,left(rtrim(ltrim(isNull(rtrim(p.first_name), '') + ' ' + isNull(p.other_names, ''))), 40) ,p.surname ,p.gender ,p.date_of_birth ,CASE WHEN sysdb.ssma_syb.ssma_datepart('mm', p.date_of_birth) < sysdb.ssma_syb.ssma_datepart('mm', getdate()) THEN sysdb.ssma_syb.ssma_datediff('yy', p.date_of_birth, getdate()) WHEN sysdb.ssma_syb.ssma_datepart('mm', p.date_of_birth) = sysdb.ssma_syb.ssma_datepart('mm', getdate()) AND sysdb.ssma_syb.ssma_datepart('dd', p.date_of_birth) <= sysdb.ssma_syb.ssma_datepart('dd', getdate()) THEN sysdb.ssma_syb.ssma_datediff('yy', p.date_of_birth, getdate()) ELSE sysdb.ssma_syb.ssma_datediff('yy', p.date_of_birth, getdate()) - 1 END ,p.title ,p.deceased_flag ,p.clearance_status ,isnull(ps.student_year, 0) ,pm.termination_date ,pm.termination_code ,pm.join_date ,pm.membership_id ,pm.status_flag ,pm.relationship ,pm.create_operator ,pm.create_datetime ,pm.eligibility_status ,p.age_at_entry ,p.no_hospital_days ,pm.update_operator ,pm.update_datetime ,CONVERT(VARCHAR(60), NULL) ,p.remove_lhc ,CASE WHEN pm.relationship = 1 THEN 1 WHEN pm.relationship = 2 THEN 2 WHEN pm.relationship = 3 THEN 2 WHEN pm.relationship > 3 THEN 3 END ,CASE WHEN pm.status_flag = 'A' THEN 1 WHEN pm.status_flag <> 'A' THEN 2 END ,NULL FROM dbo.person_membership pm INNER JOIN dbo.person p ON p.person_id = pm.person_id LEFT JOIN dbo.person_studentyear ps ON p.person_id = ps.person_id WHERE pm.membership_id = @a_mship_id AND p.person_id = pm.person_id --Update the Temp Tables UPDATE sy SET sy.promotion = p.description FROM dbo.promotion p INNER JOIN dbo.promotion_reference pr ON p.promotion_id = pr.promotion_id AND pr.main_ref_type = 'P' INNER JOIN #stud_year sy ON sy.person_id = CONVERT(NUMERIC(9), pr.main_ref) AND pr.main_ref_type='P' WHERE pr.status_date = ( SELECT max(pr1.status_date) FROM dbo.promotion_reference pr1 WHERE pr1.main_ref_type = 'P' AND sy.person_id = CONVERT(NUMERIC(9), pr1.main_ref) ) --********************************************************************** --*** Update The No. hospital days --*********************************************************************** DECLARE csr_wohosp CURSOR FAST_FORWARD FOR SELECT DISTINCT sy.person_id FROM #stud_year sy OPEN csr_wohosp FETCH NEXT FROM csr_wohosp INTO @person_id WHILE @@FETCH_STATUS = 0 BEGIN SELECT @wo_hosp_days = 0 EXECUTE dbo.p_get_days_wo_hospital @person_id ,NULL ,NULL ,@wo_hosp_days OUTPUT ,'N' ,NULL IF @wo_hosp_days = NULL SELECT @wo_hosp_days = 0 INSERT #wo_hospday SELECT @person_id ,@wo_hosp_days FETCH NEXT FROM csr_wohosp INTO @person_id END CLOSE csr_wohosp DEALLOCATE csr_wohosp UPDATE #stud_year SET no_hospital_days = #wo_hospday.no_hospital_days FROM #stud_year ,#wo_hospday WHERE #stud_year.person_id = #wo_hospday.person_id UPDATE #stud_year SET #stud_year.paid_lhc_since = ( SELECT TOP (1) paid_lhc_since FROM person WHERE person_id = @person_id ) WHERE #stud_year.relationship = 1 SELECT sy.relationship ,sy.given_names ,sy.surname ,sy.gender ,sy.date_of_birth ,sy.join_date ,max(sy.student_year) AS student_year ,sy.termination_date ,sy.termination_code ,sy.person_id ,sy.membership_id ,sy.title ,sy.deceased_flag ,sy.status_flag ,sy.age ,sy.clearance_status ,sy.waiting_period ,sy.create_operator ,sy.create_datetime ,sy.age_at_entry ,sy.no_hospital_days ,sy.update_operator ,sy.update_datetime ,sy.promotion ,sy.remove_lhc ,sy.paid_lhc_since FROM #stud_year sy GROUP BY sy.relationship ,sy.given_names ,sy.surname ,sy.gender ,sy.date_of_birth ,sy.join_date ,sy.termination_date ,sy.termination_code ,sy.person_id ,sy.membership_id ,sy.title ,sy.deceased_flag ,sy.status_flag ,sy.age ,sy.clearance_status ,sy.create_operator ,sy.create_datetime ,sy.waiting_period ,sy.age_at_entry ,sy.no_hospital_days ,sy.update_operator ,sy.update_datetime ,sy.promotion ,sy.remove_lhc ,sy.paid_lhc_since ,sy.relationship_sort_order ,sy.status_flag_sort_order ORDER BY sy.status_flag_sort_order ASC ,sy.relationship_sort_order ASC ,sy.date_of_birth ASC END