【发布时间】:2014-10-20 03:35:48
【问题描述】:
运行封装在另一个查询中的最有效方法是什么?
1.在查询中查询:
select
id_user
from table1
where
foo in (select foo from table2 where dt_signin > '2014-01-01 00:00')
2.通过使用临时表:
create table tmp_table as
select
foo
from table2
where
dt_signin > '2014-01-01 00:00'
然后查询临时表
select
id_user
from table1 t1
join tmp_table tmp
on t1.foo = tmp.foo
使用method 1 是where clause 中的查询运行(# rows of table1) 次还是只运行一次并存储在内存中以便与foo 进一步比较?
【问题讨论】:
-
有一个
where exists子句可能对您有所帮助。另见stackoverflow.com/questions/7471625/… -
具有两个查询的解决方案通常比单个查询慢 - 特别是如果第一个查询读取 并 写入数据。
标签: sql performance postgresql