简体   繁体   English

MySQL(MyISAM)是否可以在多行插入中填充表孔?

[英]Does MySQL (MyISAM) fill table holes in a multirow insert?

I'm working on a project for which I need to frequently insert ~500 or so records at a remote location. 我正在做一个需要经常在远程位置插入约500条记录的项目。 I will be doing this in a single INSERT to minimize network traffic. 我将在单个INSERT执行此操作,以最大程度地减少网络流量。 I do, however, need to know the exact id field ( AUTO_INCREMENT ) values. 但是,我确实需要知道确切的id字段( AUTO_INCREMENT )值。

My web searches seem to indicate I could probably use the last_insert_id and calculate all the id values from there. 我的网络搜索似乎表明我可以使用last_insert_id并从那里计算所有id值。 However, this wouldn't work if the rows get ids that are all over the place. 但是,如果行获得的ID到处都是,那将是行不通的。

Could anyone please clarify what would or should happen, and if the mathematical solution is safe? 任何人都可以请问将会发生或应该发生什么,以及数学解法是否安全?

A multirow insert is an atomic operation in MySQL (both MyISAM and InnoDB). 多行插入是MySQL(MyISAM和InnoDB)中的原子操作。 Since the table will be locked for writing during this operations, no other rows will be inserted/updated during it's execution. 由于在执行此操作期间将锁定表以进行写入,因此在执行过程中不会插入/更新其他行。

This means IDs will in fact be consecutive (unless auto-increment-increment option is set to something different than 1 这意味着ID实际上将是连续的(除非auto-increment-increment选项设置为不同于1的值)

Auto increment does exactly that, it auto-increments - ie each new row next the numerically next ID. 自动增量正是这样做的,它会自动递增-即,每个新行都在数字上位于下一个ID。 MySQL does not re-use IDs of rows that were deleted. MySQL不会重复使用已删除的行的ID。

Your solution is safe because write operations aquire a table lock, so no other inserts can happen while your operation completes - so you will get n contiguous auto-increment values for n inserted rows. 您的解决方案是安全的,因为写操作AQUIRE表锁,所以当你的操作完成没有其他的刀片会发生-所以你会得到n连续自动增加值n插入的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM