Asp.net 同一查询的不同执行时间-SQL Server
我有一个问题:Asp.net 同一查询的不同执行时间-SQL Server,asp.net,sql-server,sql-server-2005,executiontimeout,Asp.net,Sql Server,Sql Server 2005,Executiontimeout,我有一个问题: Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12) 当我执行此查询时,执行需要1-2秒,但当我在存储过程中使用相同的查询时,下面的查询需要5分钟以上: If(Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)) BEGIN -- CREATE TE
Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
当我执行此查询时,执行需要1-2秒,但当我在存储过程中使用相同的查询时,下面的查询需要5分钟以上:
If(Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12))
BEGIN
-- CREATE TEMPORARY TABLE [Say: #temp1]
#temp1 => Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
inserting the same value in the temp table
drop #temp1
END
这可能是什么原因?我该如何解决这个问题?我正在从asp.net运行SP
declare @Count int
select @Count = count (a) from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
if(@Count > 0)
begin
#temp1 => Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
inserting the same value in the temp table
drop #temp1
end
我也遇到了同样的情况,就这样解决了
这可能是因为查询执行了两次,并且它包含一个子查询。执行这样的查询时,不知道内部到底发生了什么。但是像这样更改查询解决了我的延迟问题试试这个
declare @Count int
select @Count = count (a) from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
if(@Count > 0)
begin
#temp1 => Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
inserting the same value in the temp table
drop #temp1
end
我也遇到了同样的情况,就这样解决了
这可能是因为查询执行了两次,并且它包含一个子查询。执行这样的查询时,不知道内部到底发生了什么。但是像这样更改查询解决了我的延迟问题一个EXISTS将为您短路IF
If EXISTS (Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12))
BEGIN
-- CREATE TEMPORARY TABLE [Say: #temp1]
#temp1 => Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
inserting the same value in the temp table
END
但是,为什么不查询tbl_abc和tbl_xyz一次呢
Select a
INTO #temp1
from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
IF EXISTS (SELECT * FROM #temp1) /* or use @@ROWCOUNT*/
BEGIN
--DoStuff
END
drop TABLE #temp1
EXISTS将为您短路IF
If EXISTS (Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12))
BEGIN
-- CREATE TEMPORARY TABLE [Say: #temp1]
#temp1 => Select a from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
inserting the same value in the temp table
END
但是,为什么不查询tbl_abc和tbl_xyz一次呢
Select a
INTO #temp1
from tbl_abc where id in ( select id from tbl_xyz where mainid = 12)
IF EXISTS (SELECT * FROM #temp1) /* or use @@ROWCOUNT*/
BEGIN
--DoStuff
END
drop TABLE #temp1
mainid值实际上是硬编码的(12),还是这只是一个简单的示例,实际上,您是将此值作为参数传递到存储的proc中?(如果是硬编码,您可能希望忽略以下内容) 如果“12”实际上是一个参数,那么您可能是参数嗅探的受害者
提到但未解释的一种解决方案是屏蔽参数——通过声明一个局部变量,将其设置为参数的值并在查询中使用它来实现这一点。mainid值实际上是硬编码的(12),还是这只是一个简单的示例,实际上,您正在将该值作为参数传递到存储的进程中?(如果是硬编码,您可能希望忽略以下内容) 如果“12”实际上是一个参数,那么您可能是参数嗅探的受害者
提到但未解释的一种解决方案是屏蔽参数——通过声明一个局部变量,将其设置为参数的值,并在查询中使用它来实现这一点。为什么使用IF语句?你想做什么?你检查过查询计划和io/cpu统计数据吗?对于那些面临相同问题的人:两个答案[by:binil&by:gbn]对我都有效。为什么使用IF语句?你想做什么?你检查过查询计划和io/cpu统计数据了吗?对于那些面临相同问题的人:两个答案[by:binil&by:gbn]对我都有效。这虽然有效,但效率不高。根据我的答案和@gbn,您将使用EXISTS,但在使用“EXISTS”时,我发现了相同的问题。执行起来花了太多时间。但当我像上面的答案那样改变它时,它开始正常工作。我不知道为什么。@gbn在sql server exists中检查时工作正常。但是,当从我的应用程序调用它时,它会被延迟,有时会抛出异常。而且我的表包含超过1000000条记录这是无效的,尽管它可以工作。根据我的答案和@gbn,您将使用EXISTS,但在使用“EXISTS”时,我发现了相同的问题。执行起来花了太多时间。但当我像上面的答案那样改变它时,它开始正常工作。我不知道为什么。@gbn在sql server exists中检查时工作正常。但是,当从我的应用程序调用它时,它会被延迟,有时会抛出异常。我的表包含超过1000000条记录