We are experiencing that the first execution of a query against an index is very slow. It's like there is a cold start of the index. The table is big with millions of rows.
- execution: 30+sec
- execution: 4ms
Database script:
CREATE TABLE [Audits] (
[Id] int NOT NULL IDENTITY,
[Timestamp] datetime2 NOT NULL,
[PackageUid] nvarchar(450) NOT NULL,
[DeviceUid] nvarchar(450) NOT NULL,
CONSTRAINT [PK_Audits] PRIMARY KEY ([Id])
);
CREATE INDEX [IX_Audits_DeviceUid] ON [Audits] ([DeviceUid]);
CREATE INDEX [IX_Audits_PackageUid] ON [Audits] ([PackageUid]);
CREATE INDEX [IX_Audits_Timestamp] ON [Audits] ([Timestamp]);
Query:
SELECT COUNT(*)
FROM [Audits] AS [f]
WHERE [f].[DeviceUid] = "04B6481955104083"
ORDER BY [f].[Timestamp] DESC
SELECT *
FROM [Audits] AS [f]
WHERE [f].[DeviceUid] = "04B6481955104083"
ORDER BY [f].[Timestamp] DESC
OFFSET 1 ROWS FETCH NEXT 10 ONLY
Possible causes:
nvarcharis to big for the index to work quickly enough. I could change it down to 50.- Missing index on DeviceUid and Timestamp