Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带vs子查询的PostgreSQL_Sql_Database_Postgresql_Optimization_Query Optimization - Fatal编程技术网

带vs子查询的PostgreSQL

带vs子查询的PostgreSQL,sql,database,postgresql,optimization,query-optimization,Sql,Database,Postgresql,Optimization,Query Optimization,我有两个具有类似查询计划输出的查询: 以上查询计划: Update on cartfund a (cost=16.61..44.36 rows=1 width=43) InitPlan 1 (returns $0) -> Index Scan using "cart_UserId_key" on cart (cost=0.28..8.30 rows=1 width=4) Index Cond: ("UserId" = 6) -> H

我有两个具有类似查询计划输出的查询:

以上查询计划:

Update on cartfund a  (cost=16.61..44.36 rows=1 width=43)
   InitPlan 1 (returns $0)
     ->  Index Scan using "cart_UserId_key" on cart  (cost=0.28..8.30 rows=1 width=4)
           Index Cond: ("UserId" = 6)
   ->  Hash Join  (cost=8.31..36.06 rows=1 width=43)
         Hash Cond: (a."CartId" = b.id)
         ->  Seq Scan on cartfund a  (cost=0.00..22.90 rows=1290 width=41)
         ->  Hash  (cost=8.30..8.30 rows=1 width=10)
               ->  Index Scan using "cart_GuestUserId_key" on cart b  (cost=0.28..8.30 rows=1 width=10)
                     Index Cond: ("GuestUserId" = 1139)
类似地,此查询执行相同的任务:

with usercart as (select id from cart where "UserId" = 6)
update cartfund a
set "CartId" = c.id
from cart b, usercart c
where a."CartId" = b.id and
b."GuestUserId" = 1139;
并具有以下查询计划:

Update on cartfund a  (cost=16.61..44.39 rows=1 width=75)
   CTE usercart
     ->  Index Scan using "cart_UserId_key" on cart  (cost=0.28..8.30 rows=1 width=4)
           Index Cond: ("UserId" = 6)
   ->  Nested Loop  (cost=8.31..36.09 rows=1 width=75)
         ->  Hash Join  (cost=8.31..36.06 rows=1 width=43)
               Hash Cond: (a."CartId" = b.id)
               ->  Seq Scan on cartfund a  (cost=0.00..22.90 rows=1290 width=41)
               ->  Hash  (cost=8.30..8.30 rows=1 width=10)
                     ->  Index Scan using "cart_GuestUserId_key" on cart b  (cost=0.28..8.30 rows=1 width=10)
                           Index Cond: ("GuestUserId" = 1139)
         ->  CTE Scan on usercart c  (cost=0.00..0.02 rows=1 width=32)

我应该用哪一个?性能权衡是什么?谢谢。

如果我读对了,这两个查询在逻辑上甚至做不到相同的事情。第二个涉及一个表usercart,该表在第一个表中不存在。@TimBiegeleisen usercart是子查询select id from cart的别名,其中UserId=6,我认为这不重要。此更新中唯一需要注意的是从购物车中选择id,其中UserId=6部分。这是瓦卢·戈纳的零钱吗?如果UserId=6和GuestUserId=1139是硬编码的,为什么不把id也硬编码?@PeterRing谢谢。是的,值将更改。它们将由应用程序代码提供。您确定这两个查询是相同的吗?对于优化器来说,这是一个障碍,有时比子查询好,有时则不然:
Update on cartfund a  (cost=16.61..44.39 rows=1 width=75)
   CTE usercart
     ->  Index Scan using "cart_UserId_key" on cart  (cost=0.28..8.30 rows=1 width=4)
           Index Cond: ("UserId" = 6)
   ->  Nested Loop  (cost=8.31..36.09 rows=1 width=75)
         ->  Hash Join  (cost=8.31..36.06 rows=1 width=43)
               Hash Cond: (a."CartId" = b.id)
               ->  Seq Scan on cartfund a  (cost=0.00..22.90 rows=1290 width=41)
               ->  Hash  (cost=8.30..8.30 rows=1 width=10)
                     ->  Index Scan using "cart_GuestUserId_key" on cart b  (cost=0.28..8.30 rows=1 width=10)
                           Index Cond: ("GuestUserId" = 1139)
         ->  CTE Scan on usercart c  (cost=0.00..0.02 rows=1 width=32)