我正在尝试添加此索引:
CREATE NONCLUSTERED INDEX [HE_MissingIndex]
ON [dbo].[HardwareEvents] ([PlaceId] ASC, [EventId] ASC, [UserTokenType])
INCLUDE ([Id], [EventTime], [UserToken], [Username], [UserId],
[VisitorId], [UserLastname])
WHERE (EventId = 16 AND UserTokenType = 5) OR EventId = 58
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = ON, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
不幸的是,我收到此错误消息:
第156条消息,第15级,状态1,第16行
关键字“或”附近的语法不正确。
对我来说有点奇怪。我已经查阅了相关的MSDN页面,我没有看到适合这里的任何约束。过滤器中的两列都存在于索引中。创建的索引在表达式中没有OR。我在这里想念什么?
答案 0 :(得分:1)
过滤索引仅允许AND
和IN
谓词,因此您的问题出在WHERE
子句中,不能使用OR
。您可以通过创建两个索引来解决此问题:
CREATE NONCLUSTERED INDEX [HE_MissingIndex] ON [dbo].[HardwareEvents]
(
[PlaceId] ASC,
[EventId] ASC,
[UserTokenType] ASC
)
INCLUDE ( [Id],
[EventTime],
[UserToken],
[Username],
[UserId],
[VisitorId],
[UserLastname])
WHERE [EventId] = 16 and [UserTokenType] = 5
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = ON, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [HE_MissingIndex_2] ON [dbo].[HardwareEvents]
(
[PlaceId] ASC,
[EventId] ASC,
[UserTokenType] ASC
)
INCLUDE ( [Id],
[EventTime],
[UserToken],
[Username],
[UserId],
[VisitorId],
[UserLastname])
WHERE [EventId] = 58
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = ON, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]