I've added an index to a table to stop a table scan. Now it scans the index instead of the table, why doesn't it perform an index seek?
CREATE PROCEDURE [dbo].[GetPeople]
(
@PlaceIDs [dbo].[Integers] READONLY,
@RoleGroupIDs [dbo].[Integers] READONLY
)
AS
BEGIN
SET NOCOUNT ON;
SELECT
PTS.PersonToSiteID,
PTS.SiteID,
PTS.PersonID,
P.PersonID,
P.GivenName,
P.FamilyName,
P.Email,
P.LandlineNumber,
P.MobileNumber,
R.RoleTypeID
FROM
[dbo].[PeopleToPlaces] PTS
INNER JOIN (Select DISTINCT Identifier FROM @PlaceIDs) Pl ON PTS.PlaceID = Pl.Identifier
INNER JOIN [dbo].[People] P ON P.PeopleID = PTS.PeopleID
INNER JOIN [dbo].[Role] R ON R.RoleID = P.RoleID
INNER JOIN (Select DISTINCT Identifier FROM @RoleGroupIDs) RG ON R.RoleGroupID = RG.Identifier
END
I have a covering index on the People table and have added the Distinct subqueries whilst testing. There is an index covering the join onto the PTS table and the Identifier field sin the UDTs are both ints matching the type they are joining to. I've also tried a SELECT IN compared to a JOIN and can't find a way to avoid the index scan
Used this as a resource so far - https://www.red-gate.com/simple-talk/sql/performance/identifying-and-solving-index-scan-problems/