sql server 存储过程的优化.(变量表,临时表的简单分析)
2007-08-31 12:27
543 查看
昨日一朋友发来一段sql的存储过程(如下),让我看看能不能优化一下。
insert @T1
select g_no,co_no,si_no,str_no,sum(ind_qty) as qty
from instock_detail where in_id = @id group by g_no,co_no,si_no,str_no
--?unitstock -->保存在变量表中
insert @T2
select a.*
from unitstock a,@T1 b
where a.g_no =b.g_no and a.co_no =b.co_no
and a.si_no =b.si_no and a.str_no=b.str_no
delete unitstock
from @T1 a
where unitstock.g_no=a.g_no and unitstock.co_no =a.co_no
and unitstock.si_no=a.si_no and unitstock.str_no=a.str_no
insert unitstock
select g_no,co_no,si_no,str_no,sum(qty) as qty from
( select * from @T1 union all select * from @T2
) AA
group by g_no,co_no,si_no,str_no
今日有空,作了一下变量表,临时表插入数据的性能分析。
1。变量表:
declare @t table
(
id nvarchar(50),
supno nvarchar(50),
eta datetime
)
insert @t
select top 10000 id,supno,eta from 表
这一句执行sql需时间:16806ms
2。临时表:
create table #t
(
id nvarchar(50),
supno nvarchar(50),
eta datetime
)
insert #t
select top 10000 id,supno,eta
from 表
这一句执行sql需时间:76ms
3。不创建临时表,直接插入到临时表
select top 10000 id,supno,eta
into #t
from 表
这一句执行sql需时间:30ms
通过以上的分析,可以非常清晰的看出那个优,那个劣了。
以上只是简单的分析了一下。所以在存储过程中尽量合作临时表来存储临时数据,不要使用变量表。
insert @T1
select g_no,co_no,si_no,str_no,sum(ind_qty) as qty
from instock_detail where in_id = @id group by g_no,co_no,si_no,str_no
--?unitstock -->保存在变量表中
insert @T2
select a.*
from unitstock a,@T1 b
where a.g_no =b.g_no and a.co_no =b.co_no
and a.si_no =b.si_no and a.str_no=b.str_no
delete unitstock
from @T1 a
where unitstock.g_no=a.g_no and unitstock.co_no =a.co_no
and unitstock.si_no=a.si_no and unitstock.str_no=a.str_no
insert unitstock
select g_no,co_no,si_no,str_no,sum(qty) as qty from
( select * from @T1 union all select * from @T2
) AA
group by g_no,co_no,si_no,str_no
今日有空,作了一下变量表,临时表插入数据的性能分析。
1。变量表:
declare @t table
(
id nvarchar(50),
supno nvarchar(50),
eta datetime
)
insert @t
select top 10000 id,supno,eta from 表
这一句执行sql需时间:16806ms
2。临时表:
create table #t
(
id nvarchar(50),
supno nvarchar(50),
eta datetime
)
insert #t
select top 10000 id,supno,eta
from 表
这一句执行sql需时间:76ms
3。不创建临时表,直接插入到临时表
select top 10000 id,supno,eta
into #t
from 表
这一句执行sql需时间:30ms
通过以上的分析,可以非常清晰的看出那个优,那个劣了。
以上只是简单的分析了一下。所以在存储过程中尽量合作临时表来存储临时数据,不要使用变量表。
相关文章推荐
- sql server 存储过程的优化.(变量表,临时表的简单分析)
- sql server 存储过程的优化.(变量表,临时表的简单分析) (转)
- sql server 存储过程中使用变量表,临时表的分析(续)
- sql server 存储过程中使用变量表,临时表的分析(续)
- sql server 存储过程中使用变量表,临时表的分析(Z)
- sql server 存储过程中使用变量表,临时表的分析(续)
- sql server 存储过程中使用变量表,临时表的分析
- sql server 存储过程中使用变量表,临时表的分析
- [转]sql server 存储过程中变量表与临时表的分析比较
- MYSQL的存储过程、变量、函数及部分简单优化(非常有用,有存储过程+光标实例)
- sql server 存储过程的优化:简单测试在存储过程中临时表与union all的性能差别
- DB Optimizer:数据库SQL代码分析调试优化,数据库存储过程优化,数据库负载测试
- SQL Server 优化存储过程的七种方法
- Sql Server中存储过程中输入和输出参数(简单实例,一看就懂)
- SQL Server 优化存储过程的七种方法
- Sql Server 系统存储过程分析 1 - 目录存储过程
- 【转】sql server存储过程中SELECT 与 SET 对变量赋值的区别
- SQL Server 存储过程中的表变量名加上'[]'后出现执行错误
- sql server 表变量存储临时查询数据
- 【转】SQL SERVER 存储过程中变量的作用域