My MSSQL Server was upgraded over the last few days to 2019 version.
I don’t run this query often, so I don’t know whether it was faster before the migration.
The following query takes 45+ seconds to run, directly in SSMS, and in a web environment lands up giving a SQL timeout error.
The tables all join onto table tblGivingContacts
using their respective primary key (eg, tblStudents.studentID, tblGivingCampaigns.campaignID, tblGivingCallStatus.callStatusID, tblGivingDonationStatus.donationStatusID).
My Query:
SELECT
DISTINCT tblGivingContacts.*, firstname, lastname, fathersname, mothersname, email, fathersemail, mothersemail, DonationStatusName, callStatusName, CampaignName, phone1, phone2, cellphone,
FathersCellphone, MothersCellphone, tblStudents.Country As StudentCountry, tblAlumni.Country As AlumniCountry, MothersCountry, FathersCountry
,(SELECT TOP 1 SchoolYear FROM tblStudentStudyYears WHERE tblStudentStudyYears.studentID = tblStudents.studentID ORDER BY schoolYear DESC) AS SchoolYear
FROM [tblGivingContacts]
INNER JOIN tblStudents ON tblGivingContacts.contactID = tblStudents.studentID
INNER JOIN tblParents ON tblParents.studentID = tblStudents.studentID
INNER JOIN tblGivingCampaigns ON tblGivingContacts.campaignID = tblGivingCampaigns.campaignID
LEFT OUTER JOIN tblAlumni ON tblAlumni.studentID = tblStudents.studentID
LEFT OUTER JOIN tblGivingCallStatus ON tblGivingContacts.callStatusID = tblGivingCallStatus.callStatusID
LEFT OUTER JOIN tblGivingDonationStatus ON tblGivingContacts.donationStatusID = tblGivingDonationStatus.donationStatusID
I have run the Execution Plan, the main things that come out are:
- tblGivingContacts.* cost = 38%
- the tblParents columns cost = 27%
and this is what the execution plans suggested:
Missing Index Details from SQLQuery1.sql
The Query Processor estimates that implementing the following index could improve the query cost by 28.7418%.
*/
/*
USE [database]
GO
CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysname,>]
ON [dbo].[tblParents] ([studentID])
INCLUDE ([FathersName],[FathersCountry],[FathersEmail],[FathersCellphone],[MothersName],[MothersCountry],[MothersEmail],[MothersCellphone])
Is this something that’s suggested? Is there any detriment to doing this? What if i land up changing the query/adding columns in the Select
?
Thanks.