Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

mysql where in的最大条数是多少?

背景

mysql表需要批量更新60w数据,表内数据大概2000w

sql

UPDATE table1 SET field_1 = 5 WHERE id = 19823049;

两种方式

第一种 每行一条

  1. UPDATE table1 SET field_1 = 5 WHERE id = 19823049;
  2. UPDATE table1 SET field_1 = 5 WHERE id = 19823043;

第二种 where in

UPDATE table1 SET field_1 = 5 WHERE id in (19823049,xxx,xxx,1324134....);

## 问题

  1. mysql where in的最大数量的是多少?
  2. 两种场景哪个更优,或者其他更好的解决办法?
  3. 这里where in主键id和in其他的普通字段(特指带索引但不是主键的字段),底层处理上有差别吗?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. 根据文档operator_in,取决于参数max_allowed_packet有多大;
  2. 第二种占优,不过id过多的话,最好再分成多条update,每条限制一下个数最多为1000个;
  3. 肯定有差别,最好in的字段是有索引的。

希望能帮助到你。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...