当“FROM”子句只有一个帐号 (a.account_nbr) 和一个目标代码 (a.fin_object_cd) 时,下面的查询运行很快,但是当我修改查询以使 FROM 子句具有一系列帐号时和一系列目标代码,返回结果需要很长时间。它从大约一分钟运行到 20 分钟或更长时间。
该查询确实返回了我想要的结果,但我需要让它运行得更快。
我能做些什么?我不确定向某些列添加索引是否会有所帮助,或者是否有更好的方法来编写查询。
如果您需要有关表格的更多信息,或者我想要完成的工作,请告诉我。
select
a.account_nbr as "Account Number",
a.account_nm as "Account Name",
a.fin_object_cd as "Object Code",
a.fin_obj_cd_nm as "Object Code Name",
(select COALESCE(sum(fin_beg_bal_ln_amt),0) from kfsprd.gl_balance_t where account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and univ_fiscal_yr = "2015" and fin_balance_typ_cd != "CB") as "Beginning Balance",
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "D") as "Debits",
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "C" and trn_ldgr_entr_amt is not null) as "Credits",
(
(select COALESCE(sum(fin_beg_bal_ln_amt),0) from kfsprd.gl_balance_t where account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and univ_fiscal_yr = "2015" and fin_balance_typ_cd != "CB") +
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "D") -
(select COALESCE(sum(trn_ldgr_entr_amt),0) FROM kfsprd.kf_f_transaction_dtl where univ_fiscal_yr = "2015" and account_nbr = a.account_nbr and fin_object_cd = a.fin_object_cd and trn_debit_crdt_cd = "C")
) as "Ending Balance"
from kfsprd.kf_f_transaction_dtl a where univ_fiscal_yr = "2015" and (univ_fiscal_prd_cd between "01" and "13" or
univ_fiscal_prd_cd = "BB") and a.account_nbr between "1014000" and "1014005" and a.fin_object_cd between "0000" and "9999" and a.fin_balance_typ_cd != "CB"
group by account_nbr, fin_object_cd
order by account_nbr, fin_object_cd;