我有下表cl:
id - int(10) primary key
contact - int(10)
list - int(10)
在联系人和列表上具有唯一索引。当我按 100 条记录批量运行以下查询时:
INSERT INTO cl(list, contact) VALUES (?, ?) ON DUPLICATE KEY UPDATE cl.id = cl.id
在高压下它会在大约 20% 的情况下失败并出现以下错误:
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ON DUPLICATE KEY UPDATE cl.id = cl.id' at line 1
80% 的查询运行良好。当我使用相同的参数重新运行失败的查询时,再次有 20% 失败。
为什么有些查询失败了,第二次执行时没有报错?
最佳答案
将查询更改为
INSERT IGNORE INTO cl(list, contact) VALUES (?, ?)
解决了这个问题。虽然不清楚为什么 mysql 抛出 java.sql.SQLSyntaxErrorException 而不是更明确的东西。
关于java - 使用重复键更新在 MariaDB 中批量插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39639524/