Redian新闻
>
query analyzer VS. Stored procedure
avatar
query analyzer VS. Stored procedure# Database - 数据库
j*n
1
今儿我们组里的一哥们遇到一个奇怪的现象:
在query analyzer 里面运行一个query,花了15分钟得到结果,
然后放到把这个query做成一个stored procedure, 除了把where 里面的条件换成了变
量而已,结果需要30分钟才行。
我们的环境是用local的query analyzer 连到 DB server 上。
为什么会有这么大的区别呢?
avatar
w*e
2
那sproc里有啥?

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

avatar
j*n
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里有啥?
avatar
c*t
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

avatar
j*n
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.

avatar
c*t
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.

avatar
j*n
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.

avatar
a*t
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 上。
: 为什么会有这么大的区别呢?

avatar
a*t
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.

avatar
j*n
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。