Does a MySQL multi-row insert grab sequential autoincrement IDs?

前端 未结 3 1401
猫巷女王i
猫巷女王i 2020-12-06 02:35

I think this is true, but I haven\'t found anything on the web to confirm it. I can get the first id generated for the autoincrement field using last_insert_id(), but can I

3条回答
  •  一个人的身影
    2020-12-06 02:51

    A multi-row insert is just a bulk-operation that performs the same operation over and over again. When an insert statement is fired, it fires a trigger that generates the ID.

    So if the database engine used, performs all of its work in a sequential way (queue like - first-in first-out) then yes, sequential id's will be one increment higher than the previous id; if the data is inserted in a multi-threaded way where statements are fired in parallel, then no, id's will be given at "random".

    I am not aware of any mysql engine that guarantees this, but then again I haven't made much research in this specific subject.

提交回复
热议问题