1
查詢我在SQL Server下表:表的正確的索引給上市
CREATE TABLE [dbo].[Users](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Email] [varchar](128) NOT NULL,
[CreatedAt] [datetime] NOT NULL,
[SourceId] [int] NOT NULL
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 * FROM Users WHERE Email = '[email protected]'
SELECT * FROM Users WHERE Email = '[email protected]' AND SourceId = some integer
SELECT * FROM Users WHERE CreatedAt BETWEEN '2011-10-01' AND '2011-10-30'
SELECT * FROM Users WHERE CreatedAt BETWEEN '2011-10-01' AND '2011-10-30' AND SourceId = some integer
目前,我已經安裝了以下指標:
CREATE INDEX IX_Users_Email_SourceId ON Users (Email, SourceId)
CREATE INDEX IX_Users_CreatedAt ON Users (CreatedAt)
CREATE INDEX IX_Users_SourceId ON Users (SourceId)
難道這些指標足以滿足上面列出的查詢的類型?如果這將是一個嚴重查詢的表,我應該將ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON設置爲OFF嗎?我對如何正確設置索引有些困惑。
感謝KM。我應該關閉行和頁鎖嗎?或者這是一個壞主意? SourceId不會很有選擇性。會有很多INSERTS(但沒有那麼多UPDATES)。 – Thomas
您擔心基於INSERT/UPDATE/DELETE頻率的鎖定以及它們事務的長度。如果你主要是經常選擇這個數據,而且交易時間不長,那麼在你開始出現問題之前,我不會擔心這些數據。 –