sql性能问题
我在 SQL Server 2000 中有一个包含 24M 记录的数据库。
当我运行此查询时
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and '2011-05-16 00:00:00.000'
,即使这样,
declare @MinDate char(30) ,@MaxDate char(30)
set @MinDate=substring(convert(char,(getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate=substring(convert(char,(getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and @MaxDate
它也运行得非常快,并在前 10 秒内返回 3500 条记录,请注意 starttime
是 char(30 )
在数据库中
但是当我运行这个查询时它只在 10~60 秒内返回 32 条记录
declare @MinDate char(30), @MaxDate char(30)
set @MinDate = substring(convert(varchar, (getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate = substring(convert(varchar, (getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between @MinDate and @MaxDate
:: @MinDate 值是 2011-05-15 00:00:00.000
请注意,starttime
在我的数据库中建立了索引
我想知道我的问题是什么?
I have a database with 24M records in SQL Server 2000.
When I run this query
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and '2011-05-16 00:00:00.000'
and even this
declare @MinDate char(30) ,@MaxDate char(30)
set @MinDate=substring(convert(char,(getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate=substring(convert(char,(getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between '2011-05-15 00:00:00.000' and @MaxDate
it runs very fast and return 3500 records in firs 10 seconds, note that starttime
is char(30)
in database
But when I run this query it just return 32 records in 10~60 seconds
declare @MinDate char(30), @MaxDate char(30)
set @MinDate = substring(convert(varchar, (getdate()-1), 120),1,10)+' 00:00:00.000'
set @MaxDate = substring(convert(varchar, (getdate()), 120),1,10)+' 00:00:00.000'
select * from cdr
where starttime between @MinDate and @MaxDate
:: @MinDate value is 2011-05-15 00:00:00.000
Note that starttime
is indexed in my database
I want to know what is my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Sql 服务器可能有一个为带有 2 个参数的查询缓存的查询计划,该计划对于您的参数值来说不是最佳的。在 sqlserver 2008 中,您可以使用优化未知提示。 http://blogs.msdn.com/b/sqlprogrammability/archive/2008/11/26/optimize-for-unknown-a-little-known-sql-server-2008-feature.aspx
在 SQL Server 2000 中,您可以尝试本文中提到的其他选项之一。
Sql server probably has a query plan cached for the query with 2 parameters that is not optimal for your parameter values. In sqlserver 2008 you can use the Optimize for unknown hint. http://blogs.msdn.com/b/sqlprogrammability/archive/2008/11/26/optimize-for-unknown-a-little-known-sql-server-2008-feature.aspx
In SQL server 2000 you can try one of the other options mentioned in the article.
与此处的问题相同 和 此处。使用
选项(重新编译)
Same issue as here and here. Use
OPTION (RECOMPILE)