-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
batch bulk insert gaps #263
Comments
Actually I'm not sure recreate the insert ids is the way to go. |
@rusher Hi, I am currently investigating an issue in our DB where we have gaps in inserted auto increment IDs using the batch function. We have managed to recreate the the bug twice, I have attached an image which shows we have run two separate batch queries with the same 5 rows of data, on both occasions the same ID gaps occurred at the same point. I just want to check whether this is a bug in the batch function itself, or whether I need to investigate this further inside our DB / code config. |
@rusher forgot to add that in the table in the image above, we currently have about 400k records but the highest id is currently over 500k, meaning about a 20% difference in count and IDs |
@Gabilabrebi did you manage to find a fix / resolution for this? |
@crushton-qg Yeah i just stopped using batch for (let i = 0; i < order.content.length; i++) { db.query( Something like this basically achieve the same thing for me, but without any id issues. It's still one query and performance are pretty much the same as far as i tested. |
@Gabilabrebi Sorry to be a pain, can you provide a full code sample please? I have tried to replicate what you have but I cannot get it to work. I am struggling to follow your sample where you are providing multiple (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0) in your query string. I have an array of arrays, each inner array containing the row data, i have changed table names, columns to generic name/data I have tried adapting my query to:
but I am getting a syntax error every time: |
@crushton-qg I guess you just need to change contentParameters.push(row); |
@Gabilabrebi that was it, brilliant thanks for the help! |
@rusher Does using bulk = false as today ensure there is not gap between ids? Is there any documentation on this option and what it does exactly? I'm trying to use .batch again but still need contiguous ids Regards |
This is a little more complex than that, i wouldn't rely on id's without gap, servers ensure uniqueness, not strict "sequencing". Server have AUTO_INCREMENT defaulting to 1 that indicate the increment between each IDs. The bulk option correspond to real batch command. I'm not completly sure about how this is implemented for bulk, server guys would be more able to help with the details, but what i suppose is that in order to deals with connections inserting to the same table, server can reserve a block of some ids when using bulk and that might result in gap in ids. So, yes, if using only one server, no galera, without bulk, you will normally not have any gap, but i would clearly nor rely on that. |
@rusher Thank you for your answer. In my case I've table A, B, and C. It's currently 3 queries, one for A, for B, and one for C, but yes i rely on contiguous id. If i shouldn't rely on contiguous ids, do I have to make tones of single query in loops to ensure to proper relation? Or is there any better alternative? |
There is various solutions, but all of them rely on knowing the real insert id. for batch, an option
|
OH i didn't know about this fullResult: true option. This is exactly what i was looking for. With this, no more need for contiguous ids. I just add this option when i want ids, and can still benefit full speed of batch when i don't need them. Thanks a ton! |
When using batch to bulk insert, gaps in AUTO_INCREMENT IDs are created when reaching 50+ inserts even with innodb_autoinc_lock_mode set to 1 (consecutive)
The mySQL, documentation says :
So I'm not sure that's intended. Due to the fact that insertId, only return one ID, i think it's very important to not have gaps in IDs so we can deduce inserted ones.
The text was updated successfully, but these errors were encountered: