Oracle :多表更新多个字段
https://blog.csdn.net/funnyfu0101/article/details/52765235
总体原则:1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新
2)跨表更新时,set和where时,尽量减少扫描次数,从而提高优化
update更新实例:
1) 最简单的形式-单表更新
- --经确认customers表中所有customer_id小于1000均为'北京'
- --1000以内的均是公司走向全国之前的本城市的老客户:)
- update customers
- set city_name='北京'
- where customer_id<1000
2) 两表(多表)关联update -- set为简单的数据(直接是值),且仅在where字句中的连接
- --这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别
- update customers a -- 使用别名
- set customer_type='01' --01 为vip,00为普通
- where exists (select 1
- from tmp_cust_city b
- where b.customer_id=a.customer_id
- )
3) 两表(多表)关联update -- 被修改值由另一个表运算而来
- update customers a -- 使用别名
- set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
- where exists (select 1
- from tmp_cust_city b
- where b.customer_id=a.customer_id
- )
- 优化:单个字段的优化,简化为扫描一遍
7.1 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)
- -- 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)
- where exists (select 1
- from tmp_cust_city b
- where b.customer_id=a.customer_id
- )
3的缺点,就是对表B进行两遍扫描;
4) 特殊情况的优化:
因为B表的纪录只有A表的20-30%的纪录数,且
A表使用INDEX的情况
使用cursor也许会比关联update带来更好的性能:
- 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;
- /** 此处也可以单条/分批次提交,避免锁表情况 **/
- -- if mod(city_cur%rowcount,10000)=0 then
- -- dbms_output.put_line('----');
- -- commit;
- -- end if;
- end loop;
- end;
5) 关联update的一个特例以及性能再探讨
在oracle的update语句语法中,除了可以update表之外,也可以是视图,所以有以下1个特例:
- 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
Oracle :多表更新多个字段的更多相关文章
- Oracle多表更新及MERGE命令和闪回机制还原数据表
一.多表更新 比如线上有个系统由于某一个模块出现异常,导致系统整体的数据出现了错误,需要你手动改写数据库错误,Oracle update语句更新的值来自另一张表 update语法最基本的格式为 UPD ...
- SQL 根据关联表更新主表中字段数据
今天遇到一个客户的数据更新问题,两个相关联的表,一个主表用于保存单据主要信息,一个副表用于保存单据的明细信息:现在要把主表的其中一个字段的数据更新到副表的一个字段中保存.精通的SQL语法的,当然是很简 ...
- oracle 联表更新
依 a 表 cate_pub_id 为依据 更新 v 表的 cate_pub_id update td_tobrel_cate_pub_attrval v set v.CATE_PUB_ID=(se ...
- ORACLE根据两个表都含有的字段条件来判断两个表连接后有没有数据
A表 字段1 字段2 B表 字段1 字段3 A表的字段1=B表的字段1 SELECT DISTINCT A.字段2 FROM TABEL1 A ,TABEL2 B WHERE A.字段1 ...
- oracle大表添加字段default经验分享
当oracle单表数据量上亿时,对表进行alter table aa add column_1 varchar2(2) defalut 'Y';时,效率及安全性是必须考虑的因素. 本帖以2亿的数据表a ...
- oracle数据库,怎么给已有数据的表添加自增字段
场景:数据仓库,ODI为使用Oracle Incremental Update IKM,需要对一事实表增加主键. 思想:基于老表创建新表,添加自增字段(序列+触发器实现),把老数据导入新表,删除老表, ...
- [K/3Cloud] KSQL 关联表更新字段Update语法
关联表更新字段 UPDATE tmp369faa3f7d224b0595670425008 as t1 SET FStatus=-1 where exists(select 1 from t_BD_S ...
- sqlserver 将一个表中的某些字段更新到另一个表中(转载)
来源:https://blog.csdn.net/qq_23888451/article/details/86615555 https://blog.csdn.net/cyxinda/article/ ...
- oracle的表名、字段名、constraint名的长度限制分别是多少?
文章出处:http://blog.csdn.net/haiross/article/details/38379615 Oracle:表名.字段名.constraint名的长度有限制 oracle 的命 ...
随机推荐
- 谷歌浏览器可以google了
做为一个开发者好多疑问点或者难点大多数时间 都在进行百度,百度也能解决问题,但是呢如果让我能够google呢?我肯定会优先google的,这里面能够搜到一些国外技术人的文章可供参考. 下面是一个能够支 ...
- tab页的使用方法
css代码: #main{ margin:0px; width:100%; height:540px; background:url(m.jpg) no-repeat; background-size ...
- DevExpress Grid使用checkBox选中的方法
到官网得到消息自13.2版本后的Dev Grid中均内置了CheckBox列多选功能.在寻找答案的过程的成果进行记录. 一.13.2版本以后用法 启用多选列 对Gird中的View进行以下属性设置: ...
- S50 抓取pattern数据
S50(原V50) 测试机台湾久元电子研发的一款数字芯片测试系统,行业内有很多人使用: 现在记录下S50抓取pattern数据的一些方法: 程序主要是通过read_log配合c代码实现,pattern ...
- Eclipse Memory Analyzer 分析内存泄露
OutOfMemoryError示例 代码 package com.walson.heap; import java.util.ArrayList;import java.util.List; /** ...
- cok-filter
小工具 # -*- coding: utf-8 -*- import pandas as pd def filterExcel(cokpath, keywords): df = pd.read_exc ...
- linux与C内存管理机制
转自知乎专栏:https://zhuanlan.zhihu.com/p/51855842?utm_source=wechat_session&utm_medium=social&utm ...
- three.js中的文字
1.三维文字 三维字体文字,使用的是FontLoader,字体文件通过来facetype.js生成 addCityText: function () { var self = this; var ci ...
- ZooKeeper是按照CP原则构建的,不适合做Service服务发现
一.cap 分布式领域中存在CAP理论,且该理论已被证明:任何分布式系统只可同时满足两点,无法三者兼顾. ①C:Consistency,一致性,数据一致更新,所有数据变动都是同步的. ②A:Avail ...
- Python中的取模运算
C++中的取模运算符%只能对整数使用(如果要对浮点数使用需要fmod),Python则不同,对整数或浮点数均有效. 在这里再介绍一下取模的定义:假设a,b两个数,那么a mod b = a - n*b ...