为什么此存储过程在 Management Studio 中执行得很快,但在应用程序中却不然?
我将此查询作为存储过程:
SELECT ID
FROM dbo.tblRentalUnit
WHERE (NOT EXISTS
(SELECT 1 AS Expr1
FROM dbo.tblTenant
WHERE (dbo.tblRentalUnit.ID = UnitID)))
在 Microsoft SQL Server Management Studio Express 中,它在 16 毫秒内执行。 当我将其放入 Visual Studio 2008 自动生成的类型化数据集中时,它的执行时间为 64,453 毫秒。 超过一分钟。
估计和执行计划是这样的:
Select [0%] <- Filter [1%] <- Merge Join (Left Outer Join) [28%] <- Index Scan [16%] <- Sort [43%] <- Clustered Index Scan [12%]
为什么会出现这种差异,我该如何纠正它?
I have this query as a stored procedure:
SELECT ID
FROM dbo.tblRentalUnit
WHERE (NOT EXISTS
(SELECT 1 AS Expr1
FROM dbo.tblTenant
WHERE (dbo.tblRentalUnit.ID = UnitID)))
In Microsoft SQL Server Management Studio Express, it executes in 16 ms. When I have it in a typed dataset auto-generated by Visual Studio 2008, it executes in 64,453 ms. More than a minute.
Estimated and Execution plan are like this:
Select [0%] <- Filter [1%] <- Merge Join (Left Outer Join) [28%] <- Index Scan [16%] <- Sort [43%] <- Clustered Index Scan [12%]
Why is this difference here, and how can I correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是一个错误缓存的查询计划。
您的索引和统计数据是最新的吗?
顺便说一句,如果 tblTenant.UnitId 是 tblRentalUnit.ID 的外键
那么你的查询可以重写为:
It sounds like an incorrectly cached query plan.
Are your indexes and statistics up to date?
BTW, if tblTenant.UnitId is a Foriegn Key into tblRentalUnit.ID
then your query can be rewritten as:
您是否在每次测试(来自任一客户端)之前刷新缓冲区? 确保在每个测试之间执行 DBCC DROPCLEANBUFFERS 和 DBCC FREEPROCCACHE。
Did you flush the buffers before each test (from either client)? Make sure you are executing DBCC DROPCLEANBUFFERS and DBCC FREEPROCCACHE between each test.
tblRentalUnit.ID 是否已正确索引?
Is tblRentalUnit.ID properly indexed?