1

我使用下面的子查询来获取附加记录。我需要知道它是否为我的任务优化了查询(似乎在三个月内它的存在记录超过 10,000 条)。那么它是否支持该数据加载。?

我可以使用JOIN关键字代替下面的方法吗?请建议我解决这个问题。目前我正在使用 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

这里显示了我执行此查询时的结果

参考这张图片

这是在底部标记的子查询的代码和结果

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

这是主要查询结果的上方,它显示了所有记录

参考这张图片

4

1 回答 1

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 线程上确认没有区别。

根据 Peter Gulutzan 和 Trudy Pelzer 的“SQL 性能调优”,在他们测试的六个或八个 RDBMS 品牌中,SQL-89 与 SQL-92 样式连接的优化或性能没有差异。可以假设大多数 RDBMS 引擎在优化或执行查询之前将语法转换为内部表示,因此人类可读的语法没有区别。

于 2013-05-17T05:25:55.063 回答