Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Postgresql 如何在sql语句中选择临时变量_Postgresql - Fatal编程技术网

Postgresql 如何在sql语句中选择临时变量

Postgresql 如何在sql语句中选择临时变量,postgresql,Postgresql,我有以下sql语句: select * from funds_balance where account_id = 7 and sequence_number <= ( select sequence_number from funds_balance where account_id = 7 and update_settlement_pool_id = 7 union all select sequence_number from funds_balance_hist

我有以下sql语句:

select * from funds_balance where account_id = 7 and sequence_number <= (
   select sequence_number from funds_balance where account_id = 7 and update_settlement_pool_id = 7
   union all
   select sequence_number from funds_balance_history where account_id = 7 and update_settlement_pool_id = 7
   limit 1
)
union all
select account_id, deliverable_id, cash, credit,pending,sequence_number, update_time,update_reason, update_funds_action_id, update_trade_serial, update_settlement_pool_id
from funds_balance_history where account_id = 7 and sequence_number <= (
   select sequence_number from funds_balance where account_id = 7 and update_settlement_pool_id = 7
   union all
   select sequence_number from funds_balance_history where account_id = 7 and update_settlement_pool_id = 7
   limit 1
)
order by sequence_number desc limit 2
在上面的复杂语句中,有两个相同的子查询,我假设它们执行了两次

有没有办法只执行此子查询一次,或者将其存储为某种temp变量以在外部查询中引用

下面的伪代码描述了我的意思:

var seq_num = select sequence_number from funds_balance where account_id = 7 and update_settlement_pool_id = 7
   union all
   select sequence_number from funds_balance_history where account_id = 7 and update_settlement_pool_id = 7
   limit 1

select * from funds_balance where account_id = 7 and sequence_number <= seq_num
union all
select account_id, deliverable_id, cash, credit,pending,sequence_number, update_time,update_reason, update_funds_action_id, update_trade_serial, update_settlement_pool_id
from funds_balance_history where account_id = 7 and sequence_number <= seq_num
order by sequence_number desc limit 2

要简单地将预期行为转换为SQL查询,可以使用:

WITH seq_num AS (

   select sequence_number from funds_balance where account_id = 7 and update_settlement_pool_id = 7
   union all
   select sequence_number from funds_balance_history where account_id = 7 and update_settlement_pool_id = 7
   limit 1

)

select * from funds_balance where account_id = 7 
    and sequence_number <= (SELECT sequence_number FROM seq_num)
union all
select account_id, deliverable_id, cash, credit,pending,sequence_number, update_time,update_reason, update_funds_action_id, update_trade_serial, update_settlement_pool_id
from funds_balance_history where account_id = 7 
    and sequence_number <= (SELECT sequence_number FROM seq_num)

我认为我的子查询执行了两次,对吗?或者查询优化人员看到了吗?我相信这取决于您的数据库配置和表结构。Tbh,我不太确定,但是当您显示执行计划时,您可以看到:解释分析。解释分析显示,使用您的方法,计划时间和执行时间都减少了50%。