将本地数据库与外部API同步
我有一个包含大约 500 000 行的表。每天一次,我会尝试将此表与外部 API 同步。大多数情况下,自上次更新以来几乎没有进行任何更改。我的问题基本上是我应该如何构建 MySQL 查询以获得最佳性能?我曾考虑过使用 insertignore,但感觉这不是最好的方法,因为只会插入几行,并且 MySQL 必须循环遍历表中的所有行。我还考虑过使用 LOAD_DATA_INFILE 将所有行插入临时表中,然后选择原始表中尚未存在的行,然后删除临时表。也许其他人有更好的建议?
先感谢您!
I have a table containing about 500 000 rows. Once a day, I will try to synchronize this table with an external API. Most of the times, there are few- or no changes made since last update. My question is basically how should I construct my MySQL query for best performance? I have thought about using insert ignore, but it doesn't feel like the best way to go since only a few rows will be inserted and MySQL must loop through all rows in the table. I have also thought about using LOAD_DATA_INFILE to insert all rows in a temporary table and then select the rows not already in my original table, and then remove the temporary table. Maybe someone else has a better suggestion?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通常使用临时表和 LOAD DATA INFILE 批量加载器。批量加载器比尝试使用动态创建的查询插入记录要高效得多。
如果您使用与 API 中的键相关的适当的唯一键为永久表建立索引,那么您应该会发现
INSERT
和UPDATE
语句运行得非常快。我使用的INSERT
查询类型的示例如下:在本示例中,我对
keywords
表执行OUTER JOIN
来检查如果它已经存在。仅临时表中与主表中不匹配的新行(keywords
表中的api_keyword_id
为NULL
)插入。另请注意,在此示例中,我需要同时使用广告组 ID 和关键字 ID 来唯一标识关键字,因为当同一关键字/匹配类型组合存在于多个广告组中时,AdWords API 会为其提供相同的 ID。
I usually use a temporary table and the
LOAD DATA INFILE
bulk loader. The bulk loader is much more efficient that trying to insert records using a dynamically created query.If you index your permanent tables with appropriate unique keys that relate to the keys in the API then you should find the the
INSERT
andUPDATE
statements work pretty fast. An example of the type ofINSERT
query I use is as follows:In this example, I perform an
OUTER JOIN
on thekeywords
table to check if it already exists. Only new rows in the temporary table where there isn't a match in the main table (theapi_keyword_id
in thekeywords
table isNULL
) are inserted.Also note that in this example I need to use both the ad group id AND the keyword id to uniquely identify the keyword because the AdWords API gives the same keyword/match type combination the same id when it exists in more than one ad group.