Performance Issue in TSQL Query

Hi all,

I have a TSQL code which takes 1 min 20 sec.  Kindly let me know the possibilities to increase the performance at least query runs in 20 to 30 sec. please find the below query:

DECLARE @Temp table ( idCol int identity(1,1) , MovementDate date, MovementTypeDesc varchar(60), CDCNumber varchar(12), MovementTime time(7), LocationReportMovement varchar(9), OtherLocationCode varchar(9), OtherInstitutionName varchar(50), InstReportMovement varchar(9), InstNameReportMovement varchar(100), OtherInstitutionCode varchar(9), movementTypeCD int, changeType varchar(30), pickupOrder int ) /* --Get All the transfer in and out patients except bed moves (where from and to locations are same) */ INSERT INTO @Temp ( MovementDate , MovementTypeDesc , CDCNumber , MovementTime , LocationReportMovement , OtherLocationCode , OtherInstitutionName , InstReportMovement , InstNameReportMovement , OtherInstitutionCode , movementTypeCD , changeType , pickupOrder ) SELECT MovementDate, MovementTypeDesc, mov.CDCNumber, MovementTime, LocationReportMovement, OtherLocationCode, OtherInstitutionName, InstReportMovement, InstNameReportMovement, OtherInstitutionCode, movementTypeCD, CASE WHEN movementTypeCD = 30 THEN 'Transfer In' WHEN movementTypeCD = 90 THEN 'Transfer Out' END AS ChangeType, RANK() OVER (PARTITION BY mov.CDCNumber ORDER BY movementDate DESC) AS rankNo FROM [HCODS].dbo.vSTSExternalMovement mov WHERE InstReportMovement = '100000000' AND LTRIM(RTRIM(InstNameReportMovement)) <> LTRIM(RTRIM(OtherInstitutionName)) AND MovementDate BETWEEN '12/01/2013' AND '06/01/2014' AND movementTypeCD IN ('30', '90') --- 30 for Transfer In and 90 for Transfer out ORDER BY ChangeType; /* ----Get REST (ignore patients who were already listed in transfer in or out) of the patients with bed moves (where from and to locations are same, ----2 records one for out and one for in will be created in each case so just getting transfer in i.e movementTypeCd 30 to avoid duplicate records ) */ INSERT INTO @Temp(MovementDate, MovementTypeDesc, CDCNumber, MovementTime,LocationReportMovement, OtherLocationCode, OtherInstitutionName, InstReportMovement,InstNameReportMovement,OtherInstitutionCode,movementTypeCD, changeType, pickupOrder) SELECT MovementDate, MovementTypeDesc, mov.CDCNumber, MovementTime, LocationReportMovement, OtherLocationCode, OtherInstitutionName, InstReportMovement, InstNameReportMovement, OtherInstitutionCode, movementTypeCD, 'Bed Move' AS ChangeType, RANK() OVER (PARTITION BY mov.CDCNumber ORDER BY movementDate DESC) AS rankNo FROM [HCODS].dbo.vSTSExternalMovement mov WHERE InstReportMovement = '100000000' AND LTRIM(RTRIM(InstNameReportMovement)) = LTRIM(RTRIM(OtherInstitutionName)) AND MovementDate BETWEEN '12/01/2013' AND '06/01/2014' AND movementTypeCD ='30' AND mov.CDCNUmber Not IN (SELECT CDCNumber FROM @Temp); ------* Actual Query *---- SELECT CONVERT(VARCHAR(10), movementDate, 101) AS movementDate, h.CDCNumber, prof.INMATELASTNAME AS PatientLast , prof.INMATEFIRSTNAME AS PatientFirst , prof.[INMATEHOUSINGAREAID] + prof.[INMATEBEDNUMBER] AS Housing, h.OtherLocationCode, h.InstNameReportMovement, h.OtherInstitutionName, h.OtherInstitutionCode, fac.LocationCode AS OtherFacilityCode,

appt.AppointmentID, appt.ServiceArea, appt.appointmentLength, appt.ProblemSymptom, appt.SourceOfVisit, CONVERT(VARCHAR(10), appt.AppointmentDate, 101) AS AppointmentDate, CONVERT(VARCHAR(5), appt.AppointmentTime, 108) AS AppointmentTime, appt.AppointmentStatus, CONVERT(VARCHAR(10), appt.Compliance, 101) AS Compliance, appt.Location, appt.AppointmentStatusID, appt.LocationID, changeType FROM @Temp as h LEFT join v_AppointmentsLocal appt ON h.CDCNumber = appt.CDCnumber INNER JOIN [HCODS].[dbo].[vInmateProfile] AS prof ON prof.[ADCNUMBER] = appt.CDCnumber JOIN v_Facility fac ON h.OtherInstitutionCode = fac.SOMSInstitutionCode WHERE appt.FacilityID = '2' AND ServiceAreaID ='2' ----AND ServiceAreaID IN (@ServiceArea) AND (AppointmentStatusID IN (1, 2)) AND h.pickupOrder = 1 ORDER BY h.changeType DESC , OtherInstitutionName, appt.LocationCode, appt.appointmentStatus DESC


Thanks.


  • Edited by BI-Help 9 hours 21 minutes ago
August 28th, 2015 5:48pm

There are several bugs in that query already.

I don't think you need to do 2 separate selects into @temp table. I suggest to not use @temp but rather a single select statement into cte and use CASE statement to correctly identify the change type.

Also there is no need to use LEFT JOIN as it will be converted into INNER JOIN anyway. Add aliases for every column used in a query as well.

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 6:05pm

Good day BI-Help,

Performance tuning should be done with your real DDL, and not with table variable (unless you have table variable in your real process)!

A query that is fit best for one database might be the worse in another database. The Performance tuning must be done according to your specific database structure and data, which include for example indexes relations and other elements, else this is a wast of time usually.

If you need help in Performance tuning, then please post your DDL+DML :-)

August 28th, 2015 6:38pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics