查询视图,扫描所有表,然后执行where

时间:2013-03-05 15:50:41

标签: sql postgresql view

我创建了一个视图,它是几个表的联合。

create view report as
select date,a,b,0,0 from table1
union all
select date,0,0,c,d from table2

我选择日期在哪里:

select * from report where date = '2013-01-01'

我查看解释,我看到where过滤器在执行结束时执行(在追加之后)。如何创建视图,以便在追加之前每个表上的位置。

谢谢

在postgres9.1中使用

解释::(为了保护隐私而重命名的表和列)

"Aggregate  (cost=127286.88..127286.89 rows=1 width=8)"
"  ->  Subquery Scan on external_reports  (cost=0.00..127254.46 rows=12969 width=8)"
"        Filter: (external_reports.date = '2012-09-09 00:00:00'::timestamp without time zone)"
"        ->  Append  (cost=0.00..94831.27 rows=2593855 width=54)"
"              ->  Result  (cost=0.00..71.38 rows=10 width=112)"
"                    ->  Append  (cost=0.00..71.38 rows=10 width=112)"
"                          ->  Seq Scan on report1  (cost=0.00..14.26 rows=2 width=112)"
"                                Filter: ((x)::text = 'ADS'::text)"
"                          ->  Seq Scan on report2  (cost=0.00..14.26 rows=2 width=112)"
"                                Filter: ((x)::text = 'ADS2'::text)"
"                          ->  Seq Scan on report3  (cost=0.00..14.26 rows=2 width=112)"
"                                Filter: ((x)::text = 'ADS3'::text)"
"                          ->  Seq Scan on report4  (cost=0.00..14.26 rows=2 width=112)"
"                                Filter: ((x)::text = 'ADS4'::text)"
"                          ->  Seq Scan on report5  (cost=0.00..14.26 rows=2 width=112)"
"                                Filter: ((x)::text = 'ADS5'::text)"
"              ->  Seq Scan on report6  (cost=0.00..65421.34 rows=2459934 width=54)"
"              ->  Subquery Scan on "*SELECT* 7"  (cost=0.00..4739.22 rows=133911 width=52)"
"                    ->  Seq Scan on report___  (cost=0.00..3400.11 rows=133911 width=52)"

1 个答案:

答案 0 :(得分:0)

似乎没有办法做到这一点。 由于每个选择都是分开的,并且在选择之后完成了联合,因此将在每个选择中完成。