我有一个包含数百万行的审计表。我将其编入索引并可以查看查询的性能,但现在我注意到插入并不总是很快。
在负载下,平均每10次插入需要500ms,虽然它有点随机,冗长的插入似乎是3个簇(第1列=毫秒):
Dur SQL
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
532 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
299 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
586 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
1 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
1 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
5 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
4 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
3 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
1 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
5 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
1 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
4 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
3 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
1 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
453 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
597 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
452 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
2 INSERT INTO modification (forecastId, userId, epochTime, description, auxText, auxDate) VALUES (?,?,?,?,?,?)
问题是什么?
这是Need to improve performance query a table with millions of rows - SQL Server
的续集(抱歉无法阻止自己)这是DDL:
CREATE TABLE [dbo].[modification] (
[id] [int] IDENTITY,
[userId] [int] NOT NULL,
[epochTime] [bigint] NOT NULL,
[forecastId] [int] NOT NULL,
[description] [char](12) NOT NULL,
[auxText] [text] NULL,
[auxDate] [date] NULL
);
ALTER TABLE [dbo].[modification] ADD CONSTRAINT PK_mod2 PRIMARY KEY(ID);
ALTER TABLE [dbo].[modification] WITH CHECK
ADD CONSTRAINT [FK_modification_forecastId] FOREIGN KEY([forecastId])
REFERENCES [dbo].[forecast] ([id]);
ALTER TABLE [dbo].[modification] WITH CHECK
ADD CONSTRAINT [FK_mod_userId] FOREIGN KEY([userId])
REFERENCES [dbo].[user] ([id]);
CREATE NONCLUSTERED INDEX IX_modification_last_save
ON [dbo].[modification] (userId, forecastId, description, epochTime);
我尝试使用查询的NO LOCK
子句减少锁定:
SELECT name, epochTime, auxDate
FROM modification WITH (NOLOCK)
JOIN [user] ON [user].id = modification.userId
WHERE forecastId = ? AND description = ?
ORDER BY epochTime DESC
更新
我把索引放在上面,并没有对时间产生任何影响。
我禁用了外键,瓶颈消失了。然后我解决了它只是因为forecastId外键约束。它的索引就像userId FK一样。
唯一的区别在于实际的父表,其中forecast
表在FILLFACTOR
没有的索引创建中有一个额外的子句user
:
ALTER TABLE [dbo].[forecast] ADD CONSTRAINT [PK_forecast]
PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON,
FILLFACTOR = 95
) ON [PRIMARY]
FILLFACTOR
可能是问题吗?