非常慢"更新表"在Postgres上运作

时间:2015-09-21 12:39:44

标签: performance postgresql indexing

我使用的是Postgres 9.2,由于某种原因,我在一个相对较小的桌子(16k行)上进行了非常慢的更新问题。这是表DDL:

CREATE TABLE my_categories (
  id SERIAL,
  category_id INTEGER,
  is_done SMALLINT DEFAULT 0,
  expected_number_of_flags INTEGER DEFAULT 0 NOT NULL,
  number_of_flags INTEGER DEFAULT 0 NOT NULL,
  is_active SMALLINT DEFAULT 0 NOT NULL,
  CONSTRAINT my_categories_pkey PRIMARY KEY(id)
) 
WITH (oids = false);

CREATE INDEX my_categories_idx ON my_categories USING btree (category_id);

以下是更新某些行的统计信息:

explain analyze 
update my_categories
set expected_number_of_flags = expected_number_of_flags + 1 
where category_id = 96465;

Update on my_categories  (cost=4.27..8.29 rows=1 width=26) (actual time=199746.281..199746.281 rows=0 loops=1)
  ->  Bitmap Heap Scan on my_categories  (cost=4.27..8.29 rows=1 width=26) (actual time=50.937..51.193 rows=1 loops=1)
      Recheck Cond: (category_id = 96465)
      ->  Bitmap Index Scan on my_categories_idx  (cost=0.00..4.27 rows=1 width=0) (actual time=1.600..1.600 rows=6167 loops=1)
            Index Cond: (category_id = 96465)
Total runtime: 199746.339 ms

您能解释一下发生了什么吗?如何改善此次更新的效果?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试将FILLFACTOR设置为70或60,默认情况下设置为100,如下所示:

ALTER TABLE my_categories SET (FILLFACTOR = 70);
VACUUM FULL my_categories;
REINDEX TABLE my_categories;

然后您可以尝试运行更新查询。