Oracle Update 语句语法与性能分析 - 多表关联

 

为了方便起见,建立了以下简单模型,和构造了部分测试数据: 
在某个业务受理子系统BSS中,

SQL 代码
--客户资料表
create table customers
(
customer_id number() not null, -- 客户标示
city_name varchar2() not null, -- 所在城市
customer_type ) not null, -- 客户类型
...
)
create unique index PK_customers on customers (customer_id)

由于某些原因,客户所在城市这个信息并不什么准确,但是在 
客户服务部的CRM子系统中,通过主动服务获取了部分客户20%的所在 
城市等准确信息,于是你将该部分信息提取至一张临时表中:

SQL 代码
create table tmp_cust_city
(
customer_id number() not null,
citye_name varchar2() not null,
customer_type ) not null
)

1) 最简单的形式

SQL 代码
--经确认customers表中所有customer_id小于1000均为'北京'
--1000以内的均是公司走向全国之前的本城市的老客户:)
update customers
set city_name='北京'

2) 两表(多表)关联update -- 仅在where字句中的连接

SQL 代码
--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
update customers a -- 使用别名
 为vip,00为普通

from tmp_cust_city b
where b.customer_id=a.customer_id
)

3) 两表(多表)关联update -- 被修改值由另一个表运算而来

SQL 代码
 update customers a -- 使用别名
 set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

 from tmp_cust_city b
 where b.customer_id=a.customer_id
 )
 -- update 超过2个值
 update customers a -- 使用别名
 set (city_name,customer_type)=(select b.city_name,b.customer_type
 from tmp_cust_city b
 where b.customer_id=a.customer_id)

 from tmp_cust_city b
 where b.customer_id=a.customer_id
 )

注意在这个语句中,

=(select b.city_name,b.customer_type from tmp_cust_city b
where b.customer_id=a.customer_id ) 

( from tmp_cust_city b
where b.customer_id=a.customer_id) 

是两个独立的子查询,查看执行计划可知,对b表/索引扫描了2篇; 
如果舍弃where条件,则默认对A表进行全表 
更新,但由于

SQL 代码
select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id

有可能不能提供"足够多"值,因为tmp_cust_city只是一部分客户的信息,所以报错(如果指定的列--city_name可以为NULL则另当别论):

SQL 代码
, , "cannot update (%s) to NULL"
// *Cause:
// *Action:

一个替代的方法可以采用:

SQL 代码
update customers a -- 使用别名
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)

或者

SQL 代码
set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),'未知')

-- 当然这不符合业务逻辑了 

4) 上述3)在一些情况下,因为B表的纪录只有A表的20-30%的纪录数, 
考虑A表使用INDEX的情况,使用cursor也许会比关联update带来更好的性能:

SQL 代码
  1. set serveroutput on
    declare
    cursor city_cur is
    select customer_id,city_name
    from tmp_cust_city
    order by customer_id;
    begin
    for my_cur in city_cur loop
    update customers
    set city_name=my_cur.city_name
    where customer_id=my_cur.customer_id;
    /** 此处也可以单条/分批次提交,避免锁表情况 **/
    -- )= then
    -- dbms_output.put_line('----');
    -- commit;
    -- end if;
    end loop;
    end;

5) 关联update的一个特例以及性能再探讨 
在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:

SQL 代码
update (select a.city_name,b.city_name as new_name
from customers a,
tmp_cust_city b
where b.customer_id=a.customer_id
)
set city_name=new_name

这样能避免对B表或其索引的2次扫描,但前提是 A(customer_id) b(customer_id)必需是unique index或primary key。否则报错:

SQL 代码
, , "cannot modify a column which maps to a non key-preserved table"
// *Cause: An attempt was made to insert or update columns of a join view which
// map to a non-key-preserved table.
// *Action: Modify the underlying base tables directly.

6)oracle另一个常见错误 
回到3)情况,由于某些原因,tmp_cust_city customer_id 不是唯一index/primary key

SQL 代码
update customers a -- 使用别名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)

from tmp_cust_city b
where b.customer_id=a.customer_id
)

当对于一个给定的a.customer_id 
(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id) 
返回多余1条的情况,则会报如下错误:

SQL 代码
, , "single-row subquery returns more than one row"
// *Cause:
// *Action:

一个比较简单近似于不负责任的做法是

SQL 代码
update customers a -- 使用别名
)

如何理解 01427 错误,在一个很复杂的多表连接update的语句,经常因考虑不周,出现这个错误, 
仍已上述例子来描述,一个比较简便的方法就是将A表代入 值表达式 中,使用group by 和 
having 字句查看重复的纪录

SQL 代码
(select b.customer_id,b.city_name,count(*)
from tmp_cust_city b,customers a
where b.customer_id=a.customer_id
group by b.customer_id,b.city_name
having count(*)>=
)
 

Oracle Update 语句语法与性能分析 - 多表关联的更多相关文章

  1. oracle sql语句跟踪及性能分析工具实现

    在网上找了一大圈,没找着合适的工具来跟踪oracle一段时间的sql. 我们的场景是打算自动化跑遍所有场景(rft)+fiddler跟踪请求+后端跟踪sql,根据结果去分析慢的请求和sql,本来awr ...

  2. mysql Update语句 语法

    mysql Update语句 语法 作用:用于修改表中的数据.广州大理石机械构件 语法:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值 mysql Update语句 示例 ...

  3. oracle执行update语句时卡住问题分析及解决办法

    转载:http://www.jb51.net/article/125754.htm 这篇文章主要介绍了oracle执行update语句时卡住问题分析及解决办法,涉及记录锁等相关知识,具有一定参考价值, ...

  4. Oracle常用语句语法汇总

    第一篇  基本操作 --解锁用户   alter user 用户 account unlock; --锁定用户   alter user 用户 account lock; alter user sco ...

  5. Oracle Update语句

    Oracle没有update from语法,可以通过四种写法实现同样的功能: 一.标准update语法(常用.速度可能最慢) 当更新的表示单个或者被更新的字段不需要关联表带过来,此法是最好的选择. u ...

  6. Oracle AWRDD报告生成和性能分析

    我写的SQL调优专栏:https://blog.csdn.net/u014427391/article/category/8679315 对于局部的,比如某个页面列表sql,我们可以使用Oracle的 ...

  7. Oracle ADDM报告生成和性能分析

    我写的SQL调优专栏:https://blog.csdn.net/u014427391/article/category/8679315 对于局部的,比如某个页面列表sql,我们可以使用Oracle的 ...

  8. Oracle AWRSQRPT报告生成和性能分析

    我写的SQL调优专栏:https://blog.csdn.net/u014427391/article/category/8679315 对于局部的,比如某个页面列表sql,我们可以使用Oracle的 ...

  9. Oracle ASH报告生成和性能分析

    我写的SQL调优专栏:https://blog.csdn.net/u014427391/article/category/8679315 对于局部的,比如某个页面列表sql,我们可以使用Oracle的 ...

随机推荐

  1. Kotlin:Android世界的Swift

    转自:http://www.infoq.com/cn/news/2015/06/Android-JVM-JetBrains-Kotlin Kotlin是一门与Swift类似的静态类型JVM语言,由Je ...

  2. commandline (命令行)登录mysql

    mysql登录在命令行登录的时候是通过mysql的里面的程序的录的 其登录格式有两种:(在oracle上看到的是列出这两种,不要既有全参数名又有缩写参数名.) 1.全参数名登录释例 mysql --h ...

  3. iOS运用fabric记录crash日志过程

    先前运用友盟记录app闪退,发现有些闪退的记录无法明确定位到详细的位置,决定运用fabric进行闪退的记录:网上也有这方面的记录,有些细节的内容不明确,把今天碰到的坑整理记发不一下: 访问官网地址(进 ...

  4. 用Reveal分析第三方App的UI

    文章出自:听云博客 Reveal简介: 这是个神奇的工具,它能常透彻地分析个App的UI结构. 这个工具包括两部分,部分是在PC上运行的一个独立应用,即Reveal.app,另一部分代码在你要分析的某 ...

  5. Swift中的部分更新与旧版的区别

    1. 函数中的外部变量名取消 “#”方式,仅能用直接命名方式 错误 func swift(#str :NSString){} 正确 func swift(str str :NSString){} 2. ...

  6. 模仿password输入框

    function hiddenPass(event) { var password0 = document.getElementById("password0"); var pas ...

  7. 你知道C#中的Lambda表达式的演化过程吗

    你知道C#中的Lambda表达式的演化过程吗? 阅读目录 委托的使用 匿名方法 Func和Action Lambda的诞生 那得从很久很久以前说起了,记得那个时候... 懵懂的记得从前有个叫委托的东西 ...

  8. CentOS添加新硬盘到新的分区(xfs/ext4) 或者添加新分区

    CentOs添加新硬盘到新的分区(xfs/ext4)  添加新分区 转载请注明:http://www.cnblogs.com/juandx/p/5618162.html 这篇文章介绍怎么添加一块新的硬 ...

  9. 利用iframe实现无刷新上传处理

    继上一篇对上传异常进行处理之后,当上传异常的时候的错误体验并不是很好,这里介绍用iframe来进行错误提示 拦截错误 @ExceptionHandler(MaxUploadSizeExceededEx ...

  10. 用了星型转换的sql跑了5小时--->5mins的过程

    =================START================================ BI数据仓库环境里面跑着一个crontab job,一旦sql运行超过4hours,就会接 ...