本文介绍了怎么会发生SP sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一些奇怪的东西。我运行这个sql:

  SELECT Id,GameTypeId,PlayerId,BetAmount,Profit,
DateAndTime
FROM结果
WHERE DateAndTime> = DATEADD(DAY,-1,SYSDATETIME())
AND
DateAndTime< SYSDATETIME()
ORDER BY DateAndTime ASC;

我在日期列

上有非集群索引和实际行数该返回值为表中的1600016行中的

672行。
(估计行为1)



之后,我运行此sql:

  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结果
WHERE DateAndTime> = @d
AND
DateAndTime< @ d2
ORDER BY DateAndTime ASC;

,实际的执行计划是TABLE SCANE!
,返回的实际行数为

表中的1600016行中的672行。
(估计的行是144000 r0ws)



有些1知道这里发生了什么? h2_lin>解决方案

由于您使用变量作为值,查询优化器不知道WHERE子句的选择性,并决定使用表扫描。
尝试在您的DateAndTime字段上创建聚簇索引。


I get something weird. i ran this sql:

SELECT   Id , GameTypeId , PlayerId , BetAmount , Profit ,
         DateAndTime
FROM     Results
WHERE    DateAndTime >= DATEADD (DAY , -1 , SYSDATETIME ())
         AND
         DateAndTime < SYSDATETIME ()
ORDER BY DateAndTime ASC;

i have noncluster index on the date column
and the actual number of rows that return is
672 row from 1600016 rows in the table.(the estimated row was 1)

after that i ran this sql:

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;

and the actual execution plan was TABLE SCANE !!!and the actual number of rows that return is
672 row from 1600016 rows in the table.(the estimated row was 144000 r0ws)

some 1 know what happend here ?!?!?

解决方案

Because you're using variables for your values, the query optimizer doesn't know how selective your WHERE clause is, and decides to use a table scan.Try creating a clustered index on your DateAndTime field.

这篇关于怎么会发生SP sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:35