The following query is taking 5 seconds to return 9692 rows:
SELECT
Project.CalculateSequencialProject,
Project.CreateDate,
Project.OpeningDate,
Project.ForecastClosingDate,
Project.RealClosingDate,
Project.ProjectStatus,
Unit.Name AS UnitName,
UTE.Name AS UTEName,
Process.Name AS Process
FROM
Project
LEFT JOIN Unit ON Project.Unit_Id = Unit.Id AND Project.Company_Id = Unit.Company_Id
LEFT JOIN UTE ON Project.UTE_Id = UTE.Id
LEFT JOIN Process ON Project.Process_Id = Process.Id
WHERE
Project.Company_Id = '????????????????'
The execution plan is like this:
And the most "expensive" part of it is detailed here:
As you may se in the tooltip image, the Clustered Index Scan with 75% of cost is using the table primary key instead of one of the following indexes.
CREATE NONCLUSTERED INDEX [IX_Company_Id] ON [dbo].[Project]
(
[Company_Id] ASC
)
CREATE NONCLUSTERED INDEX [IX_Project_02] ON [dbo].[Project]
(
[ProjectType_Id] ASC,
[ProjectStatus] ASC,
[Unit_Id] ASC,
[Id] ASC,
[ProjectDirector_Id] ASC,
[UTE_Id] ASC,
[Company_Id] ASC,
[LostType_Id] ASC,
[Pillar_Id] ASC,
[ProjectPhase_Id] ASC
)
INCLUDE ( [CalculateSequencialProject],
[DescriptionTittle],
[OpeningDate],
[ForecastClosingDate],
[RealClosingDate])
Why?
[Id]
column.