简体   繁体   English

在MySql中INSERT IGNORE ... SELECT ... ORDER BY ... LIMIT ...的确切行为是什么?

[英]What is the exact behaviour of an INSERT IGNORE… SELECT… ORDER BY… LIMIT… in MySql?

In MySql 5.6 I have a query similar to the following: 在MySql 5.6中,我有一个类似于以下内容的查询:

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. TABLE_1是一个临时表,用于存储一些元组然后清空。 It doesn't have a PRIMARY KEY. 它没有PRIMARY KEY。

Since the process fills the table in multiple times, it may happen that it may contain tuples found throught the SELECT . 由于进程多次填充表,可能会发生它可能包含通过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的内容和SELECT查询结果的示例。

TABLE_1 : 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 ): SELECT结果(忽略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. 因此,考虑到有两个重复的元组, (foo,4)(bar,5) ,我期望插入8个元素...而实际上,我发现查询插入了10个元素,忽略了两个重复元素。

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. 我在这里发现,只有存在ORDER BYLIMIT才能在INSERT...SELECT工作。 But I was not able to find a precise description of this behavior, having together INSERT IGNORE , INSERT...SELECT , ORDER BY and LIMIT . 但我无法找到这种行为的精确描述,一起INSERT IGNOREINSERT...SELECTORDER BYLIMIT

Can somebody help me to explain this unexpected behavior? 有人可以帮我解释一下这种意想不到的行为吗?

Manual for INSERT IGNORE : INSERT IGNORE手册

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. 如果使用IGNORE关键字,则会忽略执行INSERT语句时发生的错误

It is likely that there is no unique constraint on ( field_a, field_b) on TABLE_1 . TABLE_1上的( field_a, field_b)可能没有唯一约束。 No "duplicate" error can occur without such a constraint. 没有这样的约束,不会发生“重复”错误。 TABLE_2 's primary key (which is a special case of unique constraint) has no effect in your query. TABLE_2的主键(这是唯一约束的特殊情况)对查询没有影响。

Also, LIMIT n without ORDER BY "works", it just returns the first n random rows that the query happens to generate. 此外, LIMIT n没有ORDER BY “工作”,它只返回查询恰好生成的前n随机行。

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

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