ORACLE UPDATE 語句語法與效能分析
為了方便起見,建立了以下簡單模型,和構造了部分測試資料:
在某個業務受理子系統BSS中,
--客戶資料表
create table customers
(
customer_id number(8) not null, -- 客戶標示
city_name varchar2(10) not null, -- 所在城市
customer_type char(2) not null, -- 客戶型別
...
)
create unique index PK_customers on customers (customer_id)
由於某些原因,客戶所在城市這個資訊並不什麼準確,但是在
客戶服務部的CRM子系統中,透過主動服務獲取了部分客戶20%的所在
城市等準確資訊,於是你將該部分資訊提取至一張臨時表中:
create table tmp_cust_city
(
customer_id number(8) not null,
citye_name varchar2(10) not null,
customer_type char(2) not null
)
1) 最簡單的形式
--經確認customers表中所有customer_id小於1000均為'北京'
--1000以內的均是公司走向全國之前的本城市的老客戶:)
update customers
set city_name='北京'
where customer_id<1000
2) 兩表(多表)關聯update -- 僅在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
)
-- 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
)
注意在這個語句中,
=(select b.city_name,b.customer_type
from tmp_cust_city b
where b.customer_id=a.customer_id
)
與
(select 1
from tmp_cust_city b
where b.customer_id=a.customer_id
)
是兩個獨立的子查詢,檢視執行計劃可知,對b表/索引掃描了2篇;
如果捨棄where條件,則預設對A表進行全表
更新,但由於(select b.city_name from tmp_cust_city b where
where b.customer_id=a.customer_id)
有可能不能提供"足夠多"值,因為tmp_cust_city只是一部分客戶的資訊,
所以報錯(如果指定的列--city_name可以為NULL則另當別論):
01407, 00000, "cannot update (%s) to NULL"
// *Cause:
// *Action:
一個替代的方法可以採用:
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)
或者
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帶來更好的效能:
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。否則報錯:
01779, 00000, "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
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
)
當對於一個給定的a.customer_id
(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
返回多餘1條的情況,則會報如下錯誤:
01427, 00000, "single-row subquery returns more than one row"
// *Cause:
// *Action:
一個比較簡單近似於不負責任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)
如何理解 01427 錯誤,在一個很複雜的多表連線update的語句,經常因考慮不周,出現這個錯誤,
仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表示式 中,使用group by 和
having 字句檢視重複的紀錄
(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(*)>=2
)
作者Blog:http://blog.csdn.net/aceplus/
相關文章
ORACLE UPDATE 語句語法與效能分析的一點看法
移動的微軟移動的Windows
[收藏]資料庫設計中的反規範技術探討
Oracle9i與SYBASE ASE12.5相比的幾個不足
如何在已裝Windowns XP機器上再裝Windows 2000
對該文的評論
aceplus ( 2005-07-04)
/**** 改正1個錯誤 ****/
一個比較簡單近似於不負責任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b
where b.customer_id=
a.customer_id and rownum=1)
如何理解 01427 錯誤,在一個很複雜的多表連線update的語句,經常因考慮不周,出現這個錯誤,
仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表示式 中,使用group by 和
having 字句檢視重複的紀錄
(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(*)>=2
)
aceplus ( 2005-07-04)
/**** 改正1個錯誤 ****/
一個比較簡單近似於不負責任的做法是
update customers a -- 使用別名
set city_name=(select b.city_name from tmp_cust_city b
where b.customer_id=
a.customer_id rownum=1)
如何理解 01427 錯誤,在一個很複雜的多表連線update的語句,經常因考慮不周,出現這個錯誤,
仍已上述例子來描述,一個比較簡便的方法就是將A表代入 值表示式 中,使用group by 和
having 字句檢視重複的紀錄
(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(*)>=2
)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/751371/viewspace-594547/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ORACLE多表關聯UPDATE語句Oracle
- Oracle中 Update和insert結合語法Oracle
- ORACLE 資料庫 查詢語句與DML語句Oracle資料庫
- 【SQL】10 SQL UPDATE 語句SQL
- Oracle vs PostgreSQL,研發注意事項(13) - UPDATE語句OracleSQL
- MySQL -update語句流程總結MySql
- MySQL的update語句避坑MySql
- flask之控制語句 if 語句與for語句Flask
- ORACLE常用語句:Oracle
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- python基礎語法—語句Python
- Oracle基本SQL語句OracleSQL
- Oracle 建立序列語句Oracle
- 一個UPDATE語句引發的血案
- DBeaver如何生成select,update,delete,insert語句delete
- 6. Oracle開發和應用—6.4. PL/SQL語法—6.4.4. 條件語句(分支語句)OracleSQL
- insert into select語句與select into from語句
- mysql update join,insert select 語法MySql
- orcale 語句基本語法縮寫
- makefile--if條件判斷語句的語法與使用
- SQL查詢語句 (Oracle)SQLOracle
- sql語句效能優化SQL優化
- KunlunDB功能之insert/update/delete...returning語句delete
- oracle partition by 語法Oracle
- switch語句逆向分析
- oracle v$sqlare 分析SQL語句使用資源情況OracleSQL
- 18 與Oracle Data Guard 相關的SQL語句OracleSQL
- oracle中的條件語句Oracle
- Oracle資料庫語句大全Oracle資料庫
- 列出oracle dbtime得sql語句OracleSQL
- oracle檢視物件DDL語句Oracle物件
- 【LOB】Oracle lob管理常用語句Oracle
- oracle語句練習--初級Oracle
- oracle資料庫常用語句Oracle資料庫
- 比CRUD多一點兒(三):UPDATE、DELETE語句delete
- 輕量ORM-SqlRepoEx (四)INSERT、UPDATE、DELETE 語句ORMSQLdelete
- 一條update SQL語句是如何執行的SQL
- Mysql跨表更新 多表update sql語句總結MySql
- 語法與語義