是否可以对 MySQL 进行混合覆盖/追加批量写入?
我正在为我的客户设置一个上传器(使用 php),他们可以在他们的计算机上选择 CSV(采用预先确定的格式)进行上传。 CSV 可能有 4000-5000 行。 Php 将通过读取 CSV 的每一行并将其直接插入到 DB 表中来处理文件。那部分很容易。
然而,理想情况下,在将此数据附加到数据库表之前,我想查看其中的 3 列(A、B 和 C),并检查表中是否已经有这 3 个字段的匹配组合 AND IF所以我宁愿更新该行而不是追加。如果我没有这 3 列的匹配组合,我想继续插入该行,将数据附加到表中。
我的第一个想法是,我可以将 A、B 和 C 列设置为表中的唯一索引,然后插入每一行,以某种方式检测“失败”插入(由于我的唯一索引的限制),然后进行更新。似乎这种方法比必须为每一行进行单独的 SELECT 查询来查看表中是否已有匹配的组合更有效。
第三种方法可能是简单地追加所有内容,不使用 MySQL 唯一索引,然后仅在客户端稍后查询该表时获取最新的唯一组合。不过,我试图避免该表中存在大量无用数据。
关于最佳实践或巧妙方法的想法?
I am setting up an uploader (using php) for my client where they can select a CSV (in a pre-determined format) on their machine to upload. The CSV will likely have 4000-5000 rows. Php will process the file by reading each line of the CSV and inserting it directly into the DB table. That part is easy.
However, ideally before appending this data to the database table, I'd like to review 3 of the columns (A, B, and C) and check to see if I already have a matching combo of those 3 fields in the table AND IF SO I would rather UPDATE that row rather than appending. If I DO NOT have a matching combo of those 3 columns I want to go ahead and INSERT the row, appending the data to the table.
My first thought is that I could make columns A, B, and C a unique index in my table and then just INSERT every row, detect a 'failed' INSERT (due to the restriction of my unique index) somehow and then make the update. Seems that this method could be more efficient than having to make a separate SELECT query for each row just to see if I have a matching combo already in my table.
A third approach may be to simply append EVERYTHING, using no MySQL unique index and then only grab the latest unique combo when the client later queries that table. However I am trying to avoid having a ton of useless data in that table.
Thoughts on best practices or clever approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将 3 列设置为唯一 id,则可以使用 ON DUPLICATE KEY 执行 INSERT。
您可以在 MySQL 手册中阅读有关此方便技术的更多信息。
If you make the 3 columns the unique id, you can do an INSERT with ON DUPLICATE KEY.
You can read more about this handy technique here in the MySQL manual.
如果在( A、B、C )列上添加唯一索引,则可以使用 REPLACE 在一条语句中执行此操作:
If you add a unique index on the ( A, B, C ) columns, then you can use REPLACE to do this in one statement: