Sql postgres使用自连接更快地更新查询
我有一个名为feature的表,如bellowSql postgres使用自连接更快地更新查询,sql,postgresql,join,self-join,Sql,Postgresql,Join,Self Join,我有一个名为feature的表,如bellow +--------------------+---------+ | Column | Type | |--------------------+---------+ | id | bigint | | type | bigint | | oid | bigint | | level | bigin
+--------------------+---------+
| 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)
我需要更新级别
和标志
。我正在使用下面的查询进行更新
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) |
+--------------------------------------------------------------------------------------------------+
该表包含大约10万行。更新列需要数小时(超过10小时)
我做错什么了吗?如何加快我的查询?执行后计划。我首先在oid、type、level、FLAG上创建一个索引,删除索引并进行更新,然后重新应用indexes@vol7ron索引真的对更新操作有这么大的影响吗(比如>10小时)?如果你有很多索引,或者是复杂的索引(GiST/GIN,大的多列索引,等等)然后在一张大桌子上,它当然可以。请回答您的问题,并添加一些样本数据和预期结果。我认为这个查询可以简化,但是如果没有一个关于您实际想要做什么的明确描述,那么很难制定执行后计划。我首先在oid、type、level、FLAG上创建一个索引,删除索引并进行更新,然后重新应用indexes@vol7ron索引真的对更新操作有这么大的影响吗(比如>10小时)?如果你有很多索引,或者是复杂的索引(GiST/GIN,大的多列索引,等等)然后在一张大桌子上,它当然可以。请回答您的问题,并添加一些样本数据和预期结果。我认为这个查询可以简化,但是如果没有一个关于您实际想要做什么的清晰描述,就很难说清楚