如果您使用 phyton 动态生成值,您可以:
- 创建一个包含单个
INSERT
语句的缓冲区
- 开始交易
INSERT
创建一个临时表并在缓冲区中执行语句
- 执行一个
UPDATE ... FROM
- 提交事务,丢弃临时表。
该UPDATE
语句将如下所示(假设有一个new_values
包含您需要更新的新值的表):
UPDATE app_post AS a SET text_location = n.text_location
FROM new_values AS n WHERE a.id = n.id
不要忘记将列定义id
为PRIMARY KEY或在它们上创建索引。
编辑:由于您遇到的性能非常慢,另一种解决方法可能是重新创建整个表。以下想法假设您没有FOREIGN KEY
对表应用任何约束app_post
,正如您在最初的帖子中所示。
-- Begin the Transaction
BEGIN;
-- Create a temporary table to hold the new values
CREATE TEMPORARY TABLE temp_update_values (
id integer PRIMARY KEY,
text_location integer
) ON COMMIT DROP;
-- Populate it
INSERT INTO temp_update_values (id, text_location) VALUES (1, 123), (2, 456) /* ... #5000 total */ ;
-- Create a temporary table merging the existing "app_post" and "temp_update_values"
CREATE TEMPORARY TABLE temp_new_app_post ON COMMIT DROP AS
SELECT a.id, COALESCE(n.text_location, a.text_location) AS text_location, a.title
FROM app_post AS a LEFT JOIN temp_update_values AS n ON a.id = n.id;
-- Empty the existing "app_post"
TRUNCATE TABLE app_post;
-- Repopulate "app_post" table
INSERT INTO app_post (id, text_location, title)
SELECT id, text_location, title FROM temp_new_app_post;
-- Commit the Transaction
COMMIT;
如果有任何FOREIGN KEY
约束,您应该注意它们,在 TRUNCATINGapp_post
表之前删除它们,并在重新填充表后重新创建它们。