1.批量update,一條記錄update一次,性能很差
update test_tbl set dr='2' where id=1;
2.replace into 或者insert into ...on duplicate key update
replace into test_tbl (id,dr) values (1,'2'),(2,'3'),...(x,'y');
或者使用
insert into test_tbl (id,dr) values (1,'2'),(2,'3'),...(x,'y') on duplicate key update dr=values(dr);
3.創建臨時表,先更新臨時表,然後從臨時表中update
create temporary 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;
注意:這種方法需要用戶有temporary 表的create 權限。
下面是上述方法update 100000條數據的性能測試結果:
逐條update
real 0m15.557s
user 0m1.684s
sys 0m1.372s
replace into
real 0m1.394s
user 0m0.060s
sys 0m0.012s
insert into on duplicate key update
real 0m1.474s
user 0m0.052s
sys 0m0.008s
create temporary table and update:
real 0m0.643s
user 0m0.064s
sys 0m0.004s
就測試結果來看,測試當時使用replace into性能較好。
replace into 和insert into on duplicate key update的不同在於:
replace into 操作本質是對重複的記錄先delete 後insert,如果更新的字段不全會將缺失的字段置爲缺省值
insert into 則是隻update重複記錄,不會改變其它字段。
mysql 批量更新
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
sql server 所有表大小排序
xrbenbeba
2019-02-22 23:41:55
div標籤中 id和 class的區別
wjgdu
2019-02-22 23:39:04
postdrop: create file maildrop/xxx: Permission denied
lingping
2019-02-23 14:05:54
當您嘗試在 Outlook 中打開或保存電子郵件附件時出現錯誤消息:“無法創建文件
lingping
2019-02-23 14:05:54
連接查詢排序limit(join、order by、limit語句)
fdb2b
2019-02-23 14:01:03
關於數據庫的幾條常用操作
guokaikaikai
2019-02-23 13:32:01
MySQL使用索引優化DISTINCT操作
1415699306
2019-02-23 00:26:15
mysql數據庫改名的三種方法
李伯億
2019-02-23 00:14:25
Tomcat --> Cannot create a server using the selected type
小浩51
2019-02-23 00:02:03
MYSQL的賬號管理和授權
echoroot
2019-02-22 23:58:45
全國省市數據庫
wdj_student
2019-02-22 23:54:30
mysql存儲過程
osmanthus818
2019-02-22 23:29:07
CEPH RBD常用命令
aireal217
2019-02-22 23:25:53
使用svn和apache打造版本控制服務器
tangjilinux
2019-02-22 23:23:52