2013-05-17 66 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 :-) 它真的很有帮助 –