我有一张简单的表格:
CREATE TABLE DocModHistory
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[DocID] [int] NOT NULL,
[BranchID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[InsDate] [datetime] NOT NULL,
[Type] [int] NOT NULL,
CONSTRAINT [PK_DocModHistory] 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]
我有两个选择查询:
SELECT dh.BranchID, MAX(dh.ID) as MaxID FROM DocModHistory dh WHERE dh.UserID = @p_UserID GROUP BY dh.BranchID
和
SELECT dh.DocID, MAX(dh.ID) as MaxID FROM DocModHistory dh WHERE dh.UserID = @p_UserID GROUP BY dh.DocID
请问我应该创建哪些索引? 我应该为UserID,BranchID,DocID创建单独的索引,还是需要多列索引?
谢谢!
答案 0 :(得分:1)
为包含列UserID
,BranchID
和ID
DocID
创建索引
类似
CREATE INDEX IX_UserID ON DocModHistory (UserID) INCLUDE (BranchID, ID ,DocID);