我有以下查询,大约需要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;
答案 0 :(得分:1)
答案是:否,在这种情况下你不能使用基于功能的索引。
只能在来自ONE表的列上创建索引(任何索引)
COUNT中的表达式引用了三个表:l + t + s
count(distinct (case when l.NAME ='LPS' and t.BATCH is null then s.SAMPLE_NUMBER
else null end)) LPS