标签:8D% E8% 重复 BC% E5% MySql E6% 插入 E4%
向数据库的插入值时,需要判断插入是否重复,然后插入。
这种操作怎么提高效率
下面说说一些解决方案
1、insert ignore into
当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回。所以使用ignore请确保语句本身没有问题,否则也会被忽略掉。例如:
INSERT IGNORE INTO USER ( NAME )
VALUES ( '张三' );
这种方法很简单,但是有一种可能插入不是因为重复数据报错,而是因为其他原因报错的,也同样被忽略了~
2、on duplicate key update
当primary 或者 unique重复时,则执行update语句;
当update后为无用语句,如:id=id,错误不会被忽略。
INSERT INTO USER ( NAME )
VALUES ( '张三' )
ON DUPLICATE KEY UPDATE id = id;
这种方法有个前提条件,就是需要约束条件,即,主键或者唯一约束。
3、insert ... select ... where not exist
根据select的条件判断是否插入
INSERT INTO USER ( NAME )
SELECT '张三' FROM DUAL
WHERE NOT EXISTS (SELECT id FROM USER WHERE id = 1)
这种方法其实就是使用mysql的一个临时表的方式,但是里面使用子查询,效率会有影响,能不使用尽量避免。
4、replace into
如果存在 primary 或者 unique 相同的记录,则先删除再插入新记录。
REPLACE INTO USER SELECT 1, '张三' FROM USER
这种方法就是不管原来有没有相同的记录,都会先删除再插入。
%23%23%20MySql%E5%A6%82%E4%BD%95%E4%B8%8D%E6%8F%92%E5%85%A5%E9%87%8D%E5%A4%8D%E6%95%B0%E6%8D%AE%0A%E5%90%91%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%8F%92%E5%85%A5%E5%80%BC%E6%97%B6%EF%BC%8C%E9%9C%80%E8%A6%81%E5%88%A4%E6%96%AD%E6%8F%92%E5%85%A5%E6%98%AF%E5%90%A6%E9%87%8D%E5%A4%8D%EF%BC%8C%E7%84%B6%E5%90%8E%E6%8F%92%E5%85%A5%E3%80%82%0A%E8%BF%99%E7%A7%8D%E6%93%8D%E4%BD%9C%E6%80%8E%E4%B9%88%E6%8F%90%E9%AB%98%E6%95%88%E7%8E%87%0A%0A%E4%B8%8B%E9%9D%A2%E8%AF%B4%E8%AF%B4%E4%B8%80%E4%BA%9B%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%0A%0A%23%23%23%201%E3%80%81insert%20%20ignore%20into%0A%E5%BD%93%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E6%97%B6%EF%BC%8C%E5%A6%82%E5%87%BA%E7%8E%B0%E9%94%99%E8%AF%AF%E6%97%B6%EF%BC%8C%E5%A6%82%E9%87%8D%E5%A4%8D%E6%95%B0%E6%8D%AE%EF%BC%8C%E5%B0%86%E4%B8%8D%E8%BF%94%E5%9B%9E%E9%94%99%E8%AF%AF%EF%BC%8C%E5%8F%AA%E4%BB%A5%E8%AD%A6%E5%91%8A%E5%BD%A2%E5%BC%8F%E8%BF%94%E5%9B%9E%E3%80%82%E6%89%80%E4%BB%A5%E4%BD%BF%E7%94%A8ignore%E8%AF%B7%E7%A1%AE%E4%BF%9D%E8%AF%AD%E5%8F%A5%E6%9C%AC%E8%BA%AB%E6%B2%A1%E6%9C%89%E9%97%AE%E9%A2%98%EF%BC%8C%E5%90%A6%E5%88%99%E4%B9%9F%E4%BC%9A%E8%A2%AB%E5%BF%BD%E7%95%A5%E6%8E%89%E3%80%82%E4%BE%8B%E5%A6%82%EF%BC%9A%0A%60%60%60sql%0AINSERT%20IGNORE%20INTO%20USER%20(%20NAME%20)%0AVALUES%20(%20'%E5%BC%A0%E4%B8%89'%20)%3B%0A%60%60%60%0A%E8%BF%99%E7%A7%8D%E6%96%B9%E6%B3%95%E5%BE%88%E7%AE%80%E5%8D%95%EF%BC%8C%E4%BD%86%E6%98%AF%E6%9C%89%E4%B8%80%E7%A7%8D%E5%8F%AF%E8%83%BD%E6%8F%92%E5%85%A5%E4%B8%8D%E6%98%AF%E5%9B%A0%E4%B8%BA%E9%87%8D%E5%A4%8D%E6%95%B0%E6%8D%AE%E6%8A%A5%E9%94%99%EF%BC%8C%E8%80%8C%E6%98%AF%E5%9B%A0%E4%B8%BA%E5%85%B6%E4%BB%96%E5%8E%9F%E5%9B%A0%E6%8A%A5%E9%94%99%E7%9A%84%EF%BC%8C%E4%B9%9F%E5%90%8C%E6%A0%B7%E8%A2%AB%E5%BF%BD%E7%95%A5%E4%BA%86~%0A%0A%23%23%23%202%E3%80%81on%20duplicate%20key%20update%0A%E5%BD%93primary%20%E6%88%96%E8%80%85%20unique%E9%87%8D%E5%A4%8D%E6%97%B6%EF%BC%8C%E5%88%99%E6%89%A7%E8%A1%8Cupdate%E8%AF%AD%E5%8F%A5%EF%BC%9B%0A%E5%BD%93update%E5%90%8E%E4%B8%BA%E6%97%A0%E7%94%A8%E8%AF%AD%E5%8F%A5%EF%BC%8C%E5%A6%82%EF%BC%9Aid%3Did%EF%BC%8C%E9%94%99%E8%AF%AF%E4%B8%8D%E4%BC%9A%E8%A2%AB%E5%BF%BD%E7%95%A5%E3%80%82%0A%60%60%60sql%0AINSERT%20INTO%20USER%20(%20NAME%20)%0AVALUES%20(%20'%E5%BC%A0%E4%B8%89'%20)%20%0AON%20DUPLICATE%20KEY%20UPDATE%20id%20%3D%20id%3B%0A%60%60%60%0A%E8%BF%99%E7%A7%8D%E6%96%B9%E6%B3%95%E6%9C%89%E4%B8%AA%E5%89%8D%E6%8F%90%E6%9D%A1%E4%BB%B6%EF%BC%8C%E5%B0%B1%E6%98%AF%E9%9C%80%E8%A6%81%E7%BA%A6%E6%9D%9F%E6%9D%A1%E4%BB%B6%EF%BC%8C%E5%8D%B3%EF%BC%8C%E4%B8%BB%E9%94%AE%E6%88%96%E8%80%85%E5%94%AF%E4%B8%80%E7%BA%A6%E6%9D%9F%E3%80%82%0A%0A%23%23%23%203%E3%80%81insert%20...%20select%20...%20where%20not%20exist%0A%E6%A0%B9%E6%8D%AEselect%E7%9A%84%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%8F%92%E5%85%A5%0A%60%60%60sql%0AINSERT%20INTO%20USER%20(%20NAME%20)%20%0ASELECT%20'%E5%BC%A0%E4%B8%89'%20FROM%20DUAL%20%0AWHERE%20NOT%20EXISTS%20(SELECT%20id%20FROM%20USER%20WHERE%20id%20%3D%201)%0A%60%60%60%0A%E8%BF%99%E7%A7%8D%E6%96%B9%E6%B3%95%E5%85%B6%E5%AE%9E%E5%B0%B1%E6%98%AF%E4%BD%BF%E7%94%A8mysql%E7%9A%84%E4%B8%80%E4%B8%AA%E4%B8%B4%E6%97%B6%E8%A1%A8%E7%9A%84%E6%96%B9%E5%BC%8F%EF%BC%8C%E4%BD%86%E6%98%AF%E9%87%8C%E9%9D%A2%E4%BD%BF%E7%94%A8%E5%AD%90%E6%9F%A5%E8%AF%A2%EF%BC%8C%E6%95%88%E7%8E%87%E4%BC%9A%E6%9C%89%E5%BD%B1%E5%93%8D%EF%BC%8C%E8%83%BD%E4%B8%8D%E4%BD%BF%E7%94%A8%E5%B0%BD%E9%87%8F%E9%81%BF%E5%85%8D%E3%80%82%0A%0A%23%23%23%204%E3%80%81replace%20into%0A%E5%A6%82%E6%9E%9C%E5%AD%98%E5%9C%A8%20primary%20%E6%88%96%E8%80%85%20unique%20%E7%9B%B8%E5%90%8C%E7%9A%84%E8%AE%B0%E5%BD%95%EF%BC%8C%E5%88%99%E5%85%88%E5%88%A0%E9%99%A4%E5%86%8D%E6%8F%92%E5%85%A5%E6%96%B0%E8%AE%B0%E5%BD%95%E3%80%82%0A%60%60%60sql%0AREPLACE%20INTO%20USER%20SELECT%201%2C%20'%E5%BC%A0%E4%B8%89'%20FROM%20USER%0A%60%60%60%0A%E8%BF%99%E7%A7%8D%E6%96%B9%E6%B3%95%E5%B0%B1%E6%98%AF%E4%B8%8D%E7%AE%A1%E5%8E%9F%E6%9D%A5%E6%9C%89%E6%B2%A1%E6%9C%89%E7%9B%B8%E5%90%8C%E7%9A%84%E8%AE%B0%E5%BD%95%EF%BC%8C%E9%83%BD%E4%BC%9A%E5%85%88%E5%88%A0%E9%99%A4%E5%86%8D%E6%8F%92%E5%85%A5%E3%80%82
标签:8D%,
E8%,
重复,
BC%,
E5%,
MySql,
E6%,
插入,
E4%
From: https://www.cnblogs.com/lhns/p/16860804.html