postgres使用自联接更新更快的查询

时间:2017-09-18 02:47:24

标签: sql postgresql join self-join

我有一个名为 feature 的表,如bellow,

+--------------------+---------+
| Column             | Type    |
|--------------------+---------+
| id                 | bigint  |
| type               | bigint  |
| oid                | bigint  |
| level              | bigint  |
| flag               | bigint  |
+--------------------+---------+
Indexes:
    "feature_pkey" PRIMARY KEY, btree (id)
    "flag_idx" btree (flag)
    "level_idx" btree (level)
    "oid_idx" btree (oid)
    "type" btree (type)

我需要更新levelflag。我正在使用以下查询进行更新。

UPDATE feature f
SET level = f1.level,
flag = f1.flag
from feature f1
where f.oid = f1.oid
and f.type = 5
and f1.type = 1;

这是我的查询计划。

+--------------------------------------------------------------------------------------------------+
| QUERY PLAN                                                                                       |
|--------------------------------------------------------------------------------------------------|
| Update on feature f  (cost=0.85..16.90 rows=1 width=470)                                         |
|   ->  Nested Loop  (cost=0.85..16.90 rows=1 width=470)                                           |
|         ->  Index Scan using type on feature f  (cost=0.43..8.45 rows=1 width=448)               |
|               Index Cond: (type = 5)                                                             |
|         ->  Index Scan using oid_idx on feature f1  (cost=0.43..8.45 rows=1 width=30)            |
|               Index Cond: (oid = f.oid)                                                          |
|               Filter: (type = 1)                                                                 |
+--------------------------------------------------------------------------------------------------+

表包含大约10M行。更新列需要数小时(超过10小时)。

我在这里做错了吗?如何加快我的查询?

0 个答案:

没有答案