存储过程中表连接的效率

使用存储过程取数据的时候,经常会碰到多表连接的问题,这种时候千万不要在insert的时候就实用join,不然数据量一大,运行效率就很成问题,如下:

insert into #temp(order_no,order_type,order_line_no,sku_no,part_no,pr_description,order_qty,unit_price)

        select

         order_no,order_type,order_line_no,t.sku_no,tt.part_no,tt.short_desc,order_qty,unit_price

        from CIS..order_detail t

            inner join CIS..part_master tt  on t.sku_no = tt.sku_no

         where order_no=@order_no and order_type=@order_type

这样运行效率是很低的

 

最好是insert和join分开,先insert完以后,用update来join更好些

insert into #temp(order_no,order_type,order_line_no,sku_no,order_qty,unit_price) select
 order_no,order_type,order_line_no,sku_no,order_qty,unit_price from CIS..order_detail
 where order_no=@order_no and order_type=@order_type

 

update #temp set a.part_no= b.part_no,
 a.description= b.short_desc 
 from #temp a
 inner join CIS..part_master b
 on a.sku_no=b.sku_no

这样运行效率就上去了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值