SQL优化之一

来源:互联网 发布:优化企业发展环境 编辑:程序博客网 时间:2024/06/07 19:58

在SQLServer2008 Management Studio中执行下列代码:

 

SET STATISTICS TIME ON
GO
select *
from information.lhbinfo
where tradingdate between '2010-01-01' and '2011-06-10';
GO
declare @StartDate datetime,@ExpirationDate datetime
SELECT @StartDate='2010-01-01',@ExpirationDate='2011-06-10';
select *
from information.lhbinfo
where tradingdate between @StartDate and @ExpirationDate;
GO
SET STATISTICS TIME OFF
GO

结果如下:

SQL Server 分析和编译时间:
   CPU 时间 = 0 毫秒,占用时间 = 2 毫秒。
SQL Server 分析和编译时间:
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。

(3696 行受影响)

SQL Server 执行时间:
   CPU 时间 = 0 毫秒,占用时间 = 215 毫秒。
SQL Server 分析和编译时间:
   CPU 时间 = 0 毫秒,占用时间 = 1 毫秒。

SQL Server 执行时间:
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。

(3696 行受影响)

SQL Server 执行时间:
   CPU 时间 = 0 毫秒,占用时间 = 247 毫秒。
SQL Server 分析和编译时间:
   CPU 时间 = 0 毫秒,占用时间 = 0 毫秒。

思考:看来用变量的值代入生成动态SQL语句执行速度要稍快些

原创粉丝点击