So there is a View that I need that involves joining 4 tables. The execution time is (CPU Time = 109ms , elapsed time = 148 ms). I looked at the execution plan and the biggest costs involves Hash Match and even a Index Spool. So I'm no expert at SQL (but I'm learning) and have only started reading about indexes a few days ago. For the tables involved in this query, I put clustered indexes and non-clustered indexes on fields that will be commonly used for joins. I tried putting some other clustered indexes last night and was only able to bring the elapsed time down to 125 ms. To be honest, I have no idea how to improve the performance. Here are DDLs. I can't make major changes to these tables right now but I can adjust the indexes. I assume that the [Group_Name] is causing the query to be so slow but adding non-clustered indexes to that column for the tables doesn't significantly improve performance.Tables CREATE TABLE tbl_Clauses ( ClauseID smallint identity (1,1), Standard varchar(100), Clause varchar(100), Description varchar(100), IsAS9100 bit not null, IsActive bit not null, Primary key clustered (ClauseID), Unique (Standard, Clause) ) CREATE TABLE jntbl_RefDocToGroup ( Group_Name varchar(100)not null, RefDoc_Title varchar (100)not null, Primary Key Clustered (Group_Name, RefDoc_Title) ) CREATE TABLE tbl_GroupClauses ( ID smallint primary key identity (1,1), Group_Name varchar (100)not null, Standard varchar (100) not null, Clause varchar (100)not null, IsPrimary bit not null, Unique (Group_Name, Clause,Standard) ) CREATE TABLE tbl_Groups ( Group_ID smallint identity, Dept_Name varchar(100), Group_Name varchar(100), ManagerEmpID smallint, Key_Processes varchar(500),tb ManagerEmail varchar (100), Primary Key clustered (Group_ID), Unique(Group_Name) )
The Slowest Query EverCREATE VIEW v_GroupInfoASSELECT Dept_Name,v_ConcatGroupClauses.Group_Name,tbl_Groups.Group_ID,Supervisor as Supervisors,ManagerName, PrimClauses as PrimaryClauses, SecClause as SecondaryClauses, RefDoc as RefDocs,Key_Processes, v_ManagerInfo.ManagerEmail, Email as Supervisor_Email from dbo.v_ConcatGroupClauses inner join dbo.v_ConcatGroupRefDocs on v_ConcatGroupClauses.Group_Name = v_ConcatGroupRefDocs.Group_Name inner join dbo.v_ConcatGroupSecClauses on v_ConcatGroupClauses.Group_Name = v_ConcatGroupSecClauses.Group_Name inner join dbo.v_ConcatGroupSupervisors on v_ConcatGroupClauses.Group_Name = v_ConcatGroupSupervisors.Group_Name inner join dbo.v_ManagerInfo on v_ConcatGroupClauses.Group_Name = v_ManagerInfo.Group_Name inner join tbl_Groups on v_ConcatGroupClauses.Group_Name = tbl_Groups.Group_NameGO
VIEWSCREATE VIEW v_ConcatGroupRefDocsASSELECT DISTINCT Group_Name, (SELECT SUBSTRING((SELECT ', ' + RefDoc_TitleFROM jntbl_RefDocToGroupWHERE Group_Name = t.Group_NameFOR XML PATH('')),2,200000)) AS RefDocFROM jntbl_RefDocToGroup tGOCREATE VIEW v_ConcatGroupClausesASSELECT DISTINCT Group_Name, (SELECT SUBSTRING((SELECT ', ' + ClauseFROM tbl_GroupClausesWHERE Group_Name = t.Group_NameAND IsPrimary = '1'FOR XML PATH('')),2,200000)) AS PrimClausesFROM tbl_GroupClauses tGOCREATE VIEW v_ConcatGroupSecClausesASSELECT DISTINCT Group_Name, (SELECT SUBSTRING((SELECT ', ' + ClauseFROM tbl_GroupClausesWHERE Group_Name = t.Group_NameAND IsPrimary = '0'FOR XML PATH('')),2,200000)) AS SecClauseFROM tbl_GroupClauses tGOCREATE VIEW v_GroupSupervisorasselect group_name, Full_name as 'Supervisor',Emailfrom tbl_groups as g inner join (jntbl_GroupSupervisor inner join tbl_employee on jntbl_GroupSupervisor.emp_id = tbl_employee.emp_id)on g.Group_ID = jntbl_GroupSupervisor.Group_IDGOCREATE VIEW v_ConcatGroupSupervisorsASSELECT DISTINCT Group_Name, (SELECT SUBSTRING((SELECT ', ' + SupervisorFROM v_GroupSupervisorWHERE Group_Name = t.Group_NameFOR XML PATH('')),2,200000)) AS Supervisor,(SELECT SUBSTRING((SELECT '; ' + EmailFROM v_GroupSupervisorWHERE Group_Name = t.Group_NameFOR XML PATH('')),2,200000)) AS EmailFROM v_GroupSupervisor tGOAny feedback on how I should approach this problem? I've tried using an index view on v_GroupInfo but SQL Server wouldn't allow it.