我有两张桌子
Account:
AccountUID bigint (Primary Key)
AccountID bigint
Version smallint
CustomerName varchar(50)
注意:AccountID 和 CurrentVersion 也是唯一键的一部分
AccountStatus:
AccountID bigint (Primary Key)
CurrentVersion smallint
Filter1 varchar(10)
Filter2 varchar(10))
为简单起见,我目前每行有 100 行,因为每行AccountID
只有一个版本。我在Filter1
和Filter2
表中有值,因此当我执行以下操作时,我会返回 10 条记录:
SELECT *
FROM AccountStatus acs
WHERE acs.Filter1=SomeValue1 and acs.Filter2=SomeValue2
由于我有一个包含Filter1
and的索引,Filter2
因此实际的执行计划显示了一个 Index Seek,其中只有 Actual Rows 值中的 10 个选定行。
Account
当我按如下方式加入表时,我得到相同的 10 条记录:
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
但是,当我查看 Actual Execution Plan 时,我仍然可以看到AccountStatus
桌面上的 Index Seek 和以前一样,有 10 个 Actual Rows。但是,在此之上,我看到索引扫描涉及表的AccountID
和。此外,此“动作”在实际行中显示 100。Version
Account
以下是所涉及索引的详细信息:
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]
虽然这对 100 行的性能影响不大,但当我达到一百万或几百万行时,我更担心。这将变得非常低效。
有没有办法让这种类型的查询不扫描Account
表上索引中的所有行?
任何帮助将不胜感激。