0

我有以下查询,执行大约需要 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;
4

1 回答 1

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 
于 2013-09-23T18:12:58.790 回答