This question relates to a table in Microsoft SQL Server which is usually queried with ORDER BY Id DESC.
Would there be a performance benefit from setting the primary key to PRIMARY KEY CLUSTERED (Id DESC)? Or would there be a need for an index? Or is it as fast as it gets without any of it?
Table:
CREATE TABLE [dbo].[Items] ( [Id] INT IDENTITY (1, 1) NOT NULL, [Category] INT NOT NULL, [Name] NVARCHAR(255) NULL, CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ([Id] ASC) ) Query:
SELECT TOP 1 * FROM [dbo].[Items] WHERE Catgory = 123 ORDER BY [Id] DESC 
ORDER BYin a production query? There aren't as many reasons to useORDER BYas you may think! (And the ordering of an index or PK has very little effect onORDER BYbecauseORDER BYis evaluated after the data has already been read into the server's data buffer: the ordering generally happens all in-memory).CREATE TABLEdefinition (including all associated indexes) and actual execution-plan. Anything else is conjecture and speculation.