Hi,I am optimizing the script below and have a question. As you will see they are creating the table (zcap_workspace) below.I added the primary key in an effort to speed things up. I am assuming that was the correct thing to do. Should I also add indexes to this table? Or will it slow down the inserts?Any other performance improvements I can make?[code="sql"]Set NoCount OnDECLARE @CAP_METHOD_NUM_MEM intSELECT @CAP_METHOD_NUM_MEM = population_select_method_idfrom dbo.population_select_methodwhere population_select_method_nm = 'Number of Members Assigned to PCP'DECLARE @CAP_METHOD_MEM_WITH_BEN_CON intSELECT @CAP_METHOD_MEM_WITH_BEN_CON = population_select_method_idfrom dbo.population_select_methodwhere population_select_method_nm = 'All Members With Benefit Contract'DECLARE @CAP_METHOD_PCP intSELECT @CAP_METHOD_PCP = population_select_method_idfrom dbo.population_select_methodwhere population_select_method_nm = 'All Members assigned to Provider'DECLARE @CAP_METHOD_PCP_IN_NET intSELECT @CAP_METHOD_PCP_IN_NET = population_select_method_idfrom population_select_methodwhere population_select_method_nm = 'All Members whose PCP is in Network'if objectproperty(object_id('dbo.zcap_workspace'), 'IsTable') = 1 drop table zcap_workspaceCreate Table zcap_workspace ( cap_workspace_id int IDENTITY (1, 1) NOT NULL, cap_run_id int NULL, test_date datetime NULL, provider_id int NULL, payor_id int NULL, benefitplan_id int NULL, medical_group_id int NULL, network_id int NULL, contract_id int NULL, cap_schedule_id int NULL, vendor_id int NULL, account_profile_id int NULL, management_percent float NULL, management_fee_before_withhold tinyint Null, withhold_percent float NULL, benefit_contract_type_id int NULL, retro_member_id int NULL, elig_cap_adjustment_id int NULL, prov_cap_adjustment_id int NULL, return_status int NULL, num_pcp_members_amount money NULL, population_select_method_id int NULL, processed tinyint Not Null, -- The following entries are only filled in during test-date retroactive -- processing. retro_original_cap_run_id int NULL, -- Original cap run this refers to. retro_adjusted_entry_id int NULL, -- The entry the adjustment refers to. retro_period_start datetime NULL, -- The retro_begin_date. retro_period_end datetime NULL, -- The retro_end_date. retro_eligibility_id int NULL, --added by RGC 12/11/2015 CONSTRAINT cap_workspace_id_pk PRIMARY KEY (cap_workspace_id))Exec dbo.usp_cap_L00_get_contracts @cap_run_id, @cap_run_provider_id, @cap_run_payor_id, @cap_run_contract_id, @test_dateDeclare CapWorkspace_cur Cursor Static For Select cap_workspace_id, provider_id, contract_id, population_select_method_id From dbo.zcap_workspace Order By provider_id, contract_id Desc, payor_id DescOpen CapWorkspace_curDeclare @Return_Value int, @Member_Count int, @AdjustmentsMade intSelect @NumContracts = 0, @DuplicateCount = 0, @TotalMembers = 0Declare @cap_workspace_id int, @provider_id int, @contract_id int, @population_select_method_id intFetch CapWorkspace_cur Into @cap_workspace_id, @provider_id, @contract_id, @population_select_method_idWhile @@FETCH_STATUS = 0Begin If Exists ( Select * From dbo.cap_per_member AS cpm Inner Join dbo.cap_run AS cr On cpm.original_cap_run_id = cr.cap_run_id And cr.begin_date = @begin_date And cpm.contract_id = @contract_id And cpm.provider_id = @provider_id ) Begin --exec usp_cap_aaa_log @cap_run_id, '. Duplicate contract -', @iValue = @contract_id --exec usp_cap_aaa_log @cap_run_id, '. Provider -', @iValue = @provider_id --exec usp_cap_aaa_log @cap_run_id, '. Payor -', @iValue = @cap_run_payor_id Goto Next_Provider End If @population_select_method_id = @CAP_METHOD_NUM_MEM exec @member_count = dbo.usp_cap_L20_number_pcp_members @cap_workspace_id Else If @population_select_method_id = @CAP_METHOD_MEM_WITH_BEN_CON exec @member_count = dbo.usp_cap_L20_mem_with_ben_con @cap_workspace_id Else If @population_select_method_id = @CAP_METHOD_PCP_IN_NET exec @member_count = dbo.usp_cap_L20_members_pcp_in_net @cap_workspace_id Else If @population_select_method_id = @CAP_METHOD_PCP exec @member_count = dbo.usp_cap_L20_members_pcp @cap_workspace_id Else Begin --exec usp_cap_aaa_log @cap_run_id,'. Assigned contract method has not been implemented.' Goto Next_Provider End Select @NumContracts = @NumContracts + 1 --exec usp_cap_aaa_log @cap_run_id, '. Members Capitated = ', @member_count If @member_count > 0 Select @TotalMembers = @TotalMembers + IsNull(@member_count,0)Next_Provider: Fetch CapWorkspace_cur Into @cap_workspace_id, @provider_id, @contract_id, @population_select_method_idEnd -- While @@FETCH_STATUS = 0Close CapWorkspace_curDeallocate CapWorkspace_cur-- The processed flag is used by retro to figure out which contracts have been looked at in the workspace table.-- At this point all of the contracts in the workspace table are current capitation contracts and can be marked-- as processed.Update dbo.zcap_workspaceSet processed = 1Declare @map_stored_procedure varchar(50)-- Map Sequence 3210-- This map sequence is used to process retro outside of the contractSelect @map_stored_procedure = nullSelect @map_stored_procedure = stored_procedure From dbo.client_map (NOLOCK)Where map_sequence_number = 3210If @map_stored_procedure is not nullBEGIN --exec usp_cap_aaa_log @cap_run_id,'. Starting retro...' exec @AdjustmentsMade = @map_stored_procedure @cap_run_id Select @TotalMembers = @TotalMembers + IsNull(@AdjustmentsMade,0) --exec usp_cap_aaa_log @cap_run_id,'. Number of retro adjustments = ',@ivalue = @AdjustmentsMadeEND-- Map Sequence 3110-- This one is used for computing all the management fees, withholdings and creating-- the cap_run_detail records. This is done when the 3100 compute-per-contract method-- is not used.Select @map_stored_procedure = nullSelect @map_stored_procedure = stored_procedure From client_map (NOLOCK)Where map_sequence_number = 3110If @map_stored_procedure is not nullBEGIN Exec dbo.usp_cap_L00_get_contracts @cap_run_id, @cap_run_provider_id, @cap_run_payor_id, @cap_run_contract_id, @test_date --exec usp_cap_aaa_log @cap_run_id,'. Computing totals...' exec @map_stored_procedure @cap_run_id --exec usp_cap_aaa_log @cap_run_id,'. Totals done.'END-- Summarize demographic breakdown.Insert Into dbo.cap_run_breakdown ( cap_run_detail_id, cap_schedule_detail_id, cap_run_id, amount, cap_run_count, contract_id, payor_id, provider_id )Select cap_per_member.cap_run_detail_id, cap_per_member.cap_schedule_detail_id, @cap_run_id, Sum(cap_per_member.amount) AS SumOfamount, Count(cap_per_member.amount) AS CountOfamount, cap_run_detail.contract_id, cap_run_detail.payor_id, cap_run_detail.provider_idFrom dbo.cap_per_member Inner Join dbo.cap_run_detail On cap_per_member.cap_run_detail_id = cap_run_detail.cap_run_detail_idWhere cap_per_member.cap_run_id = @cap_run_idGroup By cap_per_member.cap_run_detail_id, cap_per_member.cap_schedule_detail_id, cap_run_detail.contract_id, cap_run_detail.payor_id, cap_run_detail.provider_id--exec usp_cap_aaa_log @cap_run_id,'. Total Capitated (with adjustments) = ',@iValue = @TotalMembers[/code]
↧