I have two tables
Account: AccountUID bigint (Primary Key) AccountID bigint Version smallint CustomerName varchar(50) Note: AccountID and CurrentVersion are also part of a key that is unique
AccountStatus: AccountID bigint (Primary Key) CurrentVersion smallint Filter1 varchar(10) Filter2 varchar(10)) To keep things simple, I currently have 100 rows in each as each AccountID only has one version. I have values in the Filter1 and Filter2 tables such that when I do the following I get 10 records returned:
SELECT * FROM AccountStatus acs WHERE acs.Filter1=SomeValue1 and acs.Filter2=SomeValue2 Due to the fact that I have an index that has the Filter1 and Filter2 in it, the actual execution plan shows an Index Seek with only the 10 selected rows in the Actual Rows value.
When I join in the Account table as follows, I get the same 10 records:
SELECT acs.* FROM AccountStatus acs INNER JOIN Account a ON acs.AccountID=a.AccountID AND acs.CurrentVersion=a.Version WHERE acs.Filter1=SomeValue1 and acs.Filter2=SomeValue2 However, when I look at the Actual Execution Plan, I still see the Index Seek on the AccountStatus table as before with 10 Actual Rows. However, above that I see an Index Scan on the index that involves the AccountID and Version of the Account table. Also, this "action" shows 100 in the Actual Rows.
Here is the detail of the indexes involved:
CREATE NONCLUSTERED INDEX [IX_Find] ON [dbo].[AccountStatus] ( [Filter1] ASC, [Filter2] ASC ) INCLUDE ( [AccountID], [CurrentVersion] ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] CREATE UNIQUE NONCLUSTERED INDEX [IX_Account_Status] ON [dbo].[Account] ( [AccountID] ASC, [Version] ASC ) INCLUDE ( [AccountUID]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] While this is not too much of a performance hit with 100 rows, I am more concerned when I get to a million or several million rows. This will become very inefficient.
Is there anyway to make this type of query not scan all of the rows in the Index on the Account table?
Any help will be most appreciated.
AccountID and CurrentVersion are also part of a key that is unique. What part they are of that index matters quite a lot.