https://blog.csdn.net/funnyfu0101/article/details/52765235

总体原则:1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新

2)跨表更新时,set和where时,尽量减少扫描次数,从而提高优化

update更新实例:

1) 最简单的形式-单表更新

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

2) 两表(多表)关联update -- set为简单的数据(直接是值),且仅在where字句中的连接

SQL 代码
  1. --这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
  2. update customers a -- 使用别名
  3. set customer_type='01' --01 为vip,00为普通
  4. where exists (select 1
  5. from tmp_cust_city b
  6. where b.customer_id=a.customer_id
  7. )

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

SQL 代码
  1. update customers a -- 使用别名
  2. set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
  3. where exists (select 1
  4. from tmp_cust_city b
  5. where b.customer_id=a.customer_id
  6. )
  7. 优化:单个字段的优化,简化为扫描一遍
    7.1 SQL 代码
    1. update customers a -- 使用别名
    2. set city_name=nvl((select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id),a.city_name)
  8. -- update 超过2个值(字段)
  9. update customers a -- 使用别名
  10. set (city_name,customer_type)=(select b.city_name,b.customer_type
  11. from tmp_cust_city b
  12. where b.customer_id=a.customer_id)
  13. where exists (select 1
  14. from tmp_cust_city b
  15. where b.customer_id=a.customer_id
  16. )

3的缺点,就是对表B进行两遍扫描;

4) 特殊情况的优化:

因为B表的纪录只有A表的20-30%的纪录数,且

A表使用INDEX的情况

使用cursor也许会比关联update带来更好的性能:

SQL 代码
  1. set serveroutput on
  2. declare
  3. cursor city_cur is
  4. select customer_id,city_name
  5. from tmp_cust_city
  6. order by customer_id;
  7. begin
  8. for my_cur in city_cur loop
  9. update customers
  10. set city_name=my_cur.city_name
  11. where customer_id=my_cur.customer_id;
  12. /** 此处也可以单条/分批次提交,避免锁表情况 **/
  13. -- if mod(city_cur%rowcount,10000)=0 then
  14. -- dbms_output.put_line('----');
  15. -- commit;
  16. -- end if;
  17. end loop;
  18. end;

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

SQL 代码
  1. update (select a.city_name,b.city_name as new_name
  2. from customers a,
  3. tmp_cust_city b
  4. where b.customer_id=a.customer_id
  5. )
  6. set city_name=new_name

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

Oracle :多表更新多个字段的更多相关文章

  1. Oracle多表更新及MERGE命令和闪回机制还原数据表

    一.多表更新 比如线上有个系统由于某一个模块出现异常,导致系统整体的数据出现了错误,需要你手动改写数据库错误,Oracle update语句更新的值来自另一张表 update语法最基本的格式为 UPD ...

  2. SQL 根据关联表更新主表中字段数据

    今天遇到一个客户的数据更新问题,两个相关联的表,一个主表用于保存单据主要信息,一个副表用于保存单据的明细信息:现在要把主表的其中一个字段的数据更新到副表的一个字段中保存.精通的SQL语法的,当然是很简 ...

  3. oracle 联表更新

    依 a 表 cate_pub_id  为依据 更新 v 表的 cate_pub_id update td_tobrel_cate_pub_attrval v set v.CATE_PUB_ID=(se ...

  4. ORACLE根据两个表都含有的字段条件来判断两个表连接后有没有数据

    A表  字段1 字段2 B表  字段1 字段3 A表的字段1=B表的字段1 SELECT DISTINCT A.字段2 FROM TABEL1  A ,TABEL2  B  WHERE  A.字段1 ...

  5. oracle大表添加字段default经验分享

    当oracle单表数据量上亿时,对表进行alter table aa add column_1 varchar2(2) defalut 'Y';时,效率及安全性是必须考虑的因素. 本帖以2亿的数据表a ...

  6. oracle数据库,怎么给已有数据的表添加自增字段

    场景:数据仓库,ODI为使用Oracle Incremental Update IKM,需要对一事实表增加主键. 思想:基于老表创建新表,添加自增字段(序列+触发器实现),把老数据导入新表,删除老表, ...

  7. [K/3Cloud] KSQL 关联表更新字段Update语法

    关联表更新字段 UPDATE tmp369faa3f7d224b0595670425008 as t1 SET FStatus=-1 where exists(select 1 from t_BD_S ...

  8. sqlserver 将一个表中的某些字段更新到另一个表中(转载)

    来源:https://blog.csdn.net/qq_23888451/article/details/86615555 https://blog.csdn.net/cyxinda/article/ ...

  9. oracle的表名、字段名、constraint名的长度限制分别是多少?

    文章出处:http://blog.csdn.net/haiross/article/details/38379615 Oracle:表名.字段名.constraint名的长度有限制 oracle 的命 ...

随机推荐

  1. 12 个 JS 技巧

    1. 过滤唯一值 ES6 引入了 Set 对象和延展(spread)语法…,我们可以用它们来创建一个只包含唯一值的数组. 复制代码     const array = [1, 1, 2, 3, 5, ...

  2. ie 9.10 兼容性问题 遇到的坑

    1.ie9 中ajax 跨域调用时 error报错信息为”No Transport”   原因是 ajax跨域 本人用的是 cors解决方案 但是ie9一下版本 对cors默认是不允许的所以需要我们自 ...

  3. Angular2+AngularJS

    AngularJS 系列: 1.angular.module 的定义 var mapApp = angular.module("positionSalaryEditApp",[&q ...

  4. 动态调用WebService(传对象返回接受对象)

    基础属性//客户端代理服务命名空间,可以设置成需要的值. string ns = string.Format("WindowsForms"); private Assembly a ...

  5. 黄聪:C#获取网页HTML内容的三种方式

    C#通常有三种方法获取网页内容,使用WebClient.WebBrowser或者HttpWebRequest/HttpWebResponse. 方法一:使用WebClient static void ...

  6. Web高级 网站安全

    1. SQL注入 虽然现在SQL注入发生的情况总的来说越来越少,还是提二句.关于什么是SQL注入大家都知道就不多说了. 1.1 原理 我们在做前端页面的时候,少不了会又各种输入框,然后通过GET或者P ...

  7. Solr4.7.0连接MySQL

    1.把Mysql的Jar包  例如:mysql-connector-java-5.1.8-bin.jar  或其他版本 放到D:\apache-tomcat-7.0.57\webapps\solr\W ...

  8. 【rabbitmq】RabbitMQ 集群与网络分区

    网络分区(network partitions) 官网-网络分区 网络设备故障导致的网络分裂.比如,存在A\B\C\D\E五个节点,A\B处于同一子网,B\C\D处于另外一子网,中间通过交换机相连.若 ...

  9. Telegraf安装与介绍

    Telegraf 是什么? Telegraf 是一个用 Go 编写的代理程序,是收集和报告指标和数据的代理.可收集系统和服务的统计数据,并写入到 InfluxDB 数据库.Telegraf 具有内存占 ...

  10. homestead安装

    所谓Homestead,其实就是一个虚拟机镜像. 为什么用它?它的优点可以去自行百度.(虽然我还是用了集成环境 xampp,但是我还是不推荐的,特别是统一开发环境.或者去使用诸如Laravel的框架, ...