如何解决Postgresql 在相对小/中表上更新缓慢
我正在尝试对包含 1000 万行的表运行更新(我只需要更新一些行),但查询花费的时间比我想要的要多。
这是表定义:
CREATE TABLE det_price_list
(
price_list_id BIGINT NOT NULL
CONSTRAINT fk_det_list_precio_venta_2
REFERENCES price_list
ON UPDATE RESTRICT ON DELETE RESTRICT,det_price_list_id BIGINT NOT NULL,product_id VARCHAR(25),account_id BIGINT,price NUMERIC(22,8),currency_id BIGINT,last_updated TIMESTAMP,CONSTRAINT pk_det_price_list
PRIMARY KEY (price_list_id,det_price_list_id),CONSTRAINT fk_det_list_precio_venta_1
FOREIGN KEY (product_id,account_id) REFERENCES product
ON UPDATE RESTRICT ON DELETE RESTRICT,CONSTRAINT fk_det_price_list_4
FOREIGN KEY (currency_id,account_id) REFERENCES currency
);
有了这个索引:
CREATE UNIQUE INDEX indice_det_lista_item
ON det_price_list (product_id,price_list_id);
CREATE INDEX ix_det_price_list_5
ON det_price_list (price_list_id,account_id,product_id);
CREATE INDEX ix_det_price_list_1
ON det_price_list (price_list_id);
CREATE INDEX ix_det_price_list_2
ON det_price_list (product_id);
CREATE INDEX ix_det_price_list_6
ON det_price_list (account_id,product_id,price_list_id,last_updated);
CREATE INDEX ix_det_price_list_3
ON det_price_list (account_id);
CREATE INDEX ix_det_price_list_4
ON det_price_list (last_updated);
CREATE INDEX det_price_list_account_id_price_list_id_currency_id_i
ON det_price_list (account_id,currency_id);
这是查询:
update det_price_list d
set last_updated = current_timestamp
where d.account_id = 994
AND price_list_id = 4338
and d.currency_id= 2;
这里也是使用 EXPLAIN (ANALYZE,BUFFERS) 的查询计划:
QUERY PLAN
Update on det_price_list d (cost=0.56..219.89 rows=60 width=77) (actual time=1806.823..1806.823 rows=0 loops=1)
Buffers: shared hit=1040857 read=1704 dirtied=8839
I/O Timings: read=1.814
-> Index Scan using det_price_list_account_id_price_list_id_currency_id_i on det_price_list d (cost=0.56..219.89 rows=60 width=77) (actual time=0.034..28.386 rows=26776 loops=1)
Index Cond: ((account_id = 994) AND (price_list_id = 4338) AND (currency_id = 2))
Buffers: shared hit=2740 dirtied=621
Planning time: 0.157 ms
Execution time: 1806.873 ms
查询正在使用索引。 我已经尝试过 vacumm、vacumm full、重新索引和分析,但我无法减少那个时间。 我无法复制到另一个表并删除旧表,因为它不是一个完整的更新,而且该表一直在使用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。