how that happen SP sql server

前端 未结 5 766
长情又很酷
长情又很酷 2021-01-28 04:29

I get something weird. i ran this sql:

SELECT   Id , GameTypeId , PlayerId , BetAmount , Profit ,
         DateAndTime
FROM     Results
WHERE    DateAndTime >         


        
5条回答
  •  日久生厌
    2021-01-28 04:48

    Try

    declare @d DATETIME2(7) 
    set @d = DATEADD (DAY , -1 , SYSDATETIME ())
    declare  @d2 DATETIME2(7)
    set @d2  = SYSDATETIME ()
    
    SELECT   Id , GameTypeId , PlayerId , BetAmount , Profit ,
             DateAndTime
    FROM     Results
    WHERE    DateAndTime >= @d
             AND
             DateAndTime < @d2
    ORDER BY DateAndTime ASC
    OPTION (RECOMPILE);
    

    This will recompile the plan for the statement once the value of the variables are known and so allow accurate cardinality estimates to be used. If you know you will always be selecting a very small percentage you could just use the FORCESEEK (if SQL Server 2008) hint instead to avoid the recompiles but using this hint may be catastrophically bad for larger ranges because of the number of key lookups!

提交回复
热议问题