MySQL中replace into與replace區(qū)別詳解
本篇為拋磚引玉篇,之前沒關(guān)注過replace into 與replace 的區(qū)別。經(jīng)過多個(gè)場(chǎng)景測(cè)試,居然沒找到在插入數(shù)據(jù)的時(shí)候兩者有什么本質(zhì)的區(qū)別?如果了解詳情的伙伴們,請(qǐng)告知留言告知一二,不勝感激?。?!
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` 這兩個(gè)是聯(lián)合唯一索引,多關(guān)注后續(xù)需要用到?。?!
【需求:】
- 根據(jù)表格里面, 209133002266賬戶的數(shù)據(jù),重新插入一個(gè)用戶20220302001, 使得新生成的數(shù)據(jù)中strategy_type & status & destroy_at 字段與209133002266用戶的一致。
- 使用update 一條一條更新也行,但是比較慢。
- 使用replace into 效果會(huì)高很多,但是深入研究發(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 后面跟表格+需要插入的所有字段名(自動(dòng)遞增字段不用寫) # select 后面選擇的字段,如果根據(jù)查詢結(jié)果取值,則寫字段名;如果是寫死的,則直接寫具體值即可 # 可以理解為,第一部分是插入表格的結(jié)構(gòu),第二部分是你查詢的數(shù)據(jù)結(jié)果
2.有唯一索引時(shí)—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í)行的時(shí)候,顯示update 12行的數(shù)據(jù)且created at 數(shù)據(jù)更新了,而第一次會(huì)顯示update 6行???
1.因?yàn)樵趫?zhí)行sql的時(shí)候,replace into 其實(shí)分了兩個(gè)步驟執(zhí)行。第一步是將查詢到數(shù)據(jù)轉(zhuǎn)化為新的數(shù)據(jù)。第二步, 新的數(shù)據(jù)如果表中已經(jīng)有相同的內(nèi)容,則刪除掉。如果沒有相同的內(nèi)容,則直接插入新的數(shù)據(jù)。
2.因如上第一次執(zhí)行的時(shí)候,已經(jīng)生成一次新數(shù)據(jù)了,第二次會(huì)先刪除,再把最新的數(shù)據(jù)插入進(jìn)去,最終才顯示update 12 行
step3: 第三次執(zhí)行sql情況
# 此時(shí)執(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)存在的時(shí)候,replace into 與replace是一樣的
- 后續(xù)刪除所有20220302001,執(zhí)行1次,2次sql,發(fā)現(xiàn)replace into 與 replace 效果都是一樣的
【總結(jié):】當(dāng)有唯一索引限制的時(shí)候,如果新增的數(shù)據(jù)會(huì)受限于唯一索引,則數(shù)據(jù)只會(huì)插入一次,如果已經(jīng)存在則會(huì)先刪除再插入。此時(shí)replace into 與replace 效果一樣。
3.沒有唯一索引時(shí)—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 對(duì)應(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 對(duì)應(yīng)sql:
- 第二次執(zhí)行replace 對(duì)應(yīng)sql ,因?yàn)闆]有唯一索引限制,結(jié)果原始數(shù)據(jù)居然沒變動(dòng)。又重新生成了新的6條數(shù)據(jù)。
- 如果后續(xù)還執(zhí)行如上的sql,則數(shù)據(jù)還會(huì)繼續(xù)增加
2).replace into 函數(shù)的具體情況
執(zhí)行之前,先清理數(shù)據(jù),將所有20220302001的數(shù)據(jù)都刪除掉
step1:執(zhí)行如下replace into 對(duì)應(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 對(duì)應(yīng)sql:
最終發(fā)現(xiàn),沒有唯一索引的時(shí)候,replace into 與replace 居然一摸一樣的效果,都是繼續(xù)增加數(shù)據(jù)。
通過以上分析,沒看出replace into 與replace 具體有啥區(qū)別????有誰知道呢?
4.replace的用法
- 單獨(dú)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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入探究MySQL事務(wù)實(shí)現(xiàn)原理
數(shù)據(jù)庫事務(wù)是指一組數(shù)據(jù)庫操作,這些操作必須被視為一個(gè)不可分割的單元,要么全部執(zhí)行成功,要么全部失敗回滾,本文詳細(xì)的給大家介紹了MySQL事務(wù)的實(shí)現(xiàn)原理,對(duì)我們學(xué)習(xí)MySQL有一定的幫助,感興趣的同學(xué)可以跟著小編一起來探究2023-06-06MySQL的一些功能實(shí)用的Linux shell腳本分享
這篇文章主要介紹了Linux系統(tǒng)下MySQL的一些實(shí)用功能的shell腳本分享,包括啟動(dòng)Memcached、binlog自動(dòng)清理和修復(fù)主從同步這樣三個(gè)簡(jiǎn)單的實(shí)例,需要的朋友可以參考下2015-12-12mysql觸發(fā)器(Trigger)簡(jiǎn)明總結(jié)和使用實(shí)例
這篇文章主要介紹了mysql觸發(fā)器(Trigger)簡(jiǎn)明總結(jié)和使用實(shí)例,需要的朋友可以參考下2014-04-04