GBase 8a
性能调优
文章

关联键中coalesce函数的优化

发表于2026-07-10 11:24:2617次浏览0个评论

SQL:
select 
    a.tid
     ,a.cid
     ,b.bid
     ,b.name
     ,c.addr
     ,c.phone
     ......
     from t1 a 
     left join t2 b on a.tid=b.tid
     left join t3 c on coalesce(a.cid ,b.bid)=c.id
    where .........
    

     
优化方式一:设置参数
set _t_gcluster_optimizer_rewrite_coalesce_type_func=1;

优化方式二:,如果方式一不起作用,可进行SQL优化改写
select
    aa.tid
   ,aa.cid
   ,aa.bid
   ,aa.name
   ,c.addr
   ,c.phone
    .......
    from
        (select 
        a.tid
         ,a.cid
         ,b.bid
         ,coalesce(a.cid ,b.bid) as iidd
         ,b.name     
         ......
         from t1 a 
         left join t2 b on a.tid=b.tid ) aa
         
         left join t3 c on aa.iidd=c.id
    where .........

评论

登录后才可以发表评论