MySQL 批量處理

tl;dr 靈活使用 IN() 函數(shù)漱牵,或者 INSERT ... ON DUPLICATE KEY UPDATE ... 搭配 UNIQUE KEY 來(lái)做到批量處理記錄。

數(shù)據(jù)準(zhǔn)備

建立一張測(cè)試用的表

CREATE TABLE `testing` (
CREATE TABLE `testing` (
  `auto_id` int(11) NOT NULL AUTO_INCREMENT,
  `serial` char(36) NOT NULL,
  `type` tinyint(3) DEFAULT NULL,
  `comment` text,
  `status` tinyint(3) DEFAULT '1',
  PRIMARY KEY (`auto_id`),
  UNIQUE KEY `uniq_key` (`serial`,`type`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4;

準(zhǔn)備幾條數(shù)據(jù)

INSERT INTO `testing` VALUES (1,'d0fbd3af-65da-11e9-b766-00163e0bf84f',1,'test 0',1),(2,'d4e21100-65da-11e9-b766-00163e0bf84f',2,'test 002',1),(3,'d80f6f19-65da-11e9-b766-00163e0bf84f',3,'test 003',1),(4,'dafca074-65da-11e9-b766-00163e0bf84f',4,'test 004',1),(5,'de1b15a5-65da-11e9-b766-00163e0bf84f',5,'test 005',1);

select * from testing;

-- 得到結(jié)果:
+---------+--------------------------------------+------+----------+--------+
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 1       | d0fbd3af-65da-11e9-b766-00163e0bf84f | 1    | test 0   | 1      |
| 2       | d4e21100-65da-11e9-b766-00163e0bf84f | 2    | test 002 | 1      |
| 3       | d80f6f19-65da-11e9-b766-00163e0bf84f | 3    | test 003 | 1      |
| 4       | dafca074-65da-11e9-b766-00163e0bf84f | 4    | test 004 | 1      |
| 5       | de1b15a5-65da-11e9-b766-00163e0bf84f | 5    | test 005 | 1      |
+---------+--------------------------------------+------+----------+--------+

批量查詢(xún)符合某幾列值的記錄

以測(cè)試表為例,想要查詢(xún) serial, type 的值是 [('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4)] 的記錄。

SELECT * FROM testing WHERE (serial, type) IN (('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4));

-- 得到結(jié)果:
+---------+--------------------------------------+------+----------+--------+
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 2       | d4e21100-65da-11e9-b766-00163e0bf84f | 2    | test 002 | 1      |
| 3       | d80f6f19-65da-11e9-b766-00163e0bf84f | 3    | test 003 | 1      |
| 4       | dafca074-65da-11e9-b766-00163e0bf84f | 4    | test 004 | 1      |
+---------+--------------------------------------+------+----------+--------+

批量將符合某幾列值的記錄的某個(gè)字段更新為某個(gè)值

以測(cè)試表為例,想要將 serial, type 的值是 [('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4)] 的記錄的字段 status 更新為 0

UPDATE testing SET status = 0 WHERE (serial, type) IN (('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4));

-- 得到結(jié)果:
+---------+--------------------------------------+------+----------+--------+
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 1       | d0fbd3af-65da-11e9-b766-00163e0bf84f | 1    | test 0   | 1      |
| 2       | d4e21100-65da-11e9-b766-00163e0bf84f | 2    | test 002 | 0      |
| 3       | d80f6f19-65da-11e9-b766-00163e0bf84f | 3    | test 003 | 0      |
| 4       | dafca074-65da-11e9-b766-00163e0bf84f | 4    | test 004 | 0      |
| 5       | de1b15a5-65da-11e9-b766-00163e0bf84f | 5    | test 005 | 1      |
+---------+--------------------------------------+------+----------+--------+

不是將 status 更新為 0用僧,而是更新為 type 的值

UPDATE testing SET status = type WHERE (serial, type) IN (('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4));

-- 得到結(jié)果:
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 1       | d0fbd3af-65da-11e9-b766-00163e0bf84f | 1    | test 0   | 1      |
| 2       | d4e21100-65da-11e9-b766-00163e0bf84f | 2    | test 002 | 2      |
| 3       | d80f6f19-65da-11e9-b766-00163e0bf84f | 3    | test 003 | 3      |
| 4       | dafca074-65da-11e9-b766-00163e0bf84f | 4    | test 004 | 4      |
| 5       | de1b15a5-65da-11e9-b766-00163e0bf84f | 5    | test 005 | 1      |
+---------+--------------------------------------+------+----------+--------+

批量插入新數(shù)據(jù)的同時(shí)更新數(shù)據(jù)

INSERT INTO testing (serial, type, comment, status) VALUES ('d80f6f19-65da-11e9-b766-00163e0bf84f',3,'test 113',1),('dafca074-65da-11e9-b766-00163e0bf84f',4,'test 114',1),('de1b15a5-65da-11e9-b766-00163e0bf84f',5,'test 115',1),('d80f6f20-65da-11e9-b766-00163e0bf84f',6,'test 116',6),('dafca075-65da-11e9-b766-00163e0bf84f',7,'test 117',7),('de1b16a6-65da-11e9-b766-00163e0bf84f',7,'test 118',7) ON DUPLICATE KEY UPDATE serial=VALUES(serial), type=VALUES(type), comment=VALUES(comment), status=VALUES(status);

-- 得到結(jié)果:
+---------+--------------------------------------+------+----------+--------+
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 1       | d0fbd3af-65da-11e9-b766-00163e0bf84f | 1    | test 0   | 1      |
| 2       | d4e21100-65da-11e9-b766-00163e0bf84f | 2    | test 002 | 2      |
| 3       | d80f6f19-65da-11e9-b766-00163e0bf84f | 3    | test 113 | 1      |
| 4       | dafca074-65da-11e9-b766-00163e0bf84f | 4    | test 114 | 1      |
| 5       | de1b15a5-65da-11e9-b766-00163e0bf84f | 5    | test 115 | 1      |
| 6       | d80f6f20-65da-11e9-b766-00163e0bf84f | 6    | test 116 | 6      |
| 7       | dafca075-65da-11e9-b766-00163e0bf84f | 7    | test 117 | 7      |
| 8       | de1b16a6-65da-11e9-b766-00163e0bf84f | 7    | test 118 | 7      |
+---------+--------------------------------------+------+----------+--------+

批量刪除符合某幾列值的記錄

以測(cè)試表為例,想要?jiǎng)h除 serial, type 的值為[('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4)] 的記錄

DELETE FROM testing WHERE (serial, type) IN (('d4e21100-65da-11e9-b766-00163e0bf84f',2),('d80f6f19-65da-11e9-b766-00163e0bf84f',3),('dafca074-65da-11e9-b766-00163e0bf84f',4));

-- 得到結(jié)果:
+---------+--------------------------------------+------+----------+--------+
| auto_id | serial                               | type | comment  | status |
+---------+--------------------------------------+------+----------+--------+
| 1       | d0fbd3af-65da-11e9-b766-00163e0bf84f | 1    | test 0   | 1      |
| 5       | de1b15a5-65da-11e9-b766-00163e0bf84f | 5    | test 115 | 1      |
| 6       | d80f6f20-65da-11e9-b766-00163e0bf84f | 6    | test 116 | 6      |
| 7       | dafca075-65da-11e9-b766-00163e0bf84f | 7    | test 117 | 7      |
| 8       | de1b16a6-65da-11e9-b766-00163e0bf84f | 7    | test 118 | 7      |
+---------+--------------------------------------+------+----------+--------+

相關(guān)問(wèn)題:UNIQUE KEY 的設(shè)置赞咙,(serial,type)(type,serial) 的性能差別有多大责循?

理論上而言,(type, serial) 的性能要比 (serial, type) 的好攀操。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末院仿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子速和,更是在濱河造成了極大的恐慌歹垫,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件颠放,死亡現(xiàn)場(chǎng)離奇詭異排惨,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)碰凶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)暮芭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人欲低,你說(shuō)我怎么就攤上這事辕宏。” “怎么了伸头?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵匾效,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我恤磷,道長(zhǎng)面哼,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任扫步,我火速辦了婚禮魔策,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘河胎。我一直安慰自己闯袒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著政敢,像睡著了一般其徙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上喷户,一...
    開(kāi)封第一講書(shū)人閱讀 52,255評(píng)論 1 308
  • 那天唾那,我揣著相機(jī)與錄音,去河邊找鬼褪尝。 笑死闹获,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的河哑。 我是一名探鬼主播避诽,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼璃谨!你這毒婦竟也來(lái)了沙庐?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤睬罗,失蹤者是張志新(化名)和其女友劉穎轨功,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體容达,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年垂券,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了花盐。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡菇爪,死狀恐怖算芯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情凳宙,我是刑警寧澤熙揍,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站氏涩,受9級(jí)特大地震影響届囚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜是尖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一意系、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧饺汹,春花似錦蛔添、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)夸溶。三九已至,卻和暖如春凶硅,著一層夾襖步出監(jiān)牢的瞬間缝裁,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工咏尝, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留压语,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓编检,卻偏偏與公主長(zhǎng)得像胎食,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子允懂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容