MySQL中replace into與replace區(qū)別詳解
本篇為拋磚引玉篇,之前沒關(guān)注過replace into 與replace 的區(qū)別。經(jīng)過多個場景測試,居然沒找到在插入數(shù)據(jù)的時候兩者有什么本質(zhì)的區(qū)別?如果了解詳情的伙伴們,請告知留言告知一二,不勝感激?。。?/p>
0.故事的背景
【表格結(jié)構(gòu)】
CREATE TABLE `xtp_algo_white_list` ( `strategy_type` int DEFAULT NULL, `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL, `status` int DEFAULT NULL, `destroy_at` datetime DEFAULT NULL, `created_at` datetime DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY `xtp_algo_white_list_UN` (`strategy_type`,`user_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin # `strategy_type`,`user_name` 這兩個是聯(lián)合唯一索引,多關(guān)注后續(xù)需要用到!??!
【需求:】
- 根據(jù)表格里面, 209133002266賬戶的數(shù)據(jù),重新插入一個用戶20220302001, 使得新生成的數(shù)據(jù)中strategy_type & status & destroy_at 字段與209133002266用戶的一致。
- 使用update 一條一條更新也行,但是比較慢。
- 使用replace into 效果會高很多,但是深入研究發(fā)現(xiàn)也有一些坑的地方
1.replace into 的使用方法
replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`) select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266; # replace into 后面跟表格+需要插入的所有字段名(自動遞增字段不用寫) # select 后面選擇的字段,如果根據(jù)查詢結(jié)果取值,則寫字段名;如果是寫死的,則直接寫具體值即可 # 可以理解為,第一部分是插入表格的結(jié)構(gòu),第二部分是你查詢的數(shù)據(jù)結(jié)果
2.有唯一索引時—replace into & 與replace 效果
step1: 第一次執(zhí)行sql情況
replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`) select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;
【執(zhí)行完之后,查詢結(jié)果如下:】
step2: 第二次執(zhí)行sql情況
為什么第二次執(zhí)行的時候,顯示update 12行的數(shù)據(jù)且created at 數(shù)據(jù)更新了,而第一次會顯示update 6行???
1.因為在執(zhí)行sql的時候,replace into 其實分了兩個步驟執(zhí)行。第一步是將查詢到數(shù)據(jù)轉(zhuǎn)化為新的數(shù)據(jù)。第二步, 新的數(shù)據(jù)如果表中已經(jīng)有相同的內(nèi)容,則刪除掉。如果沒有相同的內(nèi)容,則直接插入新的數(shù)據(jù)。
2.因如上第一次執(zhí)行的時候,已經(jīng)生成一次新數(shù)據(jù)了,第二次會先刪除,再把最新的數(shù)據(jù)插入進(jìn)去,最終才顯示update 12 行
step3: 第三次執(zhí)行sql情況
# 此時執(zhí)行的是replace replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`) select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;
- 最終查看到的情況與第二次執(zhí)行的sql一樣。
- 當(dāng)新數(shù)據(jù)已經(jīng)存在的時候,replace into 與replace是一樣的
- 后續(xù)刪除所有20220302001,執(zhí)行1次,2次sql,發(fā)現(xiàn)replace into 與 replace 效果都是一樣的
【總結(jié):】當(dāng)有唯一索引限制的時候,如果新增的數(shù)據(jù)會受限于唯一索引,則數(shù)據(jù)只會插入一次,如果已經(jīng)存在則會先刪除再插入。此時replace into 與replace 效果一樣。
3.沒有唯一索引時—replace into 與 replace
我們將strategy_type & user_name 聯(lián)合唯一索引刪除,且刪除20220302001用戶所有數(shù)據(jù)。最終表格結(jié)構(gòu)如下:
CREATE TABLE `xtp_algo_white_list` ( `strategy_type` int DEFAULT NULL, `user_name` varchar(64) COLLATE utf8_bin DEFAULT NULL, `status` int DEFAULT NULL, `destroy_at` datetime DEFAULT NULL, `created_at` datetime DEFAULT CURRENT_TIMESTAMP, `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1).replace函數(shù)的具體情況
step1:執(zhí)行如下replace 對應(yīng)sql:
replace xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`) select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;
step2:再次執(zhí)行replace 對應(yīng)sql:
- 第二次執(zhí)行replace 對應(yīng)sql ,因為沒有唯一索引限制,結(jié)果原始數(shù)據(jù)居然沒變動。又重新生成了新的6條數(shù)據(jù)。
- 如果后續(xù)還執(zhí)行如上的sql,則數(shù)據(jù)還會繼續(xù)增加
2).replace into 函數(shù)的具體情況
執(zhí)行之前,先清理數(shù)據(jù),將所有20220302001的數(shù)據(jù)都刪除掉
step1:執(zhí)行如下replace into 對應(yīng)sql:
replace into xtp_algo_white_list (`strategy_type`, `user_name`, `status`, `destroy_at`) select strategy_type ,20220302001, status, destroy_at from xtp_algo_white_list xawl where xawl.user_name = 209133002266;
step2:再次執(zhí)行replace into 對應(yīng)sql:
最終發(fā)現(xiàn),沒有唯一索引的時候,replace into 與replace 居然一摸一樣的效果,都是繼續(xù)增加數(shù)據(jù)。
通過以上分析,沒看出replace into 與replace 具體有啥區(qū)別????有誰知道呢?
4.replace的用法
- 單獨replace的作用是替換字段中某數(shù)值的顯示效果??梢詳?shù)值中的部分替換、也可以全部替換。
- 如下表格,將user_name的字段,20220302改為"A_20220303"顯示,并且新字段叫做new_name顯示
select *, replace(user_name,20220302,'A_20220303') as "new_name" from xtp_algo_white_list where user_name = 20220302001;
到此這篇關(guān)于MySQL中replace into與replace區(qū)別詳解的文章就介紹到這了,更多相關(guān)MySQL replace into與replace內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql觸發(fā)器(Trigger)簡明總結(jié)和使用實例
這篇文章主要介紹了mysql觸發(fā)器(Trigger)簡明總結(jié)和使用實例,需要的朋友可以參考下2014-04-04