[MDEV-22760] Bulk INSERT...ON DUPLICATE KEY UPDATE updates only a fraction of rows Created: 2020-05-30 Updated: 2023-07-12 |
|
| Status: | Confirmed |
| Project: | MariaDB Server |
| Component/s: | Server |
| Affects Version/s: | 10.4.13, 10.4, 10.5 |
| Fix Version/s: | 10.4, 10.5 |
| Type: | Bug | Priority: | Major |
| Reporter: | Serge Rogatch | Assignee: | Sergei Golubchik |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Windows 10 64-bit |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
Consider the following table:
When running the following program:
I get for example the following output: It indicates the following: I cannot reproduce the problem from HeidiSQL 11.0.0.5989. I could only reproduce it when using Connector/C versions 3.1.7 and 3.1.8 (I didn't try the older versions). The problem appears for both a direct and prepared statement. MariaDB server versions tried: 10.4.13 and 10.4.12. |
| Comments |
| Comment by Georg Richter [ 2020-05-31 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for your bug report. Can you please also report the numbers HeidiSQL returns and in which sql mode HeidiSQL is running (select @@sql_mode) ? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Serge Rogatch [ 2020-05-31 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for starting to look into this quickly. I'm going to do the above experiments right now and will post here afterward. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Serge Rogatch [ 2020-05-31 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
When I just dump the queries into a SQL file and then copy-paste the file into HeidiSQL and run the 6 queries, I get the following output: The updated program and the script are attached. Below is the output for select @@sql_mode in HeidiSQL: When I run the queries one by one, I get: So apparently HeidiSQL behaves the same way when the queries are run one by one. Sorry for misaddressing the bug report. Now I tend to think that the error is in the core DB server. Is there a way to migrate the issue to the proper project? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Georg Richter [ 2020-05-31 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
changed to server bug (MDEV) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2020-07-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks for the report and test case. The culprit here is the long unique index on blob (text) column.
(or whatever reasonable length suits your actual data). The actual problem here appears to be not as much that existing values don't get updated, but mainly that new values don't get inserted. It is not easy to infer from the behavior of UPDATE. Let's take the 1st and 2nd INSERT/UPDATE from your SQL an insert them into separate identical tables. demo_upsert1 below contains results of the 1st INSERT, and demo_upsert2 – results of the 2nd INSERT.
So, the 1st INSERT is supposed to insert 10130 rows, which it does. For new 4197 rows the value of LastListID would be inserted as 10, for 5817 duplicates it will be updated from NULL to 10, but there would also be 10130-5817=4313 rows left intact with LastListID NULL. Thus, just from the behavior of UPDATE you can't say whether it updates a fraction of values because the previous upsert didn't update existing ones or didn't insert new ones. Here is a simplified test case where we can see the actual values. The structures and values are taken from the original one, I just reduced the number of rows.
The first SELECT returns the expected two rows which were just inserted:
The second INSERT is supposed to update one row (qlf) and insert two new ones, knx and bdu.
Stats are already wrong. There is only 1 duplicate, not 2, and the total number of affected rows should be 4 (because upserted rows are counted twice), but not 5.
The duplicate row did get updated, but out of 2 new ones only one got inserted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Serge Rogatch [ 2020-07-08 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for looking into our issue. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by steven chang [ 2020-10-15 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Guys, To my understanding, INSERT...ON DUPLICATE KEY UPDATE can only be applied tables with only one unique key (either pk or uk) . Your case have 2 . |