我有以下查询,执行大约需要 23 - 30 秒。样本表有 280 万行,测试表有 2110 万行。我有关于主键样本号和测试号的索引,但是 count(distinct) 子句对性能造成了严重影响。我可以在 COUNT DISTINCT 上使用基于函数的索引来提高性能吗?
select
l.NAME as LABORATORY,
count(distinct s.SAMPLE_NUMBER),
count(distinct (case when l.NAME ='LPS' and t.BATCH is null then s.SAMPLE_NUMBER
else null end)) LPS
from LABORATORY l inner join SAMPLE s on l.NAME = s.LAB
inner join TEST t on s.SAMPLE_NUMBER = t.SAMPLE_NUMBER
and s.STATUS <> 'U' and s.TEMPLATE <> 'QC_SAMPLE' and t.STATUS in ('I', 'P')
group by l.NAME;