为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:1-2-174-2022.11.21-175072-20024-SEC
【操作系统】:
【CPU】:
【问题描述】:
以下语句执行大概要1s左右,执行次数在几千次,请问该怎么优化,提升执行效率?
select count()
from ( (select pk_trade_union
from trade_union
where higher_union_uuid in (select pk_trade_union
from trade_union
where higher_union_uuid = '8a4082d95c42ae81015c42b3db330005'
union all
select pk_trade_union
from trade_union
where higher_union_uuid in ('8a4082d95c42ae81015c42b3db330005')
and data_status = 0
and union_status = 0
and trade_union_type = 2
union all
select pk_trade_union
from trade_union
where higher_union_uuid in (select pk_trade_union
from trade_union
where higher_union_uuid in ('8a4082d95c42ae81015c42b3db330005'))
and data_status = 0
and union_status = 0
and trade_union_type = 2
union all
select pk_trade_union
from trade_union
where higher_union_uuid in (select pk_trade_union
from trade_union
where higher_union_uuid in (select pk_trade_union
from trade_union
where higher_union_uuid in ('8a4082d95c42ae81015c42b3db330005')))
and data_status = 0
and union_status = 0
and trade_union_type = 2)
and data_status = 0
and union_status = 0
and trade_union_type != 2) t
inner join member_basic_situation s
on s.pk_trade_union = t.pk_trade_union)
where s.data_status = 0;
优化第一步,先收集下统计信息。
第二步,看看走全表的过滤条件是否过滤性很好,好的话加索引。
不行的话,再看看执行计划。
能否提供下对应的涉及所有表的表结构sql,上传下,便于具体分析。
一般来说,一个长SQL要优化到极致,要逐步分析。 你可以按照如下思路:先取下长SQL里各自最小的sql单元语句,拿出来,单条的去优化,看是否能创建索引,提升效率。 最后再整体的长SQL整体来看,查看执行计划,进一步优化。