我不明白为什么我不能在我的视图中使用主键作为索引。
这是主表
CREATE TABLE [dbo].[xFedDBLogMsg](
[ID] [int] IDENTITY(1,1) NOT NULL,
[msgType] [int] NOT NULL,
[date] [datetime] NOT NULL,
[delay] [time](7) NOT NULL,
[error] [bit] NOT NULL,
[processID] [int] NULL,
CONSTRAINT [PK_tFedDBLogMsg] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
这是视图
CREATE VIEW [dbo].[tFedDBLogMsg]
AS
SELECT
L.ID
, L.msgType
, L.[date]
, M.MsgSent
, M.MsgReceived
, L.[delay]
, L.error
, L.processID
, NEWID() AS INTERNALID
FROM dbo.xFedDBLogMsg AS L
LEFT JOIN FedDBMsg.dbo.tFedDBLogMsg AS M ON (
M.ID = L.ID
)
这里的程序给了我一个警告:
ALTER PROCEDURE spGetFedDBErrorsByID (
@pIDS AS dbo.typeNumberList READONLY
)
AS
BEGIN
SELECT
MSG.ID
, MSG.msgType
, MSG.date
, MSG.MsgSent
, MSG.MsgReceived
FROM (
SELECT
CAST(ID.n AS INT) AS ID
FROM @pIDS AS ID
) AS X
INNER JOIN MyGolf.dbo.tFedDBLogMsg AS MSG WITH (INDEX(PK_tFedDBLogMsg)) ON (
MSG.ID = X.ID
)
END
GO
Warning: Index hints supplied for view 'MyGolf.dbo.tFedDBLogMsg' will be ignored.
答案 0 :(得分:3)
在部分
WITH (INDEX(PK_tFedDBLogMsg))
除了约束的名称之外,是PK_tFedDBLogMsg
,视图上的聚集索引也是?
如果是这样,请务必同时使用NOEXPAND
选项。
答案 1 :(得分:0)
“查询优化器自动使用索引视图”仅在Enterprise(和Developer)版本中可用。这意味着在企业版中,您还可以通过创建索引视图来优化查询:即使您未在查询文本中指定它,sql server也可以使用它
“索引视图的直接查询(使用NOEXPAND提示)”可用于所有版本
https://docs.microsoft.com/en-US/sql/sql-server/editions-and-components-of-sql-server-2016