在Oracle 9i数据库中使用LEFT JOIN这种连表查询方式的效率是极为低下的。
在项目中使用了这么一条语句:
select tmp2.company_name,sum(tmp2.radio_send_bytes + tmp2.radio_recv_bytes)/(1024*1024*1024) as flow
from
(select *
from
(select * from ap_pm_up a
where a.begin_time >= to_date('2010-06-01 00:00:00','yyyy-mm-dd hh24:mi:ss')
and a.begin_time <= to_date('2010-06-30 23:23:59','yyyy-mm-dd hh24:mi:ss')
) tmp
left join ap_info ap on ap.ap_id=tmp.ap_id
left join company c on c.company_id= ap.company_id
left join hot h on h.hp_id=ap.hp_id
left join hot_type ht on ht.hot_type_id=h.hot_type_id
where ht.hot_type_name='南宁') tmp2
group by tmp2.company_name
此语句在plSql中执行10分钟都没有结果,其中ap_pm_up 表里数据2000多万条。
而把语句中的所有LEFT JOIN去掉,换成子查询的话,语句如下:
select tmp.company_name,
(sum(tmp.radio_recv_bytes + tmp.radio_send_bytes)/(1024*1024*1024)) as flow
from
(
select (select c.company_name
from company c
where c.company_id in
(select ai.company_id from ap_info ai where ai.ap_id = a.ap_id)) as company_name,
a.radio_recv_bytes,
a.radio_send_bytes
from ap_pm_up a
where a.begin_time >=
to_date('2010-06-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
and a.begin_time <=
to_date('2010-06-30 23:23:59', 'yyyy-mm-dd hh24:mi:ss')
and a.ap_id in
(select ap.ap_id
from ap_info ap
where ap.hp_id in
(select h.hp_id
from hot h
where h.hot_type_id in
(select ht.hot_type_id
from hot_type ht
where ht.hot_type_name = '学校')))
) tmp
group by tmp.company_name
这条语句执行仅用了17.4秒就得出了结果。
第一条语句在SQL Server中使用是没有问题的,但在Oracle 9i中使用,效率就很低了,所以我们在Oracle中尽量避免使用Left Join等关键字,虽然这样看起来比较直观。