1

In the world I live in, building a solution that throws all the darts at a wall, and hopes some hit the bull's eye is a pretty poor solution methodology.

So, my question arises, when is using INSERT IGNORE in a production system acceptable by convention?

I have a situation that I'd like to write a TRIGGER (see #17353080) that searches for existing data, if it is not found, then it is INSERTed. A suggestion was to use INSERT IGNORE and rely in primary keying of unique fields.

So, I have a predicament:

  1. Do I use INSERT IGNORE and rely on primary keying the fields I'd like to keep unique, failing any INSERTs of repeated
  2. Do I repeatedly query the target table to check if the field values I'd like to keep unique are present, then INSERT if they are not?

Since these questions are too fuzzy to ask in this specific medium, I'd like to understand when it is acceptable by convention to use INSERT IGNORE, and I can make my own judgement as to my specific case.

Thanks,

Matt

Meta:

I take it that since conventions is an existing tag described as:

A generic tag covering any accepted method of doing things, which could include naming, spacing, coding, commenting, etc.

that questions of this type are acceptable.

4

2 回答 2

1

从逻辑上讲,我会说这取决于您在失败时需要做什么。

如果您只是执行批量插入并且不在乎该值是否已经在表中 - 只要它仍然是唯一的 - 那么INSERT IGNORE是明智的。正如该术语所暗示的,您很乐意忽略任何未插入的内容

如果您需要对失败的插入做一些事情,例如建议您的用户,那么执行单独的检查是必要的。

然而,最重要的问题是:“为什么这些值已经在表中?”

于 2013-06-29T16:40:44.063 回答
1

对我来说,这些“功能”的警钟是他们的沉默。在计费世界中;当您收到多条您理解的“唯一”记录时;你想了解他们。在电信领域,交换机会看到 30-100 列的记录;其中5个使记录独一无二;然而,携带不时改变这一点。这里的问题是 WAS 的独特性不再是什么,我们需要了解这一点。

另一个问题是;为什么在生产系统中满足于忽略重复项真的可以吗?至少;我们将有一个审计表记录重复记录以供某人查看,因为它通常表明数据流(传入)中较高的某种类型的问题。

至于性能......对于现代硬件,这真的是一个问题吗?任何进行大规模更新的东西都可能不会与客户端进行大量交互(可能是后台进程);如果它是客户端交互的,您将有一个数据库服务器设计设置来处理这种情况 - 例如表分区、优先级等。

除此之外; INSERT INGORED 的警告使它成为一个可怕的功能:

  • 将 NULL 插入具有 NOT NULL 约束的列中。
  • 向分区表插入一行,但插入的值未映射到分区。

我当然想知道数据是否以 NULL 形式出现在不应该出现的地方......

再一次,这个命令看起来就像“当时的好事”——而且我认为是米老鼠编程。

于 2013-06-29T16:59:22.767 回答