说明 :
这个sql做的事情就是从union上sql语句查询一部分数据和从union下sql查询一部分数据。两部分数据查出合并。
以前正常执行。最近发现执行这sql.会一直执行,但是没结果。求大神告知其原因,万分感谢!!select a.*  
           --,prod.productid as pid  
            from (select b.gcid as cid,      --[t_content_base_syn] 全局内容ID:lenth(datatype+(补零)+contentid)=20
                         b.DATATYPE as type,   --[t_content_base_syn] 10:游戏,20:音乐,30:视频,40:动漫,50:阅读
                         g.musicid,  --[T_MB_MUSIC_NEW_syn]歌曲ID
                         g.songname,  --[T_MB_MUSIC_NEW_syn]歌曲名称
                         g.singer,  --[T_MB_MUSIC_NEW_syn]歌手名称
                         p.pkg_id  --[T_CONTENT_PACKAGE_syn]内容包ID,
                         --p.pkg_name  --[T_CONTENT_PACKAGE_syn]内容包名称
                    from t_content_base_syn    b,  --统一内容信息表
                         T_MB_MUSIC_NEW_syn    g,  --音乐歌曲表
                         T_MB_SINGER_NEW_syn   m ,  --歌手信息表
                         T_PKG_CONTENT_syn     pc,  --内容包和内容关联关系表
                         T_CONTENT_PACKAGE_syn p,  --内容包信息表
                         filter_view v   --渠道过滤视图
                   where b.DATATYPE = 20  
                     and b.CONTENTID = g.musicid  
                     and b.gcid = pc.gcid 
                     and pc.pkg_id = p.pkg_id  
                     and b.contentid = v.contentid  
                     and (g.SINGERSID = m.sid or substr(g.SINGERSID,1,instr(g.singersid,'|')-1) = m.sid) 
                     and v.datatype = 20) a,  
                     t_product_syn prod 
                     where a.pkg_id = prod.pcid  
          union  
          select k.*  
            from (select b.gcid as cid,       --[t_content_base_syn] 全局内容ID:lenth(datatype+(补零)+contentid)=20
                         b.DATATYPE as type,  --[t_content_base_syn] 10:游戏,20:音乐,30:视频,40:动漫,50:阅读
                         g.musicid,  --[T_MB_MUSIC_NEW_syn]歌曲ID
                         g.songname,  --[T_MB_MUSIC_NEW_syn]歌曲名称
                         g.singer,  --[T_MB_MUSIC_NEW_syn]歌手名称
                          null as pkg_id  --null
                         --null as pkg_name,  --null
                         --prod.productid as pid  --产品id
                    from t_content_base_syn b,  --统一内容信息表
                         T_MB_MUSIC_NEW_syn g,  --音乐歌曲表
                         T_MB_SINGER_NEW_syn   m ,  --歌手信息表
                         t_product_syn      prod,  --产品信息表
                         filter_view v  --渠道过滤视图
                   where b.DATATYPE = 20  
                     and b.CONTENTID = g.musicid  
                     and (g.SINGERSID = m.sid or substr(g.SINGERSID,1,instr(g.singersid,'|')-1) = m.sid) 
                     and prod.pcid = b.gcid  
                     and prod.ptype = 0  
                     and b.contentid = v.contentid  
                     and v.datatype = 20) k 

解决方案 »

  1.   

    sql执行太慢,或者网络有问题,或者plsql有问题,经查遇到,多点几次那个取消按钮,看看能停下来不,停不下来的话任务管理器,把plsql结束任务,重新开,完事。
      

  2.   

    锁表应该不是,应为上面执行可以查出下面部分查询也可以查出。
    网络还行,可以说正常。这种也可以排除。数据吧,对于orcle其实也不算多。
    备注:以前确实可以正常查询。过了一段时间现象就是这样一直执行没结果。还有其他原因造成的吗?
      

  3.   

    嗯,贴下执行吧。
    filter_view是个视图吧?另外,T_PKG_CONTENT_syn和T_CONTENT_PACKAGE_syn有多少数据呢?
      

  4.   

    2个union的结果集数据量多大
    union本身效率就不高
    两个大数据集尽量不要union
      

  5.   


    ============= filter_view ======================================
    create or replace view filter_view as
    select
    distinct b.contentid,b.datatype
    from t_product_syn p,--统一产品信息
         v_package_channel c,--渠道
         t_content_base_syn b,--统一内容信息
         T_PKG_CONTENT_syn pc--内容包关联关系
    where c.productid = p.productid --产品id
          and p.ptype = 1
          and p.pcid = pc.pkg_id--包id
          and pc.gcid = b.gcid--全局id
          and pc.del_flg = '1'
          and c.channelid = '300000100001'
    union
    select distinct b.contentid, b.datatype
      from t_product_syn        p, --统一产品信息
           v_package_channel    c, --渠道
           t_content_base_syn   b, --统一内容信息
           t_single_content_syn s --单品内容信息表关联关系
     where c.productid = p.productid --产品id
       and p.pcid = b.gcid
       and p.ptype = 0
       and b.gcid = s.gcid
       and s.LINESTATUS = '0'
       and c.channelid = '300000100001';====================整个sql语句=======================select distinct music.cid,music.type,music.musicid,music.songname,music.singer from ( 
               select b.gcid as cid,      --[t_content_base_syn] 全局内容ID:lenth(datatype+(补零)+contentid)=20
                             b.DATATYPE as type,   --[t_content_base_syn] 10:游戏,20:音乐,30:视频,40:动漫,50:阅读
                             g.musicid,  --[T_MB_MUSIC_NEW_syn]歌曲ID
                             g.songname,  --[T_MB_MUSIC_NEW_syn]歌曲名称
                             g.singer,  --[T_MB_MUSIC_NEW_syn]歌手名称
                             p.pkg_id  --[T_CONTENT_PACKAGE_syn]内容包ID,
                             --p.pkg_name  --[T_CONTENT_PACKAGE_syn]内容包名称
                        from t_content_base_syn    b,  --统一内容信息表
                             T_MB_MUSIC_NEW_syn    g,  --音乐歌曲表
                             T_MB_SINGER_NEW_syn   m ,  --歌手信息表
                             T_PKG_CONTENT_syn     pc,  --内容包和内容关联关系表
                             T_CONTENT_PACKAGE_syn p,  --内容包信息表
                             filter_view v,   --渠道过滤视图
                             t_product_syn prod 
                       where b.DATATYPE = 20  
                         and b.CONTENTID = g.musicid  
                         and b.gcid = pc.gcid 
                         and p.pkg_id = prod.pcid  
                         and pc.pkg_id = p.pkg_id  
                         and b.contentid = v.contentid  
                         and (g.SINGERSID = m.sid or substr(g.SINGERSID,1,instr(g.singersid,'|')-1) = m.sid) 
                         and v.datatype = 20  
              union  
              select b.gcid as cid,       --[t_content_base_syn] 全局内容ID:lenth(datatype+(补零)+contentid)=20
                             b.DATATYPE as type,  --[t_content_base_syn] 10:游戏,20:音乐,30:视频,40:动漫,50:阅读
                             g.musicid,  --[T_MB_MUSIC_NEW_syn]歌曲ID
                             g.songname,  --[T_MB_MUSIC_NEW_syn]歌曲名称
                             g.singer,  --[T_MB_MUSIC_NEW_syn]歌手名称
                              null as pkg_id  --null
                             --null as pkg_name,  --null
                             --prod.productid as pid  --产品id
                        from t_content_base_syn b,  --统一内容信息表
                             T_MB_MUSIC_NEW_syn g,  --音乐歌曲表
                             T_MB_SINGER_NEW_syn   m ,  --歌手信息表
                             t_product_syn      prod,  --产品信息表
                             filter_view v  --渠道过滤视图
                       where b.DATATYPE = 20  
                         and b.CONTENTID = g.musicid  
                         and (g.SINGERSID = m.sid or substr(g.SINGERSID,1,instr(g.singersid,'|')-1) = m.sid) 
                         and prod.pcid = b.gcid  
                         and prod.ptype = 0  
                         and b.contentid = v.contentid  
                         and v.datatype = 20 )music
    求大神们有什么好优化方法!!!!!1
      

  6.   

    对重复数据有没有什么限制?
    Union

    Union all 还是有区别的,
    Union 会检查上下两个数据,并且只显示不同的数据,重复的数据就显示一次,你的情况会让前后的数据再一次进行比较,如果前后数据都挺多的话,会影响速度。
    Union all 是不管不顾的把上下数据连接起来,重复了也不管。就少了最后一次匹配。
      

  7.   

    用了or substr(g.SINGERSID,1,instr(g.singersid,'|')-1) = m.sid)
    又是or 又是 substr ,instr, 想快,不容易啊。这些都没法用索引的。
      

  8.   

    1、查看执行计划,看看最大的cost在哪里
    2、查看等待事件,看看哪里产生了等待