游标和update 效率问题

[复制链接]
查看11 | 回复7 | 2014-2-27 06:00:13 | 显示全部楼层 |阅读模式
表A: 会话级临时表, 12条数据
表B: 分区表, 普通索引 id,t_date;1200万条数据,年分区,但是A表的时间区间跨年了;
SQL 1: for cur in (select id, beg_date, end_date from a) loopupdate a set a.money = (select sum(b.money)
from b
where b.id = cur.id
and b.t_date >= cur.beg_date
and b.t_date 复制代码执行时间 5s 左右;
SQL 2:update a set a.money = (select sum(b.money)
from b where b.id = a.id and b.t_date >= a.beg_date and b.t_date 复制代码执行时间 超过 60s 主动中断了...
想知道下为什么?

回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
呼叫版主
回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
Try

merge into a t1
using(select a.id,

sum(b.money) sum_money
from a, b
where b.id = a.id
and b.t_date >= a.beg_date
and b.t_date = a.beg_date

and b.t_date = a.beg_date

and b.t_date <= a.end_date
) as money2
from a
)
SET money=money2;

回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
第2个update没有 where a.id = cur.id;
回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
是不是游标走索引,后面那个走全表
回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
提供一下这两个SQL的执行计划。
回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
循环12次5秒,每次不到一秒,很像用到索引
回复

使用道具 举报

千问 | 2014-2-27 06:00:13 | 显示全部楼层
bfc99 发表于 2015-8-11 13:05
提供一下这两个SQL的执行计划。

同求。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行