In MySql 5.6 I have a query similar to the following:
INSERT IGNORE INTO TABLE_1 (field_a,field_b)
SELECT field_a,field_b
FROM TABLE_2
WHERE
...
ORDER BY field_a
LIMIT 0,10
TABLE_1 is a temporary table that is used to store some tuples and then emptied. It doesn't have a PRIMARY KEY.
Since the process fills the table in multiple times, it may happen that it may contain tuples found throught the SELECT. Considering this, I thought about increasing the limit, so that I would be able to insert all the needed data.
Here is an example of the content of TABLE_1 and of the result of the SELECT query.
TABLE_1:
+---------+---------+
| field_a | field_b |
+---------+---------+
| foo | 1 |
| foo | 2 |
| foo | 3 |
| foo | 4 |
| bar | 2 |
| bar | 3 |
| bar | 4 |
| bar | 5 |
+---------+---------+
SELECT result (ignoring LIMIT):
+---------+---------+
| field_a | field_b |
+---------+---------+
| foo | 4 |
| foo | 5 |
| foo | 6 |
| foo | 7 |
| foo | 8 |
| foo | 9 |
| foo | 10 |
| foo | 11 |
| foo | 12 |
| foo | 13 |
| bar | 5 |
| bar | 6 |
| bar | 7 |
| bar | 8 |
+---------+---------+
So, considering that there are two duplicate tuples, (foo,4) and (bar,5), I expected 8 elements to be inserted... while, I actually found that the query inserted 10 elements, ignoring the two duplicates.
The point is that I can't find the reason of this behavior in the docs. I found here that LIMIT works in INSERT...SELECT only if there is an ORDER BY, which I have. But I was not able to find a precise description of this behavior, having together INSERT IGNORE, INSERT...SELECT, ORDER BY and LIMIT.
Can somebody help me to explain this unexpected behavior?