尽快做大量的upsert

时间:2011-01-14 21:48:09

标签: mysql performance upsert

我的应用程序(使用MySQL)正在进行大量后续的upsert。现在我的SQL看起来像这样:

INSERT IGNORE INTO customer (name,customer_number,social_security_number,phone) VALUES ('VICTOR H KINDELL','123','123','123')
INSERT IGNORE INTO customer (name,customer_number,social_security_number,phone) VALUES ('VICTOR H KINDELL','123','123','123')
INSERT IGNORE INTO customer (name,customer_number,social_security_number,phone) VALUES ('VICTOR H KINDELL OR','123','123','123')
INSERT IGNORE INTO customer (name,customer_number,social_security_number,phone) VALUES ('TRACY L WALTER PERSONAL REP FOR','123','123','123')
INSERT IGNORE INTO customer (name,customer_number,social_security_number,phone) VALUES ('TRACY L WALTER PERSONAL REP FOR','123','123','123')

到目前为止,我发现INSERT IGNORE是实现upserts的最快方法。选择一个记录以查看它是否存在然后更新它或插入一个新记录太慢了。即便这样也没有我想要的那么快,因为我需要为每条记录单独做一个声明。有时我会连续约有50,000个这样的陈述。

有没有办法在一个语句中处理所有这些,而不删除任何现有记录?

1 个答案:

答案 0 :(得分:0)

您可以将所有内容放入1个插页中 INSERT IGNORE INTO table_1 (field1, field2) VALUES ('val1', 'val2'), ('val3', 'val4'), etc。如果需要更新程序或插入记录,您可能还需要检查INSERT ... ON DUPLICATE KEY UPDATE