语句是这样的:
    select  count(1) as count,'25岁及以下' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=0 and   (sysdate-birthday)/365<=25 and INSTR(BELONGDZB,'001.001.032.092')>0
    union select  count(1) as count,'26到35岁' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=26 and   (sysdate-birthday)/365<=35 and INSTR(BELONGDZB,'001.001.032.092')>0
    union select  count(1) as count,'36到45岁' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=36 and   (sysdate-birthday)/365<=45  and INSTR(BELONGDZB,'001.001.032.092')>0
    union select  count(1) as count,'46到54岁' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=46 and   (sysdate-birthday)/365<=55 and INSTR(BELONGDZB,'001.001.032.092')>0
    union select  count(1) as count,'55到59岁' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=56 and   (sysdate-birthday)/365<=59 and INSTR(BELONGDZB,'001.001.032.092')>0
    union select  count(1) as count,'60岁以上' as tab  from MV_UT_MEMBER where (sysdate-birthday)/365>=60 and INSTR(BELONGDZB,'001.001.032.092')>0 order by TAb
MV_UT_MEMBER 表里有50W+的数据,这样查询一次,一条记录就要比较六次.这样算下来就要比较300W次再求和,这样效率很慢啊,有什么可以提高效率的方法吗??

解决方案 »

  1.   

    (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 
    --改成下面的形式 并给birthday建索引
    birthday between sysdate-365*25 and sysdate
      

  2.   


    ----INSTR(BELONGDZB,'001.001.032.092') 建个函数索引 ---我的 你试试
    select 
    case when (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 then '25岁及以下'
    when (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 then '26到35岁'
    when (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 then '36到45岁'
    when (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 then '46到54岁'
    when (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 then '55到59岁'
    when (sysdate-birthday)/365>=60 then '60岁以上' end,
    count(1) as cnt
    from MV_UT_MEMBER
    where INSTR(BELONGDZB,'001.001.032.092')>0---你的 把union  改成union allselect '25岁及以下' as tab,count(1) as cnt  from MV_UT_MEMBER 
    where (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 and INSTR(BELONGDZB,'001.001.032.092')>0
    union all
    select '26到35岁' as tab,count(1) as cnt from MV_UT_MEMBER where (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 and INSTR(BELONGDZB,'001.001.032.092')>0
    union all
    select '36到45岁' as tab,count(1) as cnt from MV_UT_MEMBER where (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 and INSTR(BELONGDZB,'001.001.032.092')>0
    union all
    select '46到54岁' as tab,count(1) as cnt from MV_UT_MEMBER where (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 and INSTR(BELONGDZB,'001.001.032.092')>0
    union all
    select '55到59岁' as tab,count(1) as cnt from MV_UT_MEMBER where (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 and INSTR(BELONGDZB,'001.001.032.092')>0
    union all
    select '60岁以上' as tab,count(1) as cnt from MV_UT_MEMBER 
    where (sysdate-birthday)/365>=60 and INSTR(BELONGDZB,'001.001.032.092')>0 
      

  3.   

    select 
    case when (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 then '25岁及以下'
    when (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 then '26到35岁'
    when (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 then '36到45岁'
    when (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 then '46到54岁'
    when (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 then '55到59岁'
    when (sysdate-birthday)/365>=60 then '60岁以上' end,
    count(1) as cnt
    from MV_UT_MEMBER
    where INSTR(BELONGDZB,'001.001.032.092')>0
    group by case when (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 then '25岁及以下'
    when (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 then '26到35岁'
    when (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 then '36到45岁'
    when (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 then '46到54岁'
    when (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 then '55到59岁'
    when (sysdate-birthday)/365>=60 then '60岁以上' end
      

  4.   

    --采用并行查询:
    SELECT Sum(
           CASE WHEN (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 THEN 1 
                WHEN (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 THEN 1
                WHEN (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 THEN 1
                WHEN (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 THEN 1
                WHEN (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 THEN 1
                WHEN (sysdate-birthday)/365>=60 THEN 1
           END) as count,
           CASE WHEN (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 THEN '25岁及以下'
                WHEN (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 THEN '26到35岁'
                WHEN (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 THEN '36到45岁'
                WHEN (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 THEN '46到54岁'
                WHEN (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 THEN '55到59岁'
                WHEN (sysdate-birthday)/365>=60 THEN '60岁以上'
           END as tab
    from MV_UT_MEMBER 
    where INSTR(BELONGDZB,'001.001.032.092')>0
    GROUP BY (
           CASE WHEN (sysdate-birthday)/365>=0 and (sysdate-birthday)/365<=25 THEN '25岁及以下'
                WHEN (sysdate-birthday)/365>=26 and (sysdate-birthday)/365<=35 THEN '26到35岁'
                WHEN (sysdate-birthday)/365>=36 and (sysdate-birthday)/365<=45 THEN '36到45岁'
                WHEN (sysdate-birthday)/365>=46 and (sysdate-birthday)/365<=55 THEN '46到54岁'
                WHEN (sysdate-birthday)/365>=56 and (sysdate-birthday)/365<=59 THEN '55到59岁'
                WHEN (sysdate-birthday)/365>=60 THEN '60岁以上'
           END )
      

  5.   

    INSTR(BELONGDZB,'001.001.032.092')
    建个基于函数的索引试试
      

  6.   


    INSTR(BELONGDZB,'001.001.032.092')这个不是必须的,我删除这个条件后查询只快了0.1秒
      

  7.   

    ORACLE只对简单的表提供高速缓冲(cache   buffering)   ,这个功能并不适用于多表连接查询