mysql大批量更新資料

2021-08-01 07:25:43 字數 1124 閱讀 1697

大批量的更新資料如果再使用傳統的(update)方法一條一條的更新,效率是很慢的,而且效能差,也很容易造成阻塞。

1:使用mysql 自有的語句構建批量更新

update tble

set price = case id

when 16 then 7.6

when 19 then 8.86

when 20 then 9

end

where id in (16,19,20)

這句sql 的意思是,更新表price欄位,如果id=16 則price的值為7.6,如果id=19 則price的值為8.86…等。

where部分不影響**的執行,但是會提高sql執行的效率。

如果要更新多個欄位和多個條件時的情況:

update tble

set price = case id

when

1then

3when

2then

4when

3then

5end,

name = case id

when

1then

'name 1'

when

2then

'name 2'

when

3then

'name 3'

endwhere id in (1,2,3) and pcate in (24,27,26) and ccate in (277,271,207)...[多條件時直接: and 字段 in()]

2.建立臨時表,先更新臨時表,然後從臨時表中update

create tem table tmp(id int(4) primary key,dr varchar(50));

insert into tmp values (0,』gone』), (1,』xx』),…(m,』yy』);

update test_tbl, tmp set test_tbl.dr=tmp.dr where test_tbl.id=tmp.id;

注意:這種方法需要使用者有tem 表的create 許可權。

問題諮詢群:472148690

MySQL大批量插入資料

1.對於myisam型別的表,可以通過以下方式快速的匯入大量的資料。alter table tblname disable keys loading the data alter table tblname enable keys 這兩個命令用來開啟或者關閉myisam表非唯一索引的更新。在匯入大量...

mysql跨庫批量更新大批量資料的思路

需求是這樣的,在a資料庫例項中定時讀取大批量資料 這裡暫定20w 然後在b資料庫例項中,將讀取的20w資料寫入b資料庫例項中的表 表裡有3000w的資料 插入前判斷條件使用者id,存在更新,不存在則插入。如果是幾萬條以下的資料,這很簡單。讀取資料 foreach 查詢是否存在 更新或插入。但是真是情...

oracle 更新大批量資料變更步驟

生產環境中遇到更新或者刪除大批量資料的時候,不能直接進行操作,要批量進行。1 獲取要進行更新的資料的主鍵,2 建立臨時表並將獲取的資料主鍵匯入到建立!bin sh created by yangql on 2011 11 23 parameters home oracle profile ora u...