大神求助,像这样的sql能优化到100ms一下吗?

EXPLAIN
select sql_calc_found_rows a.id,a.custom_name,
ifnull((select sum(b.change_money)
from contract_change b
join contract_list c on c.id= b.contract_id and c.company_id = 64 and c.delete_time is null and c.contract_type <> 3
where b.company_id = 64 and b.change_status = 2 and b.change_date <= '2019-02-28' and c.custom_id = a.id ),0) + ifnull((select sum(initial_money)
from contract_list b
where b.company_id= 64 and b.delete_time is null and b.contract_date<= '2019-02-28' and b.contract_type <> 3 and b.custom_id = a.id ),0) as zxiaoshou,

ifnull((select sum(arrival_amount)
from verification_contract d
join contract_list b on b.id= d.contract_id and b.company_id= 64 and b.delete_time is null and b.contract_type <> 4
where d.company_id= 64 and d.returning_time<= '2019-02-28' and d.delete_time is null and b.custom_id = a.id ),0) as zhuikuanje,

ifnull((select sum(b.change_money)
from contract_change b
join contract_list c on c.id= b.contract_id and c.company_id = 64 and c.delete_time is null and c.contract_type <> 3
where b.company_id = 64 and b.change_status = 2 and b.change_date >= '2019-02-01' and b.change_date <= '2019-02-28' and c.custom_id = a.id
),0) + ifnull((select sum(initial_money)
from contract_list b
where b.company_id= 64 and b.delete_time is null and b.contract_date>= '2019-02-01' and b.contract_date<= '2019-02-28' and b.contract_type <> 3 and b.custom_id = a.id ),0) as bqxiaoshou,

ifnull((select sum(arrival_amount)
from verification_contract d
join contract_list b on b.id= d.contract_id and b.company_id= 64 and b.delete_time is null and b.contract_type <> 4
where d.company_id= 64 and d.returning_time<= '2019-02-01' and d.returning_time<= '2019-02-28' and d.delete_time is null and b.custom_id = a.id ),0) as bqhuikuanje,

ifnull((select sum(f.billing_money)
from billing_list e
join billing_contract f on e.id = f.billing_id and f.company_id = 64 and f.delete_time is null
join contract_list b on b.id = f.contract_id and b.company_id = 64 and b.delete_time is null and b.contract_type <> 4
where e.company_id = 64 and e.status = 4 and e.delete_time is null and e.billing_date>='2019-02-01' and e.billing_date<='2019-02-28' and b.custom_id = a.id ),0) as billMoney,

ifnull((select sum(returning_money)
from verification_contract d
join contract_list b on b.id= d.contract_id and b.company_id= 64 and b.delete_time is null and b.contract_type <> 4 and b.contract_date <= '2019-02-28'
where d.company_id= 64 and d.returning_time>= '2019-02-01' and d.returning_time<= '2019-02-28' and d.delete_time is null and b.custom_id = a.id ),0) as bqhexiaoje,

ifnull((select sum(discount_amount)
from verification_contract d
join contract_list b on b.id= d.contract_id and b.company_id= 64 and b.delete_time is null and b.contract_type <> 4 and b.contract_date <= '2019-02-28'
where d.company_id= 64 and d.returning_time>= '2019-02-01' and d.returning_time<= '2019-02-28' and d.delete_time is null and b.custom_id = a.id ),0) as zherangje,

ifnull((select ifnull(sum(b.initial_money) - ifnull(sum(dd.returning_money),0),0)
from contract_list b
left join ( select contract_id,sum(returning_money) returning_money from verification_contract d where d.company_id=64 and d.returning_time<'2019-02-01' and delete_time is null group by contract_id) dd on dd.contract_id=b.id
where b.company_id=64 and b.contract_type <> 3 and b.contract_date<'2019-02-01' and b.delete_time is null and b.custom_id = a.id ),0) +

ifnull((select sum(b.change_money)
from contract_change b
join contract_list c on c.id= b.contract_id and c.company_id= 64 and c.contract_type<>3
where b.company_id= 64 and b.change_status= 2 and b.change_date<'2019-02-01' and b.delete_time is null and b.custom_id = a.id ),0) as qcysye,

ifnull((select ifnull(sum(b.initial_money) - ifnull(sum(dd.returning_money),0),0)
from contract_list b
left join ( select contract_id,sum(returning_money) returning_money from verification_contract d where d.company_id=64 and d.returning_time <= '2019-02-28' and delete_time is null group by contract_id) dd on dd.contract_id=b.id
where b.company_id=64 and b.contract_type <> 3 and b.contract_date<='2019-02-28' and b.delete_time is null and b.custom_id = a.id ),0) +

ifnull((select sum(b.change_money)
from contract_change b
join contract_list c on c.id= b.contract_id and c.company_id= 64 and c.contract_type<>3
where b.company_id= 64 and b.change_status= 2 and b.change_date<= '2019-02-28' and b.delete_time is null and b.custom_id = a.id ),0) as qmysye,

ifnull((select sum(advance_money)
from advance_payment_details g
where g.company_id = 64 and g.date <= '2019-02-28' and g.delete_time is null and g.custom_id = a.id),0) as yushoukuan

from custom_list a

left join contract_list on contract_list.custom_id= a.id and contract_list.company_id=64 and contract_list.delete_time is null

where a.company_id = 64 and a.delete_time is null

group by a.id

limit 0,20
图片说明

目前300ms左右。还有像这种需要查很多统计的数据,子查询比较方便。那一般优化时大神什么思路,从建表到sql查询方面上

耗时只是程序性能的一个维度,你要考虑下数据库CPU?数据库会不会GG,进而导致程序不可用?假设这条sql调用频率高了,会不会影响其它sql执行?这种复杂的逻辑交给数据库处理实在是个很大的隐患。
大夫看病还讲究个望闻问切,至于你说的优化查询确实能做,但也得结合具体业务场景。否则就是一系列的雪崩效应。
有几个建议,从表层到原理:
1.需求合不合理?
2.sql写法能否优化?
3.表结构是否可以优化设计?(字段、索引、缓存)
4.创造条件,是否可以用定时任务,将需要查询的结果定时计算到单独的表中,便于查询?
5.运用缓存,是否可以用定时任务,直接把结果数据写入缓存服务中?

可以,但是一般没有必要。你只要采用3倍速度的硬件即可。