My query is dead simple but sometimes it gets a timeout exception.
SELECT top 100 * FROM [cmn].[ProductChange] WHERE shop_id = @shopId ORDER BY changed_at DESC
This is the execution plan:
If i test the query on SSMS the first time it is really slow, over 30 sec, but all the next times it’s much faster.
This is the primary key of the table
ALTER TABLE [cmn].[ProductChange] ADD CONSTRAINT [PK_cmn_ProductChange] PRIMARY KEY CLUSTERED
(
[product_id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
I have about 350k rows which I don’t think that are that much. Are they?
I have only one FK with the Shop, the PK is an external ID and it’s not connected to any other table.
How can I make this query much faster?