利用业务逻辑+OB分布式特性优化SQL

Posted robinson1988

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用业务逻辑+OB分布式特性优化SQL相关的知识,希望对你有一定的参考价值。

最近某人社局核心数据库上了OB,经常出现性能问题
某人社与我司合作多年,非常信任我司在数据库的专业能力,邀请我司过去看看能否提供帮助
与OB驻场工程师合作,抓取了一天的TOP SQL,跑得慢的SQL有几十条(注意只是某一天的TOP SQL)
大致分析了一下,有缺索引的,有执行计划走错的,有SQL写法有问题的,有字段类型设计错误的,也有表分区策略设计有问题的
这些问题在我看来都很简单就不贴在博客了,下面我要分享一个对于OB SQL优化很有启发意义的案例

下面SQL每天要运行20w次,平均每次执行0.5s到2秒,它耗费了整个OB集群20%的CPU资源
也许有人会说,不就才20w次吗,我见过运行几百万次,上千万次的SQL
这里我要说的是,请不要拿国产数据库与Oracle对比,Oracle发展了40年了,国产数据库才发展多少年,能替换O已经很厉害了
每天运行20w次的时段大致在早上9:30分到11:30分以及下午2:30到5:00,也就是工作日业务办理时间

SQL代码大致如下(只贴一条,还有很多类似SQL就不贴了):

SELECT * FROM AC08  
 WHERE (AAC001 = ? AND AAE140= ? AND AAE792 IN (?,?) AND NOT EXISTS (SELECT ? FROM AC08 B WHERE AC08.AAZ686 = B.AAZ686 AND B.AAE792 IN (?)))

AC08是个超级大表,它有26亿条数据,根据AAC001 进行的HASH分区,执行计划如下:

=======================================================================
|ID|OPERATOR                 |NAME                     |EST. ROWS|COST|
-----------------------------------------------------------------------
|0 |NESTED-LOOP ANTI JOIN    |                         |0        |337 |
|1 | PX COORDINATOR          |                         |1        |302 |
|2 |  EXCHANGE OUT DISTR     |:EX10000                 |1        |294 |
|3 |   TABLE SCAN            |AC08(IDX_AC08EES_AAC001) |1        |294 |
|4 | PX COORDINATOR          |                         |1        |46  |
|5 |  EXCHANGE OUT DISTR     |:EX20000                 |1        |46  |
|6 |   SUBPLAN SCAN          |VIEW1                    |1        |46  |
|7 |    PX PARTITION ITERATOR|                         |1        |46  |
|8 |     TABLE SCAN          |B(IDX_AC08_AAZ686_AAE792)|1        |46  |
=======================================================================

Outputs & filters: 
-------------------------------------
  0 - output([AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ686], [AC08.AAC001], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE140], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE792], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil), 
      conds(nil), nl_params_([AC08.AAZ686])
  1 - output([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil)
  2 - output([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil), is_single, dop=1
  3 - output([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter([cast(cast(AC08.AAE140, VARCHAR2(3 BYTE)), NUMBER(-1, -85)) = 120], [AC08.AAE792 IN (?, ?)]), 
      access([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), partitions(p0)
  4 - output([1]), filter(nil)
  5 - output([1]), filter(nil), dop=1
  6 - output([1]), filter(nil), 
      access([VIEW1.B.AAZ686])
  7 - output([B.AAZ686]), filter(nil)
  8 - output([B.AAZ686]), filter(nil), 
      access([B.AAZ686]), partitions(p[0-127])

从执行计划上看,上面的SQL没有可以优化的地方了,访问路径是对的,表关联方式也是对的
对于一般的DBA来说,可能放弃治疗了
注意观察执行计划,ID = 8 access([B.AAZ686]), partitions(p[0-127]) 访问了所有的分区
OB是分布式数据库,会根据分区将数据打散到所有的数据节点
每次跑这个SQL都会访问所有的数据节点,如果并发较高,就会对整个OB带来压力
执行计划上没有优化的地方,那就从SQL写法和业务逻辑入手
喵了一眼SQL写法,也没问题,现在只能从业务逻辑入手了,查一下表和列的注释

AC08   养老保险人员实收明细表
AAC001 人员编号,分区KEY
AAE140 险种类型
AAE792 费用标志  
AAZ686 人员缴费ID

看到这里就知道怎么优化了,原始SQL语句中 NOT EXISTS 部分少加了个关联条件,我们再来看一下原始SQL:

SELECT * FROM AC08  
 WHERE (AAC001 = ? AND AAE140= ? AND AAE792 IN (?,?) AND NOT EXISTS (SELECT ? FROM AC08 B WHERE AC08.AAZ686 = B.AAZ686 AND B.AAE792 IN (?)))

应该把SQL改成

SELECT * FROM AC08  
 WHERE (AAC001 = ? AND AAE140= ? AND AAE792 IN (?,?) AND NOT EXISTS (SELECT ? FROM AC08 B WHERE AC08.AAZ686 = B.AAZ686 AND AC08.AAC001=B.AAC001 AND B.AAE792 IN (?)))

因为人员缴费ID(AAZ686) 一般是与人员编号(AAC001) 一一对应的
加上AC08.AAC001=B.AAC001过滤条件之后,就能避免OB跨数据节点访问了
更改后的执行计划如下:

=====================================================================
|ID|OPERATOR               |NAME                     |EST. ROWS|COST|
---------------------------------------------------------------------
|0 |EXCHANGE IN REMOTE     |                         |1        |337 |
|1 | EXCHANGE OUT REMOTE   |                         |1        |330 |
|2 |  NESTED-LOOP ANTI JOIN|                         |1        |330 |
|3 |   TABLE SCAN          |AC08(IDX_AC08EES_AAC001) |1        |294 |
|4 |   SUBPLAN SCAN        |VIEW1                    |1        |46  |
|5 |    TABLE SCAN         |B(IDX_AC08_AAZ686_AAE792)|1        |46  |
=====================================================================

Outputs & filters: 
-------------------------------------
  0 - output([AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ686], [AC08.AAC001], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE140], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE792], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil)
  1 - output([AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ686], [AC08.AAC001], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE140], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE792], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil)
  2 - output([AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ686], [AC08.AAC001], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE140], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE792], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter(nil), 
      conds(nil), nl_params_([AC08.AAZ686])
  3 - output([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), filter([cast(cast(AC08.AAE140, VARCHAR2(3 BYTE)), NUMBER(-1, -85)) = 120], [AC08.AAE792 IN (?, ?)]), 
      access([AC08.AAC001], [AC08.AAE140], [AC08.AAE792], [AC08.AAZ686], [AC08.AAZ648], [AC08.AAZ223], [AC08.AAZ159], [AC08.AAZ061], [AC08.AAB001], [AC08.AAB365], [AC08.AAE002], [AC08.AAE003], [AC08.AAA115], [AC08.AAE793], [AC08.AAE794], [AC08.AAA093], [AC08.AAC066], [AC08.AAC313], [AC08.AAC314], [AC08.AAE737], [AC08.AAB019], [AC08.AAB033], [AC08.AAE745], [AC08.AAC028], [AC08.AAC040], [AC08.AAB121], [AC08.AAE180], [AC08.AAE795], [AC08.AAE871], [AC08.AAE020], [AC08.AAE021], [AC08.AAE022], [AC08.AAE023], [AC08.AAE784], [AC08.AAE785], [AC08.AAE786], [AC08.AAE787], [AC08.AAE836], [AC08.AAE837], [AC08.AAE838], [AC08.AAE839], [AC08.AAE056], [AC08.AAE887], [AC08.AAE026], [AC08.AAE828], [AC08.AIC380], [AC08.AAE834], [AC08.AAE845], [AC08.AAE844], [AC08.AAB191], [AC08.AAE080], [AC08.AAE081], [AC08.AAE082], [AC08.AAE083], [AC08.AAE788], [AC08.AAE789], [AC08.AAE790], [AC08.AAE791], [AC08.AAE840], [AC08.AAE841], [AC08.AAE842], [AC08.AAE843], [AC08.AAE057], [AC08.AAE888], [AC08.AAE086], [AC08.AAE741], [AC08.AAE202], [AC08.AAA042], [AC08.AAA043], [AC08.AAA041], [AC08.AAA045], [AC08.AAZ616], [AC08.AAZ615], [AC08.AAE061], [AC08.AAE108], [AC08.AAB301], [AC08.AAB299], [AC08.AAE729], [AC08.AAE819], [AC08.AAE820], [AC08.AAE796], [AC08.AAE797], [AC08.AAE798], [AC08.AAE799], [AC08.AAE800], [AC08.AAZ625], [AC08.AAE150], [AC08.AAE066], [AC08.AAE748], [AC08.AAE822], [AC08.AAZ631], [AC08.AAZ650], [AC08.AAC323], [AC08.AAE013], [AC08.AAZ649], [AC08.AAE860], [AC08.AAE859], [AC08.AAE011], [AC08.AAZ692], [AC08.AAE036], [AC08.AAB034], [AC08.AAB360], [AC08.AAB359], [AC08.AAF018], [AC08.AAA431], [AC08.AAZ673], [AC08.AAA027], [AC08.AAA508], [AC08.AAA350]), partitions(p0)
  4 - output([1]), filter(nil), 
      access([VIEW1.B.AAZ686])
  5 - output([B.AAZ686]), filter([B.AAC001 = 1026005878]), 
      access([B.AAC001], [B.AAZ686]), partitions(p0)

执行计划中的PX消失了(你可以理解为跨界点访问),说明数据访问,关联都在一个节点完成
最终SQL由每次0.5秒-2秒优化到每次0.05秒,性能提升了10倍
算上每天跑20w次,平均耗费20%的CPU资源,那么这一个点的优化就可以将CPU资源从20%降低到2%

最后,有些小伙伴会有疑问,你咋知道SQL的NOT EXISTS应该加AC08.AAC001=B.AAC001过滤条件?原因有2个

1. 业务逻辑反推(得有开发&业务思维,纯运维DBA可能没这个思维)
2. AAC001是分区key

还有些小伙伴可能还有疑问,如果没有开发&业务思维怎么办? 也可以写个SQL检查AAC001与AAZ686的对应关系

select  *
  from (select AAZ686, count(*) cnt
          from (select AAZ686, AAC001 from ac08 group by AAZ686, AAC001)
         group by AAZ686
         order by 2 desc)
 where rownum <= 10;

如果CNT都是1就证明了我们的逻辑反推,如果CNT>1,得和业务确认,看看表中数据是否有问题

讲到这里还遇到点插曲,最开始查询的时候确实是1,过了2周我准备写博客了,再去查询发现CNT变成2了,把我吓了一跳
最终找到开发商,和业务确认,数据有错误。搞优化的同时帮开发商发现了数据质量问题,这尼玛...
 

以上是关于利用业务逻辑+OB分布式特性优化SQL的主要内容,如果未能解决你的问题,请参考以下文章

MapReduce计算框架高级特性程序运行并发度

Oceanus的实时流式计算实践与优化

JMeter性能测试中控制业务比例

hive优化分享

redis 业务锁 not exist 模式

一文教你迅速解决分布式事务 XA 一致性问题