批量更新資料表

2022-03-15 13:32:43 字數 1383 閱讀 4172

幾天中午系統中斷了幾分鐘,原因是資料庫down了。當時發現系統中有大量的鎖,幾乎都是來自生產使用者的,被鎖的物件和語句都是平時每天都要千百次使用的。為什麼會出現這麼大面積的鎖,原因到現在都還沒弄明白。只是事後回憶起大量的鎖中有條update的語句,是有個同事在批量更新資料,而是是關聯了其他表的更新(資料量有160多萬),而被更新的表正是被鎖住的表,疑似這個更新導致了今天中午的down庫。整個down庫的過程是:資料庫有鎖-》2號機達到最大連線數-》停止2號機偵聽無效-》重啟2號機-》1號機失去響應達3分鐘左右。

這裡不是要分析宕庫的原因,而是要分析一下update語句。在網上找了個好帖:oracle update 語句語法與效能分析

一般的update就不說了,真正難的是關聯其他表得到被更新欄位的值。也就是上文中的第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

) 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),'未知')

-- 當然這不符合業務邏輯了

mysql更新表結構 mysql更新資料表結構命令

mysql中alter命令的用法,用於編輯表結構 修改表名alter table test rename test1 修改字段型別alter table employee change depno depno int 5 not null 加索引alter table 表名 add index 索引...

MySQL 更新基礎資料表

在pacs的mysql資料庫裡,收費專案與his收費專案不能同步,需要手工更新。一 使用insert into table onduplicate keyupdate 語句,將資料直接更新。資料庫會判斷,如果主鍵存在,則更新相應字段,如果主鍵不存在,則插入該行資料。相當於是 update 和 ins...

游標批 量刪除資料表

declare tablename varchar 30 sql varchar 500 declare cur delete table cursor read only forward only for select name from sysobjects where name like pu...