假设我有以下假设的数据结构:
create table "country"
(
country_id integer,
country_name varchar(50),
continent varchar(50),
constraint country_pkey primary key (country_id)
);
create table "person"
(
person_id integer,
person_name varchar(100),
country_id integer,
constraint person_pkey primary key (person_id)
);
create table "event"
(
event_id integer,
event_desc varchar(100),
country_id integer,
constraint event_pkey primary key (event_id)
);
我想查询每个国家的人员和事件的行数。我决定使用子查询。
select c.country_name, sum(sub1.person_count) as person_count, sum(sub2.event_count) as event_count
from
"country" c
left join (select country_id, count(*) as person_count from "person" group by country_id) sub1
on (c.country_id=sub1.country_id)
left join (select country_id, count(*) as event_count from "event" group by country_id) sub2
on (c.country_id=sub2.country_id)
group by c.country_name
我知道您可以通过在字段列表中使用 select 语句来做到这一点,但使用子查询的优点是我可以更灵活地更改 SQL 以使其汇总并使用另一个字段。假设我将查询更改为按大陆显示,就像将字段“c.country_name”替换为“c.continent”一样简单。
我的问题是关于过滤。如果我们像这样添加 where 子句:
select c.country_name,
sum(sub1.person_count) as person_count,
sum(sub2.event_count) as event_count
from
"country" c
left join (select country_id, count(*) as person_count from "person" group by country_id) sub1
on (c.country_id=sub1.country_id)
left join (select country_id, count(*) as event_count from "event" group by country_id) sub2
on (c.country_id=sub2.country_id)
where c.country_name='UNITED STATES'
group by c.country_name
子查询似乎仍在为所有国家/地区执行计数。假设 person 和 event 表很大,并且我已经在所有表的 country_id 上都有索引。它真的很慢。数据库不应该只执行被过滤国家的子查询吗?我是否必须为每个子查询重新创建国家过滤器(这非常繁琐且代码不易修改)?顺便说一句,我同时使用 PostgreSQL 8.3 和 9.0,但我猜其他数据库也会发生同样的情况。