如何以更好的优化方式使用 INSERT INTO 跳过重复项? [英] How to skip duplicates using INSERT INTO in a better optimized way?

查看:24
本文介绍了如何以更好的优化方式使用 INSERT INTO 跳过重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为实验,我想构建一种任何用户都可以推荐新词的字典.

为了避免重复,我曾经做一个查询SELECT来搜索那个词,如果大小为零,那么我做INSERT INTO.

我觉得这种方法只有在您需要警告用户 br lese 时才有效,但在我的情况下,我想要更快、自动化和静音的方法.

单词的第一个条目(用户第一次建议该单词)将是页面单词的 ID,因此我不想使用 REPLACE.

我想知道使用 INSERT IGNORE 是否可以解决问题?

解决方案

INSERT IGNORE 将在这里为您解决问题.您只需要确保在不想重复的列上定义了 UNIQUE 索引.

另一个选项是 INSERT INTO ... ON DUPLICATE KEY UPDATE 不会再次插入值,但允许您更新该行中的其他列.例如计数器或时间戳.

I'd like to build as an experiment a sort of dictionary where any user can suggest new words.

In order to avoid duplicates, I used to do a query SELECT that search for that word and if size is zero then I do the INSERT INTO.

I feel this method works well only if you need to warn the user br lese, but in my case I want something faster and automated and silent.

The very first entry of the word (the very first time a user suggests that word) is going to be the ID of the page word so I don't want to use REPLACE.

I was wondering whether using INSERT IGNORE can be the solution?

解决方案

INSERT IGNORE will do the trick for you here. You just need to make sure you have a UNIQUE index defined on the column you don't want duplicated.

Another options is INSERT INTO ... ON DUPLICATE KEY UPDATE which won't insert the value again, but will allow you update other columns in that row. A counter or timestamp for example.

这篇关于如何以更好的优化方式使用 INSERT INTO 跳过重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆