使用python進行Oracle資料庫效能趨勢分析

sb5533發表於2018-06-14

一、   概述



隨著資訊系統業務需求快速增長,業務系統關聯日益複雜,資訊系統效能問題逐漸突顯,一旦出現資訊系統效能問題及不可用問題,將嚴重影響資訊系統的穩定執行及使用者體驗。

     結合運維實踐,資料庫效能問題是造成資訊系統效能下降和非停的重要原因之一,如何進行常態化的資料庫效能趨勢分析,及時發現資料庫效能衰減“病灶”,常態化提升資訊系統效能,避免救火式效能最佳化,成為衡量資訊系統管理部門運營能力的重要指標之一。

二、研究目標



使用python語言進行Oracle資料庫效能趨勢分析。

三、工具介紹



開發語言:python 2.7

資料庫:Oracle 11.2.0.4

Web框架:Django

圖形展示工具:echart

四、演算法介紹



       核心演算法由執行可靠率、資源競爭率、程式等待率和SQL穩定率四部分組成,如下圖所示,本文主要以SQL穩定率為例:

Trend =100-100*sum(( c_time-h_time) /h_time)

說明:

Trend: 表示資訊系統效能趨勢(%)

c_time: 前一小時SQL平均執行時間()

h_time: 3個月內SQL平均執行時間()

 

五、效果展示



(1)、系統效能趨勢:


(2)TOPSQL效能趨勢分析


(3)TOPSQL日效能趨勢分析


(4)TOPSQL月效能趨勢分析


 

六、核心程式碼



核心程式碼分為資料採集層、資料轉換層、web展示層。

(1)、資料採集層:


點選(此處)摺疊或開啟

  1. def get_topsql_info(username,password,ip,port,dbname,c_type,param=0,b_param=0):

  2.     s_top10 = ''

  3.     #s_snap_id = 0

  4.     print oracle_link_target

  5.    

  6.     if c_type == 'sql_topsql':

  7.          sql_topsql="

  8.          select round(Elapsed_Time, 2) Elapsed_Time,

  9.        round(cpu_time, 2) cpu_time,

  10.        Executions,

  11.        round(elap_per_exec, 2) elap_per_exec,

  12.        round(total_db_time, 2) total_db_time,

  13.        sql_id,

  14.        substr(nvl(sql_module, ' ** SQL module Not Available ** '), 1, 30) sql_module,

  15.        sql_text

  16.   from (select nvl((sqt.elap / 1000000), to_number(null)) Elapsed_Time,

  17.                nvl((sqt.cput / 1000000), to_number(null)) CPU_Time,

  18.                sqt.exec Executions,

  19.                decode(sqt.exec,

  20.                       0,

  21.                       to_number(null),

  22.                       (sqt.elap / sqt.exec / 1000000)) Elap_per_Exec,

  23.                (100 *

  24.                (sqt.elap /

  25.                (SELECT sum(e.VALUE) - sum(b.value)

  26.                     FROM DBA_HIST_SYS_TIME_MODEL e, DBA_HIST_SYS_TIME_MODEL b

  27.                    WHERE B.SNAP_ID = "+str(b_param)+"

  28.                      AND E.SNAP_ID = "+str(param)+"

  29.                      AND B.DBID = (select dbid from v$database)

  30.                      AND E.DBID = (select dbid from v$database)

  31.                      AND B.INSTANCE_NUMBER =

  32.                          (select instance_number from v$instance)

  33.                      AND E.INSTANCE_NUMBER =

  34.                          (select instance_number from v$instance)

  35.                      and e.STAT_NAME = 'DB time'

  36.                      and b.stat_name = 'DB time'))) Total_DB_Time,

  37.                sqt.sql_id,

  38.                to_char(decode(sqt.module,

  39.                               null,

  40.                               null,

  41.                               'Module: ' || sqt.module)) SQL_Module,

  42.                nvl(to_char(substr(st.sql_text, 1, 30)),

  43.                    ' ** SQL Text Not Available ** ') SQL_Text

  44.           from (select sql_id,

  45.                        max(module) module,

  46.                        sum(elapsed_time_delta) elap,

  47.                        sum(cpu_time_delta) cput,

  48.                        sum(executions_delta) exec

  49.                   from dba_hist_sqlstat

  50.                dba_hist_sqltext st

  51.          where st.sql_id(+) = sqt.sql_id

  52.          order by nvl(sqt.elap, -1) desc, sqt.sql_id)

  53.  where rownum < 100

  54.          "

  55.     elif c_type == 'top10':

  56.          #a list of top10: m_top10

  57.          m_top10=get_hsql_info(t,'top10')

  58.         

  59.          #after get top10

  60.         

  61.          #end get top10

  62.          for h_sql_id in m_top10:

  63.              l_sql_id = h_sql_id[0]

  64.              s_top10 = s_top10+",'"+l_sql_id+"'"

  65.          s_top10 = s_top10.strip(',')

  66.          sql_hsql_top10="select sql_id,to_char(substr(sql_text,1,2000)) sql_text,length(sql_text) sql_length,command_type from dba_hist_sqltext t where t.sql_id in ("+s_top10+')'

  67.     else:

  68.          cmd=sql_tablespace

  69.    

  70.     #print s_top10

  71.     #print log_cmd_i

  72.  

  73.     cmd =""

  74.     if c_type == 'sql_topsql':

  75.         cmd=sql_topsql

  76.     elif c_type == 'top10':

  77.         cmd=sql_hsql_top10

  78.     else:

  79.         cmd=sql_tablespace

  80.     #print len(m_top10)

  81.    

  82.     print 'before get topsql exe sql: '

  83.     print cmd

  84.     print 'get db shell: '

  85.     conn = cx_Oracle.connect(oracle_link_target)

  86.     cursor = conn.cursor()

  87.     cur = cursor.execute(cmd)

  88.     db_list = cur.fetchall()

  89.     #print 'before return db_list'

  90.     #print db_list

  91.     return db_list

  92.       

  93.     cursor.close()

  94. conn.close()


 

(2)、資料轉換層


點選(此處)摺疊或開啟

  1. select row_number() over(partition by ip order by to_number(total_db_time) desc) rn,

  2.                ip,

  3.                db_name,

  4.                sql_id,

  5.                decode(elap_per_exec, '0', 0.01, elap_per_exec) elap_per_exec,

  6.                decode(elap_avg_exec, '0', 0.01, elap_avg_exec) elap_avg_exec,

  7.                decode(sign(decode(elap_avg_exec, '0', 0.01, elap_avg_exec) - decode(elap_per_exec, '0', 0.01, elap_per_exec)),

  8.                       1,

  9.                       'up',

  10.                       -1,

  11.                       'down',

  12.                       'equ') sql_status,

  13.                round((decode(elap_avg_exec, '0', 0.01, elap_avg_exec) -

  14.                      decode(elap_per_exec, '0', 0.01, elap_per_exec)) /

  15.                      decode(elap_avg_exec, '0', 0.01, elap_avg_exec),

  16.                      2) sql_cont,

  17.                executions,

  18.                total_db_time,

  19.                substr(sql_module, 1, 12) sql_module,

  20.                substr(sql_text, 1, 12) sql_text,

  21.                ch_date

  22.           from (select rownum rn,

  23.                        d.ip,

  24.                        d.db_name,

  25.                        d.sql_id,

  26.                        replace(d.elap_per_exec, 'None', 0) elap_per_exec,

  27.                        e.elap_avg_exec,

  28.                        d.executions,

  29.                        d.sql_module,

  30.                        d.sql_text,

  31.                        d.ch_date,

  32.                        d.total_db_time

  33.                   from hsql.h_topsql d,

  34.                        (select b.ip,

  35.                                b.sql_id,

  36.                                round(avg(replace(b.elap_per_exec, 'None', 0)),

  37.                                      2) elap_avg_exec

  38.                           from hsql.h_topsql_bak b

  39.                          group by b.ip, b.sql_id) e

  40.                  where d.sql_id = e.sql_id

  41.                    and d.ip = e.ip)));


 

(3)web展示層


點選(此處)摺疊或開啟

  1. def topsql_line_servlet(request):

  2.          cursor = conn.cursor()

  3.          query = "select ip,

  4.        (select service_name

  5.           from hsql.h_instance h

  6.          where h.ip = b.ip

  7.            and rownum = 1) service_name,

  8.        sql_id,

  9.        executions,

  10.        elap_per_exec,

  11.        to_char(ch_date, 'hh24:mi') sj,

  12.        to_char(ch_date, 'yyyy-mm-dd') rq

  13.   from hsql.h_topsql b

  14.  where ch_date > trunc(sysdate)

  15.  order by sj"

  16.         

  17.          print query

  18.          cursor.execute(query)

  19.          resultset = cursor.fetchall()

  20.          cursor.close()

  21.          conn.close()


 

七、總結



       透過Oracle效能趨勢分析工具的應用可以進行細粒度的資料庫效能管理,及時發現潛在的資訊系統效能衰減隱患,透過持續性、常態化的資訊系統效能最佳化,最佳化資訊系統提升,提升使用者體驗。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31442014/viewspace-2151671/,如需轉載,請註明出處,否則將追究法律責任。

相關文章