Home > database >  For help under the great god help optimize SQL statements, often hint timeout
For help under the great god help optimize SQL statements, often hint timeout

Time:11-09



SELECT * FROM (SELECT [UserId], [UserBalance], [TotalAmount], [MissionAmount], [ToCardAmount], [UserGoldCoinBalance], [TotalGoldCoinAmount], [CreditScore], [CreatedAt], [CreatedUserId], [UpdatedAt], [UpdatedUserId], ROW_NUMBER () OVER (ORDER BY GETDATE ()) AS RowIndex FROM [UserAccount] WITH (NOLOCK) WHERE ([UserId]=@ UserId0)) T WHERE RowIndex BETWEEN 1 AND 1

CodePudding user response:

Take a random record?
UserId should have index
 SELECT TOP (1) (UserId), 
[UserBalance],
[TotalAmount],
[MissionAmount],
[ToCardAmount],
[UserGoldCoinBalance],
[TotalGoldCoinAmount],
[CreditScore],
[CreatedAt],
[CreatedUserId],
[UpdatedAt],
[UpdatedUserId],
NEWID () AS RowIndex
The FROM (UserAccount) WITH (NOLOCK)
The WHERE ([UserId]=@ UserId0)
The ORDER BY RowIndex;

CodePudding user response:

Thank you very much for the prompt reply, the moderator has now processing, special thanks!

CodePudding user response:

Why do you want to ORDER BY GETDATE (), not directly ORDER BY userId

CodePudding user response:

 -- UserId is a primary key, could only take a record out, don't need so many restrictions, as simple as possible, 
SELECT [UserId],
[UserBalance],
[TotalAmount],
[MissionAmount],
[ToCardAmount],
[UserGoldCoinBalance],
[TotalGoldCoinAmount],
[CreditScore],
[CreatedAt],
[CreatedUserId],
[UpdatedAt],
[UpdatedUserId]
The FROM (UserAccount) WITH (NOLOCK)
WHERE [UserId]=@ UserId