【问题标题】:Updating table from temp table with many rows in postgres从临时表更新表,在 postgres 中有很多行
【发布时间】:2016-02-22 21:52:25
【问题描述】:

我有一个名为 repos 的主表和一个来自 csv 的临时表,我想匹配名称,并使用临时表中的新值更新一个值。我的代码可以工作,但速度很慢。

CREATE TEMP TABLE tmp(name text, language text);
COPY tmp FROM 'path/to/csv';

UPDATE repos
    SET language = x.language
FROM (
    SELECT * FROM tmp) x
WHERE repos.name = x.name

【问题讨论】:

  • 切换到实际表,即不是临时表,有什么区别吗?

标签: sql postgresql


【解决方案1】:

首先,子查询是不必要的。不影响性能但很尴尬:

UPDATE repos
    SET language = x.language
FROM tmp x
WHERE repos.name = x.name;

对于此查询,您需要在tmp(name, language) 上建立索引:

create index idx_tmp_name_language on tmp(name, language);

在填充表后创建索引通常更快(与先创建索引然后加载表相反)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-16
    • 2010-10-05
    • 2023-01-13
    • 1970-01-01
    相关资源
    最近更新 更多