gpt4 book ai didi

sql-server - 如何在 SQL Server 中使用 NOT IN 创建过滤索引

转载 作者:行者123 更新时间:2023-12-02 20:10:18 26 4
gpt4 key购买 nike

我想创建唯一的非聚集索引,它限制特定列的某些记录,我使用 NOT IN 来过滤它,但由于 NOT IN,我收到错误。有什么替代方法吗?

CREATE UNIQUE NONCLUSTERED INDEX 
[UN_Student] ON [dbo].[Student]
(
[School_Cd] ASC,
[Class_Id] ASC,
[Enroll_Dt] ASC,
[Email_Id] ASC
)
WHERE ([School_Cd]='EM' AND [Class_Id] NOT IN (1,2,3))
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)

最佳答案

过滤索引不能使用NOT IN。您可以改用多个 AND 运算符。 Brent Ozar 有一篇非常好的文章 here关于过滤索引允许和不允许的内容。

CREATE UNIQUE NONCLUSTERED INDEX 
[UN_Student] ON [dbo].[Student]
(
[School_Cd] ASC,
[Class_Id] ASC,
[Enroll_Dt] ASC,
[Email_Id] ASC
)
WHERE ([School_Cd]='EM' AND [Class_Id] <> 1 AND [Class_Id] <> 2 AND [Class_Id] <> 3)
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)

关于sql-server - 如何在 SQL Server 中使用 NOT IN 创建过滤索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53749057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com