Loading

OceanBase获取执行效率下降SQL

  移动现场有一个原厂写的通过gv$plan_cache_plan_stat视图来查询执行计划突变的SQL,通过比较gv$plan_cache_plan_stat视图记录的不同执行计划来判断是否执行计划发生突变而导致的执行计划变差,但是在gv$plan_cache_plan_stat视图中只有一个执行计划的时候,且这个执行计划的效率低下,无法对执行计划进行flush刷新,为了解决这个问题,可以通过ocp中的ocp_monitor数据库中的相关视图来判断进行的TOPSQL较昨天是否出现性能回退,如果出现可以记录并flush,这里仅贴出如何找到执行计划较昨天性能出现回退的脚本

复制代码
## Copyright (c) 2013-2024
## Author: lihanghang@shsnc.com     
## Version:      V1.0
## History:
## date:   20240619 
## 针对获取执行计划相较昨天变差SQL,并记录
##################################################################################################
#!/bin/bash
set +x
#DIR_HOME=$(cd "$(dirname "$0")"; pwd)

#LogDaily=$DIR_HOME/worse_sql_`hostname`.log

while [ 1 ]
do
  DATE=`date +%Y%m%d`
  LogDaily=/home/shsnc/monitor/log/wrose_sql_`hostname`_$DATE.log
  TIME1=`date +"%Y-%m-%d %H:%M:%S"`
  TIME2=`date -d "-1800 second" +"%Y-%m-%d %H:%M:%S"`
  TIME3=`date -d "-90000 second" +"%Y-%m-%d %H:%M:%S"`
  TIME4=`date -d "-82800 second" +"%Y-%m-%d %H:%M:%S"`
  echo "$TIME1 $TIME2 $TIME3 $TIME4"
  echo "#### $TIME1  数据库最近30分钟SQL执行计划相较昨天变差SQL信息####">> $LogDaily
mysql -hxxxx -P3306 -uroot@ocp_monitor#obcluster -pH@I8%Yl+FA -Docp_monitor -A -c -t<<!|tee -a $LogDaily
  select * from (select /*+parallel(32)*/
         a.sql_id,sum(a.executions) execs,
         round(sum(a.elapsed_time / 1000), 2) "total",(select avg_exe_usec from (select sql_id, plan_id, avg(avg_exe_usec / 1000) avg_exe_usec
          from ob_hist_plan_cache_plan_stat_0
         where collect_time > time_to_usec('$TIME3')
           and collect_time < time_to_usec('$TIME4')
           and ob_tenant_id = xxx
           and ob_cluster_id = 1632654686 and sql_id=a.sql_id
         group by 1, 2
         order by 3 desc limit 1)) last_avg_elapsed,
         round(sum(a.elapsed_time) / sum(a.executions) / 1000, 2) today_avg_elapsed
          from ob_hist_sql_audit_stat_0 a
         where a.ob_cluster_id = 1632654686
           and a.begin_interval_time > time_to_usec('$TIME2')
           and a.begin_interval_time < time_to_usec('$TIME1')
           and a.ob_tenant_id = xxx
           and a.cluster_name = 'oxxx'
           and ob_server_id in (3, 4) and retry_cnt=0
         group by a.sql_id
         order by 3 desc limit 100) where   today_avg_elapsed-last_avg_elapsed>500 and last_avg_elapsed<1000; 
!
  echo
  sleep 120
done
复制代码

 

作者:hanglinux

出处:https://www.cnblogs.com/hanglinux/p/18279477

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   李行行  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题