This query on an audit trail table starts slowing down once the load increases in performance testing (reads and inserts) and I've done what I can with indexing. With this query and these tables and indexes, what more can I do?
CREATE TABLE [dbo].[mod2] (
[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].[mod2] ADD CONSTRAINT PK_mod2 PRIMARY KEY(ID);
ALTER TABLE [dbo].[mod2] WITH CHECK
ADD CONSTRAINT [FK_mod2_forecastId] FOREIGN KEY([forecastId])
REFERENCES [dbo].[forecast] ([id]);
ALTER TABLE [dbo].[mod2] CHECK CONSTRAINT [FK_mod2_forecastId];
ALTER TABLE [dbo].[mod2] WITH CHECK
ADD CONSTRAINT [FK_mod2_userId] FOREIGN KEY([userId])
REFERENCES [dbo].[user] ([id]);
ALTER TABLE [dbo].[mod2] CHECK CONSTRAINT [FK_mod2_userId];
CREATE NONCLUSTERED INDEX IX_modification_auxDate ON [dbo].[mod2] (auxDate ASC);
CREATE NONCLUSTERED INDEX IX_modification_epochTime ON [dbo].[mod2] (epochTime ASC);
CREATE NONCLUSTERED INDEX IX_modification_description ON [dbo].[mod2] (description ASC);
CREATE NONCLUSTERED INDEX IX_modification_forecastId ON [dbo].[mod2] (forecastId ASC);
CREATE NONCLUSTERED INDEX IX_modification_userId ON [dbo].[mod2] (userId ASC);
and this is my query:
SELECT name, epochTime, auxDate
FROM mod2 WITH (NOLOCK)
JOIN [user] ON [user].id = mod2.userId
WHERE forecastId = ? AND description = ? AND auxDate = ?
This is a legacy system and it was crawling before I put these indexes on it and changed the description
field from VARCHAR
to CHAR
The forecast and user id
fields are INT
and indexed similarly.