I have a table with the following schema:
AccountId: int
StartDateTime: datetime2(0)
EndDateTime: datetime2(0)
I want to be able to query this for rows with a certain AccountId, and whose date range intersects with a specified date range. E.g:
SELECT *
FROM ... AS d
WHERE d.AccountId = @AccountId
AND d.StartDateTime <= @EndDateTime
AND d.EndDateTime >= @StartDateTime
This will be a very large table and I'll want to be able to query it as efficiently as possible. I've specified a single composite index of AccountId, StartDateTime, and EndDateTime.
Will this index, and the above query work effectively together? If not, what would be a better alternative? Thanks.