Using BizTalk to call a SQL stored procedure (see below), which;
Writes data to 5 tables
Checks each table to see if the record exists
Updates or Inserts accordingly
The problem is that when BizTalk receives hundreds of calls, the stored procedure is called each time which appears to add an overhead to the SQL server and eventually causes SQL timeout errors returned in BizTalk, resulting in the data not being written to the DB.
Can anyone advise on the best way to optimise my query so that it processes these tables without much overhead, or have I got it optimised enough already?
USE [MDH]
GO
/****** Object: StoredProcedure [dbo].[spcuPersonStudentProgrammeModule] Script Date: 8/15/2022 2:29:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spcuPersonStudentProgrammeModule]
-- person/personref params
@student_id VARCHAR(max)
,@account_id VARCHAR(max) = NULL
,@prefix_honorific VARCHAR(max) = NULL
,@first_name VARCHAR(max) = NULL
,@middle_name VARCHAR(max) = NULL
,@surname VARCHAR(max) = NULL
,@familiar_name VARCHAR(max) = NULL
,@date_of_birth DATE = NULL
,@external_email_address VARCHAR(max) = NULL
,@mobile_phone_no VARCHAR(max) = NULL
,@gender VARCHAR(max) = NULL
,@ethnicity VARCHAR(max) = NULL
,@domicile VARCHAR(max) = NULL
,@disability VARCHAR(max) = NULL
,@nationality VARCHAR(max) = NULL
,@telephone_no VARCHAR(max) = NULL
,@prev_surname VARCHAR(max) = NULL
,@country_of_birth VARCHAR(max) = NULL
-- student params
,@student_email_address VARCHAR(max) = NULL
,@currently_studying_flag VARCHAR(max) = NULL
,@HesaStudentID VARCHAR(max) = NULL
,@UCAS_ID VARCHAR(max) = NULL
,@uln VARCHAR(max) = NULL
,@VisaReq VARCHAR(max) = NULL
,@PurposeOfResidency VARCHAR(max) = NULL
,@cas_status VARCHAR(max) = NULL
,@student_status VARCHAR(max) = NULL
,@source_system VARCHAR(max) = NULL
,@main_programme_code VARCHAR(max) = NULL
,@type VARCHAR(max) = NULL
,@student_support_no VARCHAR(max) = NULL
,@exam_id VARCHAR(max) = NULL
,@su_opt VARCHAR(max) = NULL
,@change_type VARCHAR(max) = NULL
,@international_sponsored_students varchar(80) = null
,@visa_type VARCHAR(max) = null
-- student_programmes params
,@programme_code VARCHAR(50)
,@programme_description VARCHAR(MAX) = NULL
,@start_date DATETIME = NULL
,@end_date DATETIME = NULL
,@mdh_stage_code VARCHAR(MAX) = NULL
,@main_award_flag VARCHAR(10) = NULL
,@load_category VARCHAR(10) = NULL
,@qualification_level VARCHAR(10) = NULL
,@student_study_level VARCHAR(10) = NULL
,@school_code VARCHAR(10) = NULL
,@college_code VARCHAR(10) = NULL
,@campus_code VARCHAR(10) = NULL
,@graduate_yn VARCHAR(10) = NULL
,@is_wbdl VARCHAR(80) = NULL
,@ul_qual_aim VARCHAR(MAX) = NULL
,@ul_qual_aim_desc VARCHAR(MAX) = NULL
-- student_modules params
,@module_code VARCHAR(50)
,@module_desc VARCHAR(MAX) = NULL
,@mod_date_time DATETIME = NULL
-- student_address params
,@perm_address1 VARCHAR(50) = NULL
,@perm_address2 VARCHAR(50) = NULL
,@perm_address3 VARCHAR(50) = NULL
,@perm_address4 VARCHAR(50) = NULL
,@perm_address5 VARCHAR(50) = NULL
,@perm_postcode VARCHAR(50) = NULL
,@perm_country_code VARCHAR(50) = NULL
,@term_address1 VARCHAR(50) = NULL
,@term_address2 VARCHAR(50) = NULL
,@term_address3 VARCHAR(50) = NULL
,@term_address4 VARCHAR(50) = NULL
,@term_address5 VARCHAR(50) = NULL
,@term_postcode VARCHAR(50) = NULL
,@term_country_code VARCHAR(50) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
DECLARE @person_id UNIQUEIDENTIFIER
SET NOCOUNT ON;
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
-- Create/Update person_test/person_reference_test
IF EXISTS ( SELECT person_id FROM dbo.person_reference WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
BEGIN
SELECT 'Student exists, updating'
SET @person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
UPDATE person
SET prefix_honorific = CASE WHEN @prefix_honorific = 'null' or @prefix_honorific is null or @prefix_honorific = '' THEN prefix_honorific ELSE @prefix_honorific END
,first_name = ISNULL(@first_name, first_name)
,middle_name = ISNULL(@middle_name, middle_name)
,surname = ISNULL(@surname, surname)
,familiar_name = @familiar_name
,date_of_birth = ISNULL(@date_of_birth, date_of_birth)
,external_email_address = ISNULL(@external_email_address, external_email_address)
,gender = ISNULL(@gender, gender)
,ethnicity = ISNULL(@ethnicity, ethnicity)
,domicile = ISNULL(@domicile, domicile)
,disability = ISNULL(@telephone_no, disability)
,telephone_no = ISNULL(@telephone_no, telephone_no)
,prev_surname = ISNULL(@prev_surname, prev_surname)
,country_of_birth = ISNULL(@country_of_birth, country_of_birth)
,proc_date_time = GETDATE()
,mobile_phone_no = ISNULL(@mobile_phone_no, mobile_phone_no)
,nationality = ISNULL(@nationality, nationality)
WHERE person_id_guid = @person_id
IF @account_id IS NOT NULL
BEGIN
UPDATE dbo.person_reference
SET account_id = @account_id
,proc_date_time = GETDATE()
WHERE person_id = @person_id
END
END
ELSE
BEGIN
SELECT 'Student does not exist, creating'
--INSERT person
SET @person_id = NEWID()
INSERT INTO dbo.person (
person_id_guid
,prefix_honorific
,first_name
,middle_name
,surname
,familiar_name
,date_of_birth
,external_email_address
,mobile_phone_no
,gender
,ethnicity
,domicile
,disability
,nationality
,telephone_no
,prev_surname
,country_of_birth
,source_system
,proc_date_time
)
VALUES (
@person_id
,@prefix_honorific
,@first_name
,@middle_name
,@surname
,@familiar_name
,@date_of_birth
,@external_email_address
,@mobile_phone_no
,@gender
,@ethnicity
,@domicile
,@disability
,@nationality
,@telephone_no
,@prev_surname
,'OneUni'
,@country_of_birth
,GETDATE()
)
--INSERT person_reference
INSERT INTO dbo.person_reference (
person_id
,student_id
,proc_date_time
,account_id
)
VALUES (
@person_id
,@student_id
,GETDATE()
,@account_id
)
END
-- Create/Update student
IF EXISTS ( SELECT account_id FROM dbo.student WITH (NOLOCK) WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
BEGIN
SELECT 'Student exists, updating'
UPDATE student
SET
--account_id = @account_id
--,student_id = @student_id
ucas_id = @UCAS_ID
,unique_learner_number = @uln
,main_programme_code = isnull(@main_programme_code, main_programme_code)
,student_email_address = @student_email_address
,currently_studying_flag = @currently_studying_flag
,hesa_student_id = @HesaStudentID
,visa_required = @VisaReq
,cas_status = @cas_status
,student_status = @student_status
,purpose_of_residency = @PurposeOfResidency
,mod_date_time = GETDATE()
,student_support_no = @student_support_no
,source_system = @source_system
,exam_id = @exam_id
,su_opt = @su_opt
,international_sponsored_students = @international_sponsored_students
,visa_type = @visa_type
WHERE account_id = @account_id
END
-- Create Student/Student Programme/Student Module
ELSE
BEGIN
SELECT 'Student does not exist, creating'
SET @person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
INSERT INTO student (
person_id_guid
,account_id
,ucas_id
,unique_learner_number
,student_email_address
,currently_studying_flag
,hesa_student_id
,visa_required
,cas_status
,student_status
,purpose_of_residency
,proc_date_time
,source_system
,main_programme_code
,student_id
,student_support_no
,exam_id
,su_opt
,international_sponsored_students
,visa_type
)
VALUES (
@person_id
,@account_id
,@UCAS_ID
,@uln
,@student_email_address
,@currently_studying_flag
,@HesaStudentID
,@VisaReq
,@cas_status
,@student_status
,@PurposeOfResidency
,getdate()
,@source_system
,@main_programme_code
,@student_id
,@student_support_no
,@exam_id
,@su_opt
,@international_sponsored_students
,@visa_type
)
END
-- Create/Update student_programmes if change_record is 'Course'
IF @change_type = 'Programme'
BEGIN
-- Create/Update student_programmes
IF EXISTS ( SELECT student_id FROM student_programmes WITH (NOLOCK) WHERE account_id = @account_id and programme_code = @programme_code)
BEGIN
SELECT 'Student Programme exists, updating'
--UPDATE student_programme? (Wait for confirmation)
UPDATE student_programmes
SET
--account_id = @account_id
--,student_id = @student_id
--course_code = @course_code
programme_description = @programme_description
,[start_date] = @start_date
,end_date = @end_date
,mdh_stage_code = @mdh_stage_code
,main_award_flag = @main_award_flag
,load_category = @load_category
,qualification_level = @qualification_level
,student_study_level = @student_study_level
,is_wbdl = @is_wbdl
,school_code = @school_code
,college_code = @college_code
,campus_code = @campus_code
,ul_qual_aim = @ul_qual_aim
,ul_qual_aim_description = @ul_qual_aim_desc
,mod_date_time = GETDATE()
WHERE account_id = @account_id
and programme_code = @programme_code
END
ELSE
BEGIN
SELECT 'Student Programme does not exist, creating'
SET @person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
--INSERT student_programme
INSERT INTO student_programmes (
person_id_guid
,account_id
,student_id
,programme_code
,programme_description
,[start_date]
,end_date
,mdh_stage_code
,main_award_flag
,load_category
,qualification_level
,student_study_level
,is_wbdl
,school_code
,college_code
,campus_code
,ul_qual_aim
,ul_qual_aim_description
,mod_date_time
)
VALUES (
@person_id
,@account_id
,@student_id
,@programme_code
,@programme_description
,@start_date
,@end_date
,@mdh_stage_code
,@main_award_flag
,@load_category
,@qualification_level
,@student_study_level
,@is_wbdl
,@school_code
,@college_code
,@campus_code
,@ul_qual_aim
,@ul_qual_aim_desc
,GETDATE()
)
END
END
-- Create/Update student_modules if change_record is 'Module'
IF @change_type = 'Module'
BEGIN
IF EXISTS ( SELECT student_id FROM student_modules WITH (NOLOCK) WHERE account_id = @account_id and programme_code = @programme_code and module_code = @module_code)
BEGIN
SELECT 'Student Module exists, updating'
--UPDATE student_module? (Wait for confirmation)
UPDATE student_modules
SET
--account_id = @account_id
--,student_id = @student_id
--course_code = @course_code
--module_code = @module_code
module_description = @module_desc
,mdh_stage_code = @mdh_stage_code
,student_study_level = @student_study_level
,mod_date_time = GETDATE()
WHERE account_id = @account_id
and programme_code = @programme_code
and module_code = @module_code
END
ELSE
BEGIN
SELECT 'Student Module does not exist, creating'
SET @person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = @account_id and ISNULL(status,'') <> 'Delete')
-- If the programme for the module/student doesnt exist, insert it
IF NOT EXISTS ( SELECT student_id FROM dbo.student_programmes WITH (NOLOCK) WHERE account_id = @account_id and programme_code = @programme_code)
BEGIN
SET @person_id = ( SELECT person_id FROM dbo.person_reference WITH (NOLOCK) WHERE account_id = @account_id )
--INSERT student_programme
INSERT INTO student_programmes (
person_id_guid
,account_id
,student_id
,programme_code
,programme_description
,[start_date]
,end_date
,mdh_stage_code
,main_award_flag
,load_category
,qualification_level
,student_study_level
,is_wbdl
,school_code
,college_code
,campus_code
,ul_qual_aim
,ul_qual_aim_description
,mod_date_time
)
VALUES (
@person_id
,@account_id
,@student_id
,@programme_code
,@programme_description
,@start_date
,@end_date
,@mdh_stage_code
,@main_award_flag
,@load_category
,@qualification_level
,@student_study_level
,@is_wbdl
,@school_code
,@college_code
,@campus_code
,@ul_qual_aim
,@ul_qual_aim_desc
,GETDATE()
)
END
--INSERT student_module
INSERT INTO student_modules (
person_id_guid
,account_id
,student_id
,programme_code
,module_code
,module_description
,mdh_stage_code
,student_study_level
,mod_date_time
)
VALUES (
@person_id
,@account_id
,@student_id
,@programme_code
,@module_code
,@module_desc
,@mdh_stage_code
,@student_study_level
,GETDATE()
)
END
END
END