select sum(BALANCELY)
from t_fact_loan_m t
where inputdate = '2012/01'
and t.duebillno in
(select distinct t1.duebillno
from t_fact_loan_m t1
where t1.customertype in ('0301', '0103')
or t1.duebillno in
(select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e on t2.customerid = e.customerid
where e.scope2 in ('4', '5')));
运行的太慢了!求优化
sql
优化
--------------------编程问答--------------------
不能格式化一下么?
or t1.duebillno in 这句用得着in么?连接不行么?
or能不能改union
你为啥那么喜欢in呢?
--------------------编程问答--------------------
你不觉得与其把时间放在SQL优化上,在逻辑上,或者在service层上优化一下更合适么
--------------------编程问答--------------------
in 改成 exists 事实看看。
然后用sql 工具看看执行计划到底哪一步比较慢。也有可能是建表问题。
--------------------编程问答--------------------
换成连接查询试一下
--------------------编程问答--------------------
尽量用JOIN,JOIN用不了再考虑exist,能不用IN尽量不用IN,当然,还得根据实际情况
--------------------编程问答--------------------
1、最大可能优化SQL语句,不要欺负数据库:
SELECT SUM(balancely)
FROM t_fact_loan_m t1 LEFT JOIN ent_info e ON t2.customerid = e.customerid
WHERE inputdate = '2012/01' AND (
t1.customertype IN ('0301', '0103') OR e.scope2 IN ('4', '5'))
)
select sum(BALANCELY)
from t_fact_loan_m t
where inputdate = '2012/01'
and t.duebillno in
(select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e
on t2.customerid = e.customerid
where e.scope2 in ('4', '5')
or t2.customertype in ('0301', '0103'));
或
select sum(BALANCELY)
from (select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e
on t2.customerid = e.customerid
where e.scope2 in ('4', '5')
or t2.customertype in ('0301', '0103')) a
left join t_fact_loan_m t
on t.duebillno = a.duebillno
where inputdate = '2012/01';
(select distinct t1.duebillno
from t_fact_loan_m t1
where t1.customertype in ('0301', '0103')
or t1.duebillno in
(select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e on t2.customerid = e.customerid
where e.scope2 in ('4', '5')))
(select distinct t1.duebillno
from t_fact_loan_m t1
where t1.customertype in ('0301', '0103')
or t1.duebillno in
(select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e on t2.customerid = e.customerid
where e.scope2 in ('4', '5')))
select sum(BALANCELY)
from t_fact_loan_m t
where inputdate = '2012/01'
and t.duebillno in
(select t2.duebillno
from t_fact_loan_m t2
left join ent_info e
on t2.customerid = e.customerid
where e.scope2 in ('4', '5')
or t2.customertype in ('0301', '0103'));
select sum(BALANCELY)
from (select t2.duebillno
from t_fact_loan_m t2
left join ent_info e
on t2.customerid = e.customerid
where e.scope2 in ('4', '5')
or t2.customertype in ('0301', '0103')) a
left join t_fact_loan_m t
on t.duebillno = a.duebillno
where inputdate = '2012/01';
SELECT sum(balancely)
FROM t_fact_loan_m t LEFT JOIN ent_info e ON t.customerid = e.customerid
WHERE inputdate = '2012/01' AND (
t.customertype IN ('0301', '0103') OR e.scope2 IN ('4', '5')
);
【楼主的】
select sum(BALANCELY)
from t_fact_loan_m t
where inputdate = '2012/01'
and t.duebillno in
(select distinct t1.duebillno
from t_fact_loan_m t1
where t1.customertype in ('0301', '0103')
or t1.duebillno in
(select distinct t2.duebillno
from t_fact_loan_m t2
join ent_info e on t2.customerid = e.customerid
where e.scope2 in ('4', '5')));