
sql server 存储过程的优化.(变量表,临时表的简单分析)
发布日期:2021-05-09 01:24:00
浏览次数:19
分类:博客文章
本文共 1084 字,大约阅读时间需要 3 分钟。
昨日一朋友发来一段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 @tselect top 10000 id,supno,eta from 表这一句执行sql需时间:16806ms
2。临时表:create table #t(id nvarchar(50),supno nvarchar(50),eta datetime)insert #tselect top 10000 id,supno,eta from 表这一句执行sql需时间:76ms
3。不创建临时表,直接插入到临时表select top 10000 id,supno,eta
into #tfrom 表这一句执行sql需时间:30ms
通过以上的分析,可以非常清晰的看出那个优,那个劣了。
以上只是简单的分析了一下。所以在存储过程中尽量合作临时表来存储临时数据,不要使用变量表。发表评论
最新留言
感谢大佬
[***.8.128.20]2025年05月02日 06时58分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
HTML中如何给HTML元素添加事件
2019-03-21
wpf 使用Font Awesome
2019-03-21
Windows10:远程桌面连接报错“出现身份验证错误。要求的函数不受支持”
2019-03-21
lettcode 221. 最大正方形
2019-03-21
0X3协议与数据包
2019-03-21
python解释器环境问题
2019-03-21
uni-app快速导入自己需要的插件
2019-03-22
编写xor_shellcode.py
2019-03-22
Echarts笔记
2019-03-22
Ubuntu 20.04 Docker 安装并配置
2019-03-22
在 eclipse 中将 web 项目部署到 tomcat 服务器上
2019-03-22
iOS关于申请公司开发者账号缴费支付
2019-03-22
10-3 A1-4在产品表中找出库存数量大于50的产品的信息 (20 分)
2019-03-22
Ajax学习笔记-错误的处理-7
2019-03-23
SparkStreaming利用队列生成测试数据源
2019-03-23
js——BOM操作知多少?
2019-03-23
划分子网与NAT的区别。。。
2019-03-23
钻石操作符使用升级
2019-03-23
设置方法区大小与OOM
2019-03-23
Laravel 直接返回404页面
2019-03-24