我写的一个存储过程,怎么执行速度比在查询分析器的慢很多呀?

meng_1226 2006-03-09 10:02:02
我写的一个存储过程,怎么执行速度比在查询分析器的慢很多呀?

存储过程如下:

CREATE procedure [pr_StubsA]
@StubsDate Datetime

AS

--SET NOCOUNT ON

--如果已有结存记录,则删除重新结存
if ((Select count(*) From StubsA Where Datediff(m,SDate,@StubsDate)=0)>0)
Delete From StubsA Where Datediff(m,SDate,@StubsDate)=0


--清空临时表
--Truncate Table StubsTemp1
CREATE TABLE #StubsTemp1 (GoodID INT PRIMARY KEY)


--添加上月结存表里的GoodID
Insert into #StubsTemp1 Select GoodID From StubsA Where Datediff(m,SDate,@StubsDate)=1 and (SNum<>0)

--添加当月入库的GoodID
Insert into #StubsTemp1 Select Distinct GoodID From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and StorID=1 and GoodID not in (Select GoodID From #StubsTemp1)

Insert into StubsA Select
@StubsDate,GoodID,

--上月库存
isnull((Select SNum From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LNum,
isnull((Select SWeight From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LWeight,
isnull((Select SMoney From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LMoney,
--当月正常入库(包括退库的方材)
(Select isnull(Sum(GNum*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InNum,
(Select isnull(Sum(GWeight*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InWeight,
(Select isnull(Sum(GMoney*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InMoney,
--当月其他入库
(Select isnull(Sum(GNum),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InMoney1,
--当月正常出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutNum,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutWeight,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutMoney,
--当月其他出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutMoney1,
--当月库存
0,0,0
From #StubsTemp1


--SET NOCOUNT OFF
GO

我在查询分析器里运行exec pr_stubsA '2006-3-1' ,执行时间要1分20多少秒,
但我执行下面的语句只要3秒,高手来帮忙呀,急死了!

declare @StubsDate datetime
set @stubsdate='2006-3-1'
--SET NOCOUNT ON

--如果已有结存记录,则删除重新结存
if ((Select count(*) From StubsA Where Datediff(m,SDate,@StubsDate)=0)>0)
Delete From StubsA Where Datediff(m,SDate,@StubsDate)=0


--清空临时表
--Truncate Table StubsTemp1
CREATE TABLE #StubsTemp1 (GoodID INT PRIMARY KEY)


--添加上月结存表里的GoodID
Insert into #StubsTemp1 Select GoodID From StubsA Where Datediff(m,SDate,@StubsDate)=1 and (SNum<>0)

--添加当月入库的GoodID
Insert into #StubsTemp1 Select Distinct GoodID From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and StorID=1 and GoodID not in (Select GoodID From #StubsTemp1)

Insert into StubsA Select
@StubsDate,GoodID,

--上月库存
isnull((Select SNum From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LNum,
isnull((Select SWeight From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LWeight,
isnull((Select SMoney From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=#StubsTemp1.GoodID),0) as LMoney,
--当月正常入库(包括退库的方材)
(Select isnull(Sum(GNum*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InNum,
(Select isnull(Sum(GWeight*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InWeight,
(Select isnull(Sum(GMoney*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InMoney,
--当月其他入库
(Select isnull(Sum(GNum),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=#StubsTemp1.GoodID) as InMoney1,
--当月正常出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutNum,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutWeight,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutMoney,
--当月其他出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=#StubsTemp1.GoodID) as OutMoney1,
--当月库存
0,0,0
From #StubsTemp1


我在查询分析器里运行exec pr_stubsA '2006-3-1' ,执行时间要1分20多少秒,
但我执行下面的语句只要3秒,高手来帮忙呀,急死了!




...全文
220 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugchen888 2006-03-09
  • 打赏
  • 举报
回复
把菜单里那个
--->query----->show excution plan
选项打开.
子陌红尘 2006-03-09
  • 打赏
  • 举报
回复
把临时表换成表变量,再试试二者之间的差别。
OracleRoob 2006-03-09
  • 打赏
  • 举报
回复
在查询分析器中分别执行存储过程和SQL语句,查看执行计划
meng_1226 2006-03-09
  • 打赏
  • 举报
回复
to wangtiecheng(cappuccino)

我在事件探查器中监视,
就只显示 存储过程总执行的时间,166秒,没有显示存储过程里面的各语句的执行时间呀!!!!
OracleRoob 2006-03-09
  • 打赏
  • 举报
回复
在事件探查器中监视一下,分析一下执行的瓶颈在哪个环节
meng_1226 2006-03-09
  • 打赏
  • 举报
回复
而且在查询分析器里运行的语句就是存储过程里的内容,
只不过把@StubsDate定义了一个变量!

大家快帮忙呀!!!!
meng_1226 2006-03-09
  • 打赏
  • 举报
回复
搞不懂了,
还是先完成任务再说吧!

谢谢各位了!
子陌红尘 2006-03-09
  • 打赏
  • 举报
回复
一个猜测:因为楼主之前的存储过程里创建了临时表,而存储过程内部创建的临时表的索引无法被预编译的存储过程引用,从而导致了查询效率的低下。
meng_1226 2006-03-09
  • 打赏
  • 举报
回复
to libin_ftsafe(子陌红尘) :
多谢了,用表变量是可以提高执行时间了,只不过还要40多秒的,

奇怪了,
我把上面的存储过程分成两个,把临时表变成表只执行时间只要2秒了
奇怪了,有哪位大侠遇到过这种情况!我的存储过程如下

CREATE procedure [pr_StubsA_1]
@StubsDate Datetime

AS

SET NOCOUNT ON

--如果已有结存记录,则删除重新结存
if ((Select count(*) From StubsA Where Datediff(m,SDate,@StubsDate)=0)>0)
Delete From StubsA Where Datediff(m,SDate,@StubsDate)=0


--清空临时表
Truncate Table StubsTemp1
--CREATE TABLE #StubsTemp1 (GoodID INT PRIMARY KEY)


--添加上月结存表里的GoodID
Insert into StubsTemp1 Select GoodID From StubsA Where Datediff(m,SDate,@StubsDate)=1 and (SNum<>0)

--添加当月入库的GoodID
Insert into StubsTemp1 Select Distinct GoodID From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and StorID=1 and GoodID not in (Select GoodID From StubsTemp1)

SET NOCOUNT OFF
GO


CREATE procedure [pr_StubsA_2]
@StubsDate Datetime

AS

SET NOCOUNT ON

Insert into StubsA Select
@StubsDate,GoodID,

--上月库存
isnull((Select SNum From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=StubsTemp1.GoodID),0) as LNum,
isnull((Select SWeight From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=StubsTemp1.GoodID),0) as LWeight,
isnull((Select SMoney From StubsA Where Datediff(m,SDate,@StubsDate)=1 and StubsA.GoodID=StubsTemp1.GoodID),0) as LMoney,
--当月正常入库(包括退库的方材)
(Select isnull(Sum(GNum*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=StubsTemp1.GoodID) as InNum,
(Select isnull(Sum(GWeight*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=StubsTemp1.GoodID) as InWeight,
(Select isnull(Sum(GMoney*(case when typeid=1 then 1 else -1 end)),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID in (1,10) and DetsAViewI.GoodID=StubsTemp1.GoodID) as InMoney,
--当月其他入库
(Select isnull(Sum(GNum),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=StubsTemp1.GoodID) as InNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=StubsTemp1.GoodID) as InWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewI Where Datediff(m,BDate,@StubsDate)=0 and TypeID=3 and DetsAViewI.GoodID=StubsTemp1.GoodID) as InMoney1,
--当月正常出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutNum,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutWeight,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=2 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutMoney,
--当月其他出库
(Select isnull(Sum(GNum),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutNum1,
(Select isnull(Sum(GWeight),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutWeight1,
(Select isnull(Sum(GMoney),0) From DetsAViewO Where Datediff(m,BDate,@StubsDate)=0 and TypeID=4 and DetsAViewO.GoodID=StubsTemp1.GoodID) as OutMoney1,
--当月库存
0,0,0
From StubsTemp1

SET NOCOUNT OFF
GO

然后在查询分析器里执行
exec pr_stubsa_1 '2006-3-1'
exec pr_stubsa_2 '2006-3-1'
只要2秒

哪位大侠给解释一下?

源码下载地址: https://pan.quark.cn/s/eb74eff766f6 在信息技术领域中,小程序作为一类轻量级的应用程序,凭借其无需安装、即开即用的特性,赢得了众多用户和程序设计人员的喜爱。本主题探讨的是一个2021年的最新影视小程序源代码,它是一个无需授权的版本,并且已经过破解,表明开发人员可以不受限制地运用和调整代码,无需顾虑授权方面的顾虑。影视小程序源代码的主要功能一般包含电影、电视剧、真人秀、动画片的分类呈现,视频的播放,以及用户注册、收藏、评论等互动特性。源代码的破解,使其内部机制和构造对外公开,有利于开发人员深入探究和个性化开发。对于初学者来说,这是一个极佳的学习资源,有助于理解小程序的开发流程和常见功能的实现途径。在介绍中说明,这个源代码还配备了详尽的构建指南,这无疑简化了应用过程。即便是对编程知识不太了解的用户,只要依照指南逐步操作,也能够成功地构建自己的影视小程序。指南通常涉及环境设置、项目导入、数据库接入、API接口配置、前端界面设计和后端逻辑编等环节,是学习小程序开发的关键参考资料。在构建期间,需要留意的是,源代码中可能包含的联系方式可能是付费服务,这部分内容可以安全地忽略。如果在构建时遭遇任何困难,介绍中提到可以随时咨询,提供者将给予指导,这为开发人员提供了良好的支持。对于“小程序源码”这一标识,我们可以理解为与小程序开发相关的源代码,不仅限于影视类别,但在这个案例中,其应用环境明确为影视播放。开发人员可以通过剖析和调整源码,增加新的功能,如付费访问、会员体系、推荐机制等,以满足不同的业务需求。至于“价值500的小程序劝解源码”这个文件名,可能意味着原作者或提供者对源码的估价,也可能是一种市场推广策略。无论价值如何,重要...
已经博主授权,源码转载自 https://pan.quark.cn/s/b9f820ba6b41 3Dmax是一款功能全面的三维设计、动画制作及渲染软件,在游戏开发、影视后期制作和建筑规划等多个行业得到普遍应用。FBX(Filmbox)是一种跨系统的数据传输标准,它使得用户能够在不同的三维设计软件之间共享模型、材质贴图、骨骼动作等数据资源。本篇内容将深入剖析3Dmax输出FBX插件的各项技术要点。3Dmax输出FBX插件作为3Dmax软件的一个附加组件,它能够帮助3Dmax更加便捷、精准地将模型数据传输至其他兼容FBX标准的应用程序,例如Unity、Unreal Engine或Maya等软件平台。该插件提供针对不同操作系统环境的32位与64位两种版本,旨在保障在各种计算机硬件配置下均能稳定运行。1. **FBX标准的优越性**: - **通用性**:FBX标准被众多主流三维设计工具所接纳,有效支持了跨系统的数据共享。 - **数据保真度**:FBX能够完整记录模型的几何构造、材质属性、纹理映射、光照效果、镜头参数以及骨骼动画等详细信息。 - **高效的文件存储**:与其他数据格式相比,FBX能在更小的文件体积中承载丰富的数据信息。2. **输出FBX的操作流程**: - 启动3Dmax程序,加载或构建出需要导出的三维模型。 - 在“文件”功能列表中选取“导出”,然后在出现的选项窗口中指定FBX作为导出格式。 - 在FBX导出配置界面中,用户可以个性化设定导出参数,比如是否包含纹理贴图、动态效果、摄像视角等。 - 确定合适的文件存储位置并命名,点击“导出”按钮完成整个导出过程。3. **FBX插件的功能特性与设置选项**: - **三维形体**:用户可以设定导出多边形网格、...

22,296

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧