来自不同表的 MySQL 更新状态

发布于 2024-11-08 17:17:58 字数 424 浏览 2 评论 0原文

我有一个包含相当少量数据(25k 行)的文章表。该表有一个已发布的列(布尔值),需要根据外部源的数据状态进行更新。

首先,我对所有文章设置published = 0。然后,我将提要数据加载到临时表中,其中仅包含文章 ID 和已发布 = 1。我想将已发布状态设置回临时表中具有 ID 的所有文章中的 1(临时表仅包含已发布的文章) -- 默认情况下,如果它不在 feed 中,则需要取消发布)。

现在我正在使用这个:

UPDATE articles a
LEFT JOIN data_temp b
ON a.id = b.id
SET a.published = 1
WHERE b.id IS NOT null

这可行,但它真的很慢(比如在我的开发服务器上慢了 200 秒)。看来必须有一种方法可以非常快速地做到这一点。感谢您的帮助。

I have a table of articles with a fairly small amount of data (25k rows). The table has a published column (boolean) that needs to be updated depending on the status of data from an external feed.

First I set published = 0 on all articles. Then I load the feed data into a temp table with simply the article ID and published = 1. I want to set the published status back to 1 of all the articles that have an ID in the temp table (the temp table only contains published articles-- by default, if it's not in the feed, it needs to be unpublished).

Right now I'm using this:

UPDATE articles a
LEFT JOIN data_temp b
ON a.id = b.id
SET a.published = 1
WHERE b.id IS NOT null

This works, but it's really slow (like 200 seconds slow on my dev server). It seems like there's got to be a way to do this really quickly. Thanks for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

甜点 2024-11-15 17:17:58

你有两个表的两个ID列的索引吗?如果没有,这可能会大大加快加入速度。

Have you got indexes on the two ID columns of the two tables? If not, this could potentially speed up the join a fair bit.

夏至、离别 2024-11-15 17:17:58
UPDATE articles SET published = 1 WHERE id IN (SELECT id FROM data_temp)

由于它缓存了子查询的结果,所以它应该很快......

UPDATE articles SET published = 1 WHERE id IN (SELECT id FROM data_temp)

Since it caches the result of the sub-query, it should be pretty fast…

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