I need to do tuning on this sp:
alter PROCEDURE Gaming.usp_DB_GetGameResultsByDateTime
(@FromDateTime AS DATETIME2(7) = null ,
@ToDateTime AS DATETIME2(7) = null)
AS
DECLARE @FromDateTime_Actual AS DATETIME2(7) ,
@ToDateTime_Actual AS DATETIME2(7);
SET @FromDateTime_Actual = ISNULL(@FromDateTime, DATEADD (DAY, -1, SYSDATETIME()));
SET @ToDateTime_Actual = ISNULL (@ToDateTime , SYSDATETIME ());
SELECT Id, GameTypeId, PlayerId,
BetAmoun开发者_StackOverflowt, Profit,
DateAndTime
FROM Gaming.GameResults
WHERE DateAndTime >= @FromDateTime_Actual
AND DateAndTime < @ToDateTime_Actual
ORDER BY DateAndTime ASC;
GO
What is wrong with this stored procedure? How would you rewrite this stored procedure?
There is nothing wrong with this stored procedure.
You could make it a little bit more readable by getting rid of the variables:
ALTER PROCEDURE
Gaming.usp_DB_GetGameResultsByDateTime
(
@FromDateTime AS DATETIME2(7) = NULL ,
@ToDateTime AS DATETIME2(7) = NULL
)
AS
DECLARE
SELECT Id, GameTypeId, PlayerId, BetAmount, Profit, DateAndTime
FROM Gaming.GameResults
WHERE DateAndTime >= ISNULL (@FromDateTime , DATEADD (DAY , -1 , SYSDATETIME ()))
AND DateAndTime < ISNULL (@ToDateTime , SYSDATETIME ())
ORDER BY
DateAndTime ASC
GO
and more efficient by creating an index on GameResults (DateAndTime)
Based on what you provided, the proc is fairly simple. If you have performance issues, make sure you have an index on DateAndTime
since all your filtering is on that field.
Looks goods to me except I might do
WHERE DateAndTime between @FromDateTime_Actual AND @ToDateTime_Actual
but I wouldn't expect it to affect performance
If you're looking to make performance faster you should probably consider looking at your indexes for example DateAndTime might be a good place to start.
After that you'll need to look at Perforamnce Tips articles like this one
As Quassnoi noted, BETWEEN
is inclusive which changes < @FromDateTime_Actual to <= @FromDateTime_Actual. If you really want it to be not inclusive and you want to you BETWEEN
you can modify the how you set @ToDateTime_Actual to make this happen.
精彩评论