2013-05-17 104 views
1

我用下面的子查詢獲取附加記錄。我需要知道它是爲我的任務優化查詢(似乎在三個月內它的存在記錄超過10,000),那麼它是否支持該數據加載。SQL JOIN查詢使用子查詢進行優化

我可以使用JOIN關鍵字,而不是下面的method.please建議我來整理一下。 目前我使用postgresql作爲我的後端。

select worker,worktype,paymenttype,sum(output)as totalkgs_ltrs,sum(overkgs)as overkgs_ltrs,sum(workedhrs) as workedhrs,sum(scrap) as scrap,sum(cashworkincome) as cashworkincome,sum(pss) as pss 
from (select 
    comp.name as company, 
    est.name as estate, 
    div.name as division, 
    wkr.name as worker, 
    txn.date as updateddate, 
    txn.type as worktype, 
    txn.payment_type as paymenttype, 
    txn.names as workedhrs, 
    txn.norm as norm, 
    txn.output as output, 
    txn.over_kgs as overkgs, 
    txn.scrap as scrap, 
    txn.cash_work_income as cashworkincome, 
    txn.pss as pss 
from 
    bpl_daily_transaction_master txn, 
    res_company comp, 
    bpl_division_n_registration div, 
    bpl_estate_n_registration est, 
    bpl_worker wkr 
where 
    comp.id = txn.bpl_company_id and 
    div.id = txn.bpl_division_id and 
    est.id = txn.bpl_estate_id and 
    wkr.id = txn.worker_id 
)as subq 
group by worker,worktype,paymenttype 

這裏顯示了我的結果,當我執行這個查詢

refer this image

這裏是子查詢的代碼&結果標記在底部

select 
    comp.name as company, 
    est.name as estate, 
    div.name as division, 
    wkr.name as worker, 
    txn.date as updateddate, 
    txn.type as worktype, 
    txn.payment_type as paymenttype, 
    txn.names as workedhrs, 
    txn.norm as norm, 
    txn.output as output, 
    txn.over_kgs as overkgs, 
    txn.scrap as scrap, 
    txn.cash_work_income as cashworkincome, 
    txn.pss as pss 
from 
    bpl_daily_transaction_master txn, 
    res_company comp, 
    bpl_division_n_registration div, 
    bpl_estate_n_registration est, 
    bpl_worker wkr 
where 
    comp.id = txn.bpl_company_id and 
    div.id = txn.bpl_division_id and 
    est.id = txn.bpl_estate_id and 
    wkr.id = txn.worker_id 

這是上述主要的查詢結果,並其顯示所有記錄

refer this image

回答

2
select wkr.name as worker,txn.type as worktype,txn.payment_type as paymenttype,sum(txn.output)as totalkgs_ltrs,sum(txn.over_kgs)as overkgs_ltrs, 
     sum(txn.names) as workedhrs,sum(txn.scrap) as scrap,sum(txn.cash_work_income) as cashworkincome,sum(txn.pss) as pss 

from 
    bpl_daily_transaction_master txn 
inner join res_company comp 
    on comp.id = txn.bpl_company_id 
inner join bpl_division_n_registration div 
    on div.id = txn.bpl_division_id 
inner join bpl_estate_n_registration est 
    on est.id = txn.bpl_estate_id 
inner join bpl_worker wkr 
    on wkr.id = txn.worker_id 

group by wkr.name,txn.type,txn.payment_type 

你在你的子查詢做什麼是舊的ANSI SQL語法-89加盟不推薦表。 但是就表現而言,我不認爲這是stackoverflow thread確認的差異。

據「SQL性能調優」由Peter Gulutzan和特魯迪 佩爾澤,他們測試了六個或八個RDBMS品牌,有與SQL-92 風格優化或SQL-89的性能沒有 差異連接。人們可以假設大多數RDBMS引擎在優化或執行查詢之前將 語法轉換爲內部表示,因此人類可讀的語法沒有區別。

+0

非常感謝你親愛的mhasan :-) 它真的很有幫助 –