将本地数据库与外部API同步

发布于 2024-11-02 19:47:17 字数 257 浏览 1 评论 0原文

我有一个包含大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-09 19:47:17

我通常使用临时表和 LOAD DATA INFILE 批量加载器。批量加载器比尝试使用动态创建的查询插入记录要高效得多。

如果您使用与 API 中的键相关的适当的唯一键为永久表建立索引,那么您应该会发现 INSERTUPDATE 语句运行得非常快。我使用的 INSERT 查询类型的示例如下:

INSERT INTO keywords(api_adgroup_id, api_keyword_id, keyword_text, match_type, status) 
SELECT a.api_id, a.keyword_text, a.match_type, a.status
FROM tmp_keywords a LEFT JOIN keywords b ON a.api_adgroup_id = b.api_adgroup_id AND a.api_keyword_id = b.api_keyword_id
WHERE b.api_keyword_id IS NULL

在本示例中,我对 keywords 表执行 OUTER JOIN 来检查如果它已经存在。仅临时表中与主表中不匹配的新行(keywords 表中的 api_keyword_idNULL)插入。

另请注意,在此示例中,我需要同时使用广告组 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 and UPDATE statements work pretty fast. An example of the type of INSERT query I use is as follows:

INSERT INTO keywords(api_adgroup_id, api_keyword_id, keyword_text, match_type, status) 
SELECT a.api_id, a.keyword_text, a.match_type, a.status
FROM tmp_keywords a LEFT JOIN keywords b ON a.api_adgroup_id = b.api_adgroup_id AND a.api_keyword_id = b.api_keyword_id
WHERE b.api_keyword_id IS NULL

In this example, I perform an OUTER JOIN on the keywords table to check if it already exists. Only new rows in the temporary table where there isn't a match in the main table (the api_keyword_id in the keywords table is NULL) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文