【发布时间】:2017-03-02 07:22:53
【问题描述】:
我有两张大桌子:
Table "public.tx_input1_new" (100,000,000 rows)
Column | Type | Modifiers
----------------|-----------------------------|----------
blk_hash | character varying(500) |
blk_time | timestamp without time zone |
tx_hash | character varying(500) |
input_tx_hash | character varying(100) |
input_tx_index | smallint |
input_addr | character varying(500) |
input_val | numeric |
Indexes:
"tx_input1_new_h" btree (input_tx_hash, input_tx_index)
Table "public.tx_output1_new" (100,000,000 rows)
Column | Type | Modifiers
--------------+------------------------+-----------
tx_hash | character varying(100) |
output_addr | character varying(500) |
output_index | smallint |
input_val | numeric |
Indexes:
"tx_output1_new_h" btree (tx_hash, output_index)
我想通过另一个表来更新table1:
UPDATE tx_input1 as i
SET
input_addr = o.output_addr,
input_val = o.output_val
FROM tx_output1 as o
WHERE
i.input_tx_hash = o.tx_hash
AND i.input_tx_index = o.output_index;
在我执行这个 SQL 命令之前,我已经为这两个表创建了索引:
CREATE INDEX tx_input1_new_h ON tx_input1_new (input_tx_hash, input_tx_index);
CREATE INDEX tx_output1_new_h ON tx_output1_new (tx_hash, output_index);
我使用EXPLAIN 命令查看查询计划,但它没有使用我创建的索引。
完成此UPDATE 大约需要 14-15 小时。
里面有什么问题?
如何缩短执行时间,或调整我的数据库/表?
谢谢。
【问题讨论】:
-
请Edit您的问题并添加执行计划(再次:formatted text请
-
好的。对不起。
-
您的问题更多是关于:Why postgres is not using the index?
-
我已经搜索过这个主题,但它无济于事。我认为这可能不是由错误的索引引起的。
-
1)
input_tx_hash在任一表中包含多少个不同的值? 2) 这次更新实际上会改变多少条记录的值?
标签: performance postgresql sql-update sql-execution-plan