GBase 8a
性能调优
文章

SQL优化案例-规避拉表,减少多节点数据交换

发表于2025-05-17 17:08:3655次浏览5个评论

MPP架构数据库,SQL运行的最优计划为本地数据直接计算,而不需要进行拉表重分布,不拉表的规则如下:
1、join时两表的关联列均为hash列;
2、join时两表任意一张表为复制表;
3、group 时hash列在其中;

优化案例-group by后跟hash分布列

 

select 

   x.rwkey,

   x.gjnum,

      (select count(1) from tb_ryhc_basic zj where zj.rw_id=w.id) yhcnum

  from (

   select 

   if(ifnull(f.hbid,'')='',x.gjid,f.hbid) rwkey,

   max(x.hdsj) hdsj,max(x.jbxxid) jbxxid,max(x.zjh) zjh,max(x.xm) xm,count(1) gjnum

   from fk_rysc_gjhz_xkyry x 

   left join fk_rysc_gjhz_rwxf f on  x.jbxxid=f.jbxxid and  x.gjid=f.gjid 

   where 1=1

         and x.hdsj >= concat('2017-03-01',' 00:00:00')

         and x.hdsj <= concat('2018-03-03',' 23:59:59')

   group by rwkey 

  ) x

  left join fk_rysc_ryjbxx t on x.jbxxid = t.id

  left join tb_ryhc_rw w on x.rwkey = w.hbid and w.hclx='HC12'

  left join tb_ryhc_confirm_record qs on w.id=qs.rw_id

  where 1=1 

  and t.LSXKYRY='1' 

      order by x.gjnum desc,x.hdsj desc

    limit 0,10

优化后:

select 

   x.rwkey,

   x.gjnum,

      (select count(1) from tb_ryhc_basic zj where zj.rw_id=w.id) yhcnum

  from (

   select 

   if(ifnull(f.hbid,'')='',x.gjid,f.hbid) rwkey,

   max(x.hdsj) hdsj,x.jbxxid,max(x.zjh) zjh,max(x.xm) xm,count(1) gjnum

   from fk_rysc_gjhz_xkyry x 

   left join fk_rysc_gjhz_rwxf f on  x.jbxxid=f.jbxxid and  x.gjid=f.gjid 

   where 1=1

         and x.hdsj >= concat('2017-03-01',' 00:00:00')

         and x.hdsj <= concat('2018-03-03',' 23:59:59')

   group by x.jbxxid,rwkey  --加入hash分布列

  ) x

  left join fk_rysc_ryjbxx t on x.jbxxid = t.id

  left join tb_ryhc_rw w on x.rwkey = w.hbid and w.hclx='HC12'

  left join tb_ryhc_confirm_record qs on w.id=qs.rw_id

  where 1=1 

  and t.LSXKYRY='1' 

      order by x.gjnum desc,x.hdsj desc

    limit 0,10

优化效果:

优化前

优化后

3.58秒

1.5秒

评论

登录后才可以发表评论
用户头像
levvel发表于 8个月前
少年易学老难成,一寸光阴不可轻
崔哥发表于 6个月前
千点寒梅晓角中,一番春信画楼东。收灯庭院迟迟月,落索秋千翦翦风。鱼雁杳,水云重,异乡节序恨匆匆。当歌幸有金陵子,翠斝清尊莫放空。
用户头像
levvel发表于 3个月前
值得一读!
曾浩轩发表于 2个月前
来了
ljt98发表于 2个月前
谢谢分享