Oracle資料庫 Left Join 使用之我見

2021-06-05 20:17:01 字數 1813 閱讀 6777

在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等關鍵字,雖然這樣看起來比較直觀。

oracle資料庫賦權 Oracle資料庫許可權

oracle資料庫許可權基本認識 一 oracle許可權 oracle系統提供三種許可權 object 物件級 system 系統級 role 角色級。許可權分類 1 系統許可權 系統規定使用者使用資料庫的許可權。系統許可權是對使用者而言 2 實體許可權 某種許可權使用者對其它使用者的表或檢視的訪問...

oracle資料庫和資料庫例項

oracle中的資料庫和資料庫例項 oracle的體系結構簡單來分的話,分成2部分 資料庫 database 和資料庫例項 database instance 這是個容易搞糊塗的概念,在sql server裡面,你在你的機器上安裝一次sql server,那麼你就等於安裝了一次資料庫例項,在這個例項...

資料庫 Oracle中建立資料庫

create database lihua 網上的說法是 oracle中的例項只對應乙個資料庫,出現此種情況說明oracle資料庫處於mount 裝載 或open狀態,需要使用startup nomount語句進行狀態更改,或者是直接使用dbca的圖形介面來建立 注 經測試,startup nomo...