需要帮助以避免下面的SQL代码冗余是SQL语句,其中列dept10和dept20具有相同的代码但差别不大。
有人可以帮助避免它,因为由于该声明需要很长时间才能运行。
需要帮助以避免SQL代码fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
}
});
中的此类冗余。
以下是此类sql的示例:
select dname from dept where deptno = xx
#
以下是我试图避免代码冗余的实际查询: -
列amt7和amt8是具有相同sql代码的列
select column1,
column2,
(select dname from dept where deptno=10) dept10,
(select dname from dept where deptno=20) dept20
from dual
where column1 = xyz;
以下是解释计划: -
SELECT /*+ GATHER_PLAN_STATISTICS */ rr.receipt_no,
sc.bill_id bill_id,
2 AS sNo,
50 AS pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
Decode(crtd.tax_id, '14', SUM(crtd.tax_colln_amt),0) gpen,
(CASE WHEN sc.type_of_bill = 'M' AND CRTD.tax_id = 14 AND SUM(CRTD.tax_colln_amt) > 0 THEN '350300402'
WHEN sc.type_of_bill = 'R' AND CRTD.tax_id = 14 AND SUM(CRTD.tax_colln_amt) > 0 THEN '350300304'
ELSE '350300104'
END) GL_Account_code,
Nvl(SUM(CASE WHEN Substr(sc.period, 1, 4) <= '2009' THEN
(SELECT (CASE WHEN
(SUM( Nvl(bd.bd_r_tax_amt, 0) + Nvl( bd.bd_nr_tax_amt, 0))) = 0 THEN 0
ELSE (SUM( Nvl(bd.bd_r_tax_amt, 0)+ Nvl( bd.bd_nr_tax_amt, 0)) )
END )
FROM abm_bill_detail bd,
cv_ptax_outstanding b
WHERE bd.bd_bill_id = sc.bill_id
AND b.bill_id = bd.bd_bill_id
AND b.status = 'A'
AND Substr(b.bill_code, 1, 6) = Substr(b.bill_code, 1, 6)
AND bd.bd_taxhead_id = 7
AND bd.isdeleted = 'N')
ELSE (CASE WHEN
(SELECT SUM(bdd.tax_amount)
FROM cv_bill_detail bdd
WHERE bdd.bill_mas_id = sc.bill_id
AND bdd.tax_id = 7
AND bdd.status = 'A') = 0 THEN 1
END )
END), 0) amt7,
Nvl(SUM(CASE WHEN Substr(sc.period, 1, 4) <= '2009' THEN
(SELECT (CASE WHEN
(SUM( Nvl(bd.bd_r_tax_amt, 0) + Nvl( bd.bd_nr_tax_amt, 0)) ) = 0 THEN 0
ELSE ( SUM( Nvl(bd.bd_r_tax_amt, 0) + Nvl( bd.bd_nr_tax_amt, 0)))
END )
FROM abm_bill_detail bd,
cv_ptax_outstanding b
WHERE bd.bd_bill_id = sc.bill_id
AND b.bill_id = bd.bd_bill_id
AND b.status = 'A'
AND Substr(b.bill_code, 1, 6) = Substr(b.bill_code, 1, 6)
AND bd.bd_taxhead_id = 8
AND bd.isdeleted = 'N')
ELSE (CASE WHEN
(SELECT SUM(bdd.tax_amount)
FROM cv_bill_detail bdd
WHERE bdd.bill_mas_id = sc.bill_id
AND bdd.tax_id = 8
AND bdd.status = 'A') = 0 THEN 0
END)
END), 0) amt8,
CFC.fc_rrloc_id FUND,
CFC.fc_fund_center FCTR,
CFC.fc_business_area BA,
'99100000000' FUNCTIONCODE,
'11' FUNDCODE,
rr.receipt_date AS CollDate,
rr.created_at
FROM cv_receipt_sac_detail sc,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
WHERE rr.rct_id = sc.rct_id
AND a.cvp_id = sc.cvp_id
AND cfc.fc_rrloc_id = a.ward_id
AND crtd.rct_det_id = sc.rcpt_det_id
AND crtd.tax_id = 14
AND rr.status = 'A'
AND sc.status = 'A'
AND ( a.status = 'A'
OR a.status = 'O' )
AND crtd.status = 'A'
GROUP BY CFC.fc_rrloc_id,
CFC.fc_fund_center,
rr.created_at,
CFC.fc_business_area,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date,
sc.bill_id,
sc.period,
sc.type_of_bill;
答案 0 :(得分:2)
SELECT *
FROM dual
CROSS JOIN (
SELECT * FROM dept
PIVOT(
max(dname) FOR (deptno) IN (10 as Dept10, 20 as dept20 )
))
/* WHERE column1 = 2 */
答案 1 :(得分:0)
很难对只使用dual的示例提供更好的查询,但基本上不是在结果集中进行查询,而是应该使用连接。这将允许db选择加入表的最低成本方法。下面是一个例子的尝试,你提供了什么,但实际上没有多大意义,因为正在使用双重。您上面的查询基本上只是一起运行3个独立查询,通常您可以通过某些相关列(XXX)将它们连接在一起。如果没有,那么为什么甚至将它们作为1个查询运行,单独运行它们。
select column1, column2, d10.dname dept10, d20.dname dept20
from dual left outer join dept d10 on dual.XXX = d10.XXX and d10.deptno = 10
left outer join dept d20 on dual.XXX = d20.XXX and d20.deptno = 20
where column1 = xyz;
对查询运行解释计划确实可以帮助您查看数据库实际对您的查询执行的操作。当您在大型表上看到全范围扫描并且查询速度很慢时,那么这可能是您需要索引的信号。
答案 2 :(得分:0)
查询逻辑不容易理解,并且没有通用的方法来避免冗余。此外,没有办法测试结果和性能。 以下是如何更改查询的示例:
WITH
sc as
(select /*+ MATERIALIZE */ bill_id, type_of_bill, Substr(period, 1, 4) period, msr_no, rct_id, cvp_id
FROM cv_receipt_sac_detail
WHERE status= 'A'
GROUP BY bill_id, type_of_bill, Substr(period, 1, 4), msr_no, rct_id, cvp_id
)
qamt1 as
(SELECT /*+ MATERIALIZE */ a.bill_id,
SUM( decode(bd.bd_taxhead_id, 8, Nvl(bd.bd_r_tax_amt, 0) + Nvl( bd.bd_nr_tax_amt, 0)),0) as q1_amt8,
SUM( decode(bd.bd_taxhead_id, 7, Nvl(bd.bd_r_tax_amt, 0) + Nvl( bd.bd_nr_tax_amt, 0)),0) as q1_amt7
FROM abm_bill_detail bd,
cv_ptax_outstanding b,
sc a
WHERE bd.bd_bill_id = cs.bill_id
AND b.bill_id = bd.bd_bill_id
AND b.status = 'A'
AND Substr(b.bill_code, 1, 6) = Substr(b.bill_code, 1, 6)
AND bd.bd_taxhead_id IN (8,7)
AND bd.isdeleted = 'N'
),
qamt2 as
(SELECT /*+ MATERIALIZE */ a.bill_id,
SUM( decode(bd.bd_taxhead_id, 8, SUM(bdd.tax_amount), 0)) q2_amt8,
SUM( decode(bd.bd_taxhead_id, 7, SUM(bdd.tax_amount), 0)) q2_amt7,
FROM cv_bill_detail bdd,
sc a
WHERE bdd.bill_mas_id = a.bill_id
ANDbd.bd_taxhead_id IN (8,7)
AND bdd.status = 'A'
)
SELECT /*+ GATHER_PLAN_STATISTICS */ rr.receipt_no,
sc.bill_id bill_id,
2 AS sNo,
50 AS pk,
rr.receipt_date,
a.sac_no,
sc.msr_no,
Decode(crtd.tax_id, '14', SUM(crtd.tax_colln_amt),0) gpen,
(CASE WHEN sc.type_of_bill = 'M' AND CRTD.tax_id = 14 AND SUM(CRTD.tax_colln_amt) > 0 THEN '350300402'
WHEN sc.type_of_bill = 'R' AND CRTD.tax_id = 14 AND SUM(CRTD.tax_colln_amt) > 0 THEN '350300304'
ELSE '350300104'
END) GL_Account_code,
Nvl(SUM(CASE WHEN sc.period <= '2009' THEN q1_amt7
ELSE (CASE WHEN q2_amt7 = 0 THEN 1 END )
END), 0) amt7,
Nvl(SUM(CASE WHEN sc.period <= '2009' THEN q1_amt8
ELSE (CASE WHEN q2_amt8 = 0 THEN 0 END )
END), 0) amt8,
CFC.fc_rrloc_id FUND,
CFC.fc_fund_center FCTR,
CFC.fc_business_area BA,
'99100000000' FUNCTIONCODE,
'11' FUNDCODE,
rr.receipt_date AS CollDate,
rr.created_at
FROM sc,
qamt1,
qamt2,
cv_receipt_master rr,
cv_assessment a,
cv_fund_collection cfc,
cv_receipt_tax_detail crtd
WHERE rr.rct_id = sc.rct_id
AND sc.bill_id = qamt1.bill_id(+)
AND sc.bill_id = qamt2.bill_id(+)
AND a.cvp_id = sc.cvp_id
AND cfc.fc_rrloc_id = a.ward_id
AND crtd.rct_det_id = sc.rcpt_det_id
AND crtd.tax_id = 14
AND rr.status = 'A'
AND a.status IN ( 'A', 'O' )
AND crtd.status = 'A'
GROUP BY CFC.fc_rrloc_id,
CFC.fc_fund_center,
rr.created_at,
CFC.fc_business_area,
rr.receipt_date,
crtd.tax_id,
crtd.tax_colln_amt,
a.sac_no,
sc.msr_no,
rr.receipt_no,
rr.receipt_date,
sc.bill_id,
sc.period,
sc.type_of_bill;