由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - query analyzer VS. Stored procedure
相关主题
如何分析Stored Procedure运行时间过长?求被“开班”培养!
请教一个mssql的问题关于学习数据库我也说几句
SQL debug step into a store procedure from another one (转载)DBA主要是做优化还是维护
请问怎样找到StoredProcedures的修改记录?怎么找工作啊?
再请教一个问题:stored procedure能不能直接显示信息什么的? (转载)问一个 SQL combine records问题
怎么样提高SQL SERVER的编程水平?怎么在openquey里传变量.
请问如何DEPLOY STORED PROCEDURE啊来做sql题目。
开班了webbew SQL问题解法2 -- SQL 利器Recursive CTE
相关话题的讨论汇总
话题: startdate话题: query话题: stored话题: analyzer话题: procedure
进入Database版参与讨论
1 (共1页)
j*****n
发帖数: 1781
1
今儿我们组里的一哥们遇到一个奇怪的现象:
在query analyzer 里面运行一个query,花了15分钟得到结果,
然后放到把这个query做成一个stored procedure, 除了把where 里面的条件换成了变
量而已,结果需要30分钟才行。
我们的环境是用local的query analyzer 连到 DB server 上。
为什么会有这么大的区别呢?
w*******e
发帖数: 1622
2
那sproc里有啥?

【在 j*****n 的大作中提到】
: 今儿我们组里的一哥们遇到一个奇怪的现象:
: 在query analyzer 里面运行一个query,花了15分钟得到结果,
: 然后放到把这个query做成一个stored procedure, 除了把where 里面的条件换成了变
: 量而已,结果需要30分钟才行。
: 我们的环境是用local的query analyzer 连到 DB server 上。
: 为什么会有这么大的区别呢?

j*****n
发帖数: 1781
3
简单点说吧:
in QA:
SELECT * FROM tbl where startDate BETWEEN '2001-1-1', '2001-6-1'
in SP:
SELECT * FROM tbl where startDate BETWEEN @startDate, @endDate

【在 w*******e 的大作中提到】
: 那sproc里有啥?
c*****t
发帖数: 1879
4
在 PostgreSQL 上,prepared plan 可能会比直接叫 sql 慢:
There is a disadvantage to using parameters: since the planner does
not know the values that will be supplied for the parameters, it might
make worse planning choices than it would make for a normal command
with all constants visible.

【在 j*****n 的大作中提到】
: 简单点说吧:
: in QA:
: SELECT * FROM tbl where startDate BETWEEN '2001-1-1', '2001-6-1'
: in SP:
: SELECT * FROM tbl where startDate BETWEEN @startDate, @endDate

j*****n
发帖数: 1781
5
it would not be the case in MSSQL. except dynamic SQL, SP is pre-compiled
and the execution plan is fixed unless statistics changes.

【在 c*****t 的大作中提到】
: 在 PostgreSQL 上,prepared plan 可能会比直接叫 sql 慢:
: There is a disadvantage to using parameters: since the planner does
: not know the values that will be supplied for the parameters, it might
: make worse planning choices than it would make for a normal command
: with all constants visible.

c*****t
发帖数: 1879
6
No, you misunderstood. Compiled plan doesn't mean its execution plan
would be exactly the same as the direct SQL query execution plan. The
difference thus can lead to performance difference.
I'd suggest you to write a stored procedure that does direct query
and compare.

【在 j*****n 的大作中提到】
: it would not be the case in MSSQL. except dynamic SQL, SP is pre-compiled
: and the execution plan is fixed unless statistics changes.

j*****n
发帖数: 1781
7
I did not... I checked both execution plans, they are same...
however, the duration is different.

【在 c*****t 的大作中提到】
: No, you misunderstood. Compiled plan doesn't mean its execution plan
: would be exactly the same as the direct SQL query execution plan. The
: difference thus can lead to performance difference.
: I'd suggest you to write a stored procedure that does direct query
: and compare.

a*******t
发帖数: 891
8
parameter sniffing
it's a pretty oftenly seen problem but it's not very commonly known... M$ is
hidding this from the public...
one of the articles talked about it
http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx
bascially, you can create a local variable
create procedure usp_myProc
@startdate_in datetime
as
declare @startdate datetime
set @startdate = @startdate_in

【在 j*****n 的大作中提到】
: 今儿我们组里的一哥们遇到一个奇怪的现象:
: 在query analyzer 里面运行一个query,花了15分钟得到结果,
: 然后放到把这个query做成一个stored procedure, 除了把where 里面的条件换成了变
: 量而已,结果需要30分钟才行。
: 我们的环境是用local的query analyzer 连到 DB server 上。
: 为什么会有这么大的区别呢?

a*******t
发帖数: 891
9
you can also force the SP to be recompiled if you think the plan is out-
dated
but I suspect your problem is not about this

【在 j*****n 的大作中提到】
: it would not be the case in MSSQL. except dynamic SQL, SP is pre-compiled
: and the execution plan is fixed unless statistics changes.

j*****n
发帖数: 1781
10
yeah, that's should be the right answer.
thanks a lot.

is

【在 a*******t 的大作中提到】
: parameter sniffing
: it's a pretty oftenly seen problem but it's not very commonly known... M$ is
: hidding this from the public...
: one of the articles talked about it
: http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx
: bascially, you can create a local variable
: create procedure usp_myProc
: @startdate_in datetime
: as
: declare @startdate datetime

1 (共1页)
进入Database版参与讨论
相关主题
webbew SQL问题解法2 -- SQL 利器Recursive CTE再请教一个问题:stored procedure能不能直接显示信息什么的? (转载)
webbew SQL问题解法3 -- 还在远古时代总么办怎么样提高SQL SERVER的编程水平?
webbew SQL问题解法1 -- 90%以上的数据库版SQL问题可以用partition by解决请问如何DEPLOY STORED PROCEDURE啊
养老院人事 SQL难题解法综述 -- 申精开班了
如何分析Stored Procedure运行时间过长?求被“开班”培养!
请教一个mssql的问题关于学习数据库我也说几句
SQL debug step into a store procedure from another one (转载)DBA主要是做优化还是维护
请问怎样找到StoredProcedures的修改记录?怎么找工作啊?
相关话题的讨论汇总
话题: startdate话题: query话题: stored话题: analyzer话题: procedure