中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mysql的性能優化工具(源自韓鋒大師,針對5.7修改)

發布時間:2020-08-10 12:11:43 來源:ITPUB博客 閱讀:436 作者:mcszycc 欄目:MySQL數據庫
    韓鋒大師分享了一個mysql的性能優化工具,一個能自動采集SQL優化相關各種信息的python腳本,對于優化人員而言,這能省不少事,贊!
    試用了一下,發現在mysql5.7下,運行不起來,細節上還是有好幾個坑的,費了一些周折,終于把坑踩完了,現在把細節說明一下,并把修改后的SQL分享出來;
   問題1:調用腳本時,若SQL是用單引號包含(韓老師就是這么示范的:python mysql_tuning.py -p tuning_sql.ini -s 'select xxx),但這樣會報錯,解決辦法:用雙引號分隔,如:python mysql_tuning.py -p tuning_sql.ini -s "select * from employees.dept_emp"這樣就沒問題;

   問題2:沒有引用string單元,在使用string.atoi時會報錯,解決辦法:import string;

    問題3:mysql5.7后,infomation_schema的幾個表INFORMATION_SCHEMA.GLOBAL_VARIABLES、INFORMATION_SCHEMA.SESSION_VARIABLES、   INFORMATION_SCHEMA.SESSION_STATUS要替換成performance_schema下的;

   問題4:在顯示執行計劃時,table與type也是有可能是NULL的,要做空值處理,另外沒有顯示partitions欄位;

   問題5:p_after_status[key]有可能是小數,所以用int去轉換會報錯,需要用float;

   問題6:db_name顯示成user_pwd了,這個或者不算問題;

   修改后的腳本如下:

點擊(此處)折疊或打開

  1. #!/usr/local/bin/python
  2. import datetime
  3. import getopt
  4. import sys
  5. import string
  6. import pprint
  7. from warnings import filterwarnings
  8. import MySQLdb
  9. import ConfigParser
  10. import sqlparse
  11. from sqlparse.sql import IdentifierList, Identifier
  12. from sqlparse.tokens import Keyword, DML

  13. filterwarnings('ignore', category = MySQLdb.Warning)

  14. seq1="+"
  15. seq2="-"
  16. seq3="|"

  17. SYS_PARM_FILTER = (
  18.     'BINLOG_CACHE_SIZE',
  19.     'BULK_INSERT_BUFFER_SIZE',
  20.     'HAVE_PARTITION_ENGINE',
  21.     'HAVE_QUERY_CACHE',
  22.     'INTERACTIVE_TIMEOUT',
  23.     'JOIN_BUFFER_SIZE',
  24.     'KEY_BUFFER_SIZE',
  25.     'KEY_CACHE_AGE_THRESHOLD',
  26.     'KEY_CACHE_BLOCK_SIZE',
  27.     'KEY_CACHE_DIVISION_LIMIT',
  28.     'LARGE_PAGES',
  29.     'LOCKED_IN_MEMORY',
  30.     'LONG_QUERY_TIME',
  31.     'MAX_ALLOWED_PACKET',
  32.     'MAX_BINLOG_CACHE_SIZE',
  33.     'MAX_BINLOG_SIZE',
  34.     'MAX_CONNECT_ERRORS',
  35.     'MAX_CONNECTIONS',
  36.     'MAX_JOIN_SIZE',
  37.     'MAX_LENGTH_FOR_SORT_DATA',
  38.     'MAX_SEEKS_FOR_KEY',
  39.     'MAX_SORT_LENGTH',
  40.     'MAX_TMP_TABLES',
  41.     'MAX_USER_CONNECTIONS',
  42.     'OPTIMIZER_PRUNE_LEVEL',
  43.     'OPTIMIZER_SEARCH_DEPTH',
  44.     'QUERY_CACHE_SIZE',
  45.     'QUERY_CACHE_TYPE',
  46.     'QUERY_PREALLOC_SIZE',
  47.     'RANGE_ALLOC_BLOCK_SIZE',
  48.     'READ_BUFFER_SIZE',
  49.     'READ_RND_BUFFER_SIZE',
  50.     'SORT_BUFFER_SIZE',
  51.     'SQL_MODE',
  52.     'TABLE_CACHE',
  53.     'THREAD_CACHE_SIZE',
  54.     'TMP_TABLE_SIZE',
  55.     'WAIT_TIMEOUT'
  56. )

  57. def is_subselect(parsed):
  58.     if not parsed.is_group():
  59.         return False
  60.     for item in parsed.tokens:
  61.         if item.ttype is DML and item.value.upper() == 'SELECT':
  62.             return True
  63.     return False

  64. def extract_from_part(parsed):
  65.     from_seen = False
  66.     for item in parsed.tokens:
  67.         #print item.ttype,item.value
  68.         if from_seen:
  69.             if is_subselect(item):
  70.                 for x in extract_from_part(item):
  71.                     yield x
  72.             elif item.ttype is Keyword:
  73.                 raise StopIteration
  74.             else:
  75.                 yield item
  76.         elif item.ttype is Keyword and item.value.upper() == 'FROM':
  77.             from_seen = True

  78. def extract_table_identifiers(token_stream):
  79.     for item in token_stream:
  80.         if isinstance(item, IdentifierList):
  81.             for identifier in item.get_identifiers():
  82.                 yield identifier.get_real_name()
  83.         elif isinstance(item, Identifier):
  84.             yield item.get_real_name()
  85.         # It's a bug to check for Keyword here, but in the example
  86.         # above some tables names are identified as keywords...
  87.         elif item.ttype is Keyword:
  88.             yield item.value

  89. def extract_tables(p_sqltext):
  90.     stream = extract_from_part(sqlparse.parse(p_sqltext)[0])
  91.     return list(extract_table_identifiers(stream))

  92. def f_find_in_list(myList,value):
  93.     try:
  94.         for v in range(0,len(myList)):
  95.             if value==myList[v]:
  96.                 return 1
  97.         return 0
  98.     except:
  99.         return 0

  100. def f_get_parm(p_dbinfo):
  101.     conn = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  102.     cursor = conn.cursor()
  103.     cursor.execute("select lower(variable_name),variable_value from performance_schema.global_variables where upper(variable_name) in ('"+"','".join(list(SYS_PARM_FILTER))+"') order by variable_name")
  104.     records = cursor.fetchall()
  105.     cursor.close()
  106.     conn.close()
  107.     return records

  108. def f_print_parm(p_parm_result):
  109.     print "===== SYSTEM PARAMETER ====="
  110.     status_title=('parameter_name','value')
  111.     print "+--------------------------------+------------------------------------------------------------+"
  112.     print seq3,status_title[0].center(30),
  113.     print seq3,status_title[1].center(58),seq3
  114.     print "+--------------------------------+------------------------------------------------------------+"

  115.     for row in p_parm_result:
  116.     print seq3,row[0].ljust(30),
  117.         if 'size' in row[0]:
  118.             if string.atoi(row[1])>=1024*1024*1024:
  119.                 print seq3,(str(round(string.atoi(row[1])/1024/1024/1024,2))+' G').rjust(58),seq3
  120.             elif string.atoi(row[1])>=1024*1024:
  121.                 print seq3,(str(round(string.atoi(row[1])/1024/1024,2))+' M').rjust(58),seq3
  122.             elif string.atoi(row[1])>=1024:
  123.                 print seq3,(str(round(string.atoi(row[1])/1024,2))+' K').rjust(58),seq3
  124.             else:
  125.                 print seq3,(row[1]+' B').rjust(58),seq3
  126.         else:
  127.             print seq3,row[1].rjust(58),seq3
  128.     print "+--------------------------------+------------------------------------------------------------+"
  129.     print

  130. def f_print_optimizer_switch(p_dbinfo):
  131.     print "===== OPTIMIZER SWITCH ====="
  132.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  133.     cursor = db.cursor()
  134.     cursor.execute("select variable_value from performance_schema.global_variables where upper(variable_name)='OPTIMIZER_SWITCH'")
  135.     rows = cursor.fetchall()
  136.     print "+------------------------------------------+------------+"
  137.     print seq3,'switch_name'.center(40),
  138.     print seq3,'value'.center(10),seq3
  139.     print "+------------------------------------------+------------+"
  140.     for row in rows[0][0].split(','):
  141.         print seq3,row.split('=')[0].ljust(40),
  142.         print seq3,row.split('=')[1].rjust(10),seq3
  143.     print "+------------------------------------------+------------+"
  144.     cursor.close()
  145.     db.close()
  146.     print

  147. def f_exec_sql(p_dbinfo,p_sqltext,p_option):
  148.     results={}
  149.     conn = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  150.     cursor = conn.cursor()

  151.     if f_find_in_list(p_option,'PROFILING'):
  152.         cursor.execute("set profiling=1")
  153.         cursor.execute("select ifnull(max(query_id),0) from INFORMATION_SCHEMA.PROFILING")
  154.         records = cursor.fetchall()
  155.         query_id=records[0][0] +2 #skip next sql

  156.     if f_find_in_list(p_option,'STATUS'):
  157.         #cursor.execute("select concat(upper(left(variable_name,1)),substring(lower(variable_name),2,(length(variable_name)-1))) var_name,variable_value var_value from performance_schema.session_status where variable_name in('"+"','".join(tuple(SES_STATUS_ITEM))+"') order by 1")
  158.         cursor.execute("select concat(upper(left(variable_name,1)),substring(lower(variable_name),2,(length(variable_name)-1))) var_name,variable_value var_value from performance_schema.session_status order by 1")
  159.         records = cursor.fetchall()
  160.         results['BEFORE_STATUS']=dict(records)

  161.     cursor.execute(p_sqltext)

  162.     if f_find_in_list(p_option,'STATUS'):
  163.         cursor.execute("select concat(upper(left(variable_name,1)),substring(lower(variable_name),2,(length(variable_name)-1))) var_name,variable_value var_value from performance_schema.session_status order by 1")
  164.         records = cursor.fetchall()
  165.         results['AFTER_STATUS']=dict(records)

  166.     if f_find_in_list(p_option,'PROFILING'):
  167.         cursor.execute("select STATE,DURATION,CPU_USER,CPU_SYSTEM,BLOCK_OPS_IN,BLOCK_OPS_OUT ,MESSAGES_SENT ,MESSAGES_RECEIVED ,PAGE_FAULTS_MAJOR ,PAGE_FAULTS_MINOR ,SWAPS from INFORMATION_SCHEMA.PROFILING where query_id="+str(query_id)+" order by seq")
  168.         records = cursor.fetchall()
  169.         results['PROFILING_DETAIL']=records

  170.         cursor.execute("SELECT STATE,SUM(DURATION) AS Total_R,ROUND(100*SUM(DURATION)/(SELECT SUM(DURATION) FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID="+str(query_id)+"),2) AS Pct_R,COUNT(*) AS Calls,SUM(DURATION)/COUNT(*) AS R_Call FROM INFORMATION_SCHEMA.PROFILING WHERE QUERY_ID="+str(query_id)+" GROUP BY STATE ORDER BY Total_R DESC")
  171.         records = cursor.fetchall()
  172.         results['PROFILING_SUMMARY']=records

  173.     cursor.close()
  174.     conn.close()
  175.     return results

  176. def f_print_status(p_before_status,p_after_status):
  177.     print "===== SESSION STATUS (DIFFERENT) ====="
  178.     status_title=('status_name','before','after','diff')
  179.     print "+-------------------------------------+-----------------+-----------------+-----------------+"
  180.     print seq3,status_title[0].center(35),
  181.     print seq3,status_title[1].center(15),
  182.     print seq3,status_title[2].center(15),
  183.     print seq3,status_title[3].center(15),seq3
  184.     print "+-------------------------------------+-----------------+-----------------+-----------------+"

  185.     for key in sorted(p_before_status.keys()):
  186.         if p_before_status[key]<>p_after_status[key]:
  187.             print seq3,key.ljust(35),
  188.             print seq3,p_before_status[key].rjust(15),
  189.             print seq3,p_after_status[key].rjust(15),
  190.             print seq3,str(float(p_after_status[key])-float(p_before_status[key])).rjust(15),seq3
  191.     print "+-------------------------------------+-----------------+-----------------+-----------------+"
  192.     print

  193. def f_print_time(p_starttime,p_endtime):
  194.     print "===== EXECUTE TIME ====="
  195.     print timediff(p_starttime,p_endtime)
  196.     print


  197. def f_print_profiling(p_profiling_detail,p_profiling_summary):
  198.     print "===== SQL PROFILING(DETAIL)====="
  199.     status_title=('state','duration','cpu_user','cpu_sys','bk_in','bk_out','msg_s','msg_r','p_f_ma','p_f_mi','swaps')
  200.     print "+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+"
  201.     print seq3,status_title[0].center(30),
  202.     print seq3,status_title[1].center(8),
  203.     print seq3,status_title[2].center(8),
  204.     print seq3,status_title[3].center(8),
  205.     print seq3,status_title[4].center(8),
  206.     print seq3,status_title[5].center(8),
  207.     print seq3,status_title[6].center(8),
  208.     print seq3,status_title[7].center(8),
  209.     print seq3,status_title[8].center(8),
  210.     print seq3,status_title[9].center(8),
  211.     print seq3,status_title[10].center(8),seq3
  212.     print "+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+"

  213.     for row in p_profiling_detail:
  214.         print seq3,row[0].ljust(30),
  215.         print seq3,str(row[1]).rjust(8),
  216.         print seq3,str(row[2]).rjust(8),
  217.         print seq3,str(row[3]).rjust(8),
  218.         print seq3,str(row[4]).rjust(8),
  219.         print seq3,str(row[5]).rjust(8),
  220.         print seq3,str(row[6]).rjust(8),
  221.         print seq3,str(row[7]).rjust(8),
  222.         print seq3,str(row[8]).rjust(8),
  223.         print seq3,str(row[9]).rjust(8),
  224.         print seq3,str(row[10]).rjust(8),seq3
  225.     print "+--------------------------------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+"
  226.     print 'bk_in: block_ops_in'
  227.     print 'bk_out: block_ops_out'
  228.     print 'msg_s: message sent'
  229.     print 'msg_r: message received'
  230.     print 'p_f_ma: page_faults_major'
  231.     print 'p_f_mi: page_faults_minor'
  232.     print

  233.     print "===== SQL PROFILING(SUMMARY)====="
  234.     status_title=('state','total_r','pct_r','calls','r/call')
  235.     print "+-------------------------------------+-----------------+------------+-------+-----------------+"
  236.     print seq3,status_title[0].center(35),
  237.     print seq3,status_title[1].center(15),
  238.     print seq3,status_title[2].center(10),
  239.     print seq3,status_title[3].center(5),
  240.     print seq3,status_title[4].center(15),seq3
  241.     print "+-------------------------------------+-----------------+------------+-------+-----------------+"

  242.     for row in p_profiling_summary:
  243.         print seq3,row[0].ljust(35),
  244.         print seq3,str(row[1]).rjust(15),
  245.         print seq3,str(row[2]).rjust(10),
  246.         print seq3,str(row[3]).rjust(5),
  247.         print seq3,str(row[4]).rjust(15),seq3
  248.     print "+-------------------------------------+-----------------+------------+-------+-----------------+"
  249.     print

  250. def f_get_sqlplan(p_dbinfo,p_sqltext):
  251.     results={}

  252.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  253.     cursor = db.cursor()
  254.     cursor.execute("explain extended "+p_sqltext)
  255.     records = cursor.fetchall()
  256.     results['SQLPLAN']=records
  257.     cursor.execute("show warnings")
  258.     records = cursor.fetchall()
  259.     results['WARNING']=records
  260.     cursor.close()
  261.     db.close()
  262.     return results

  263. def f_print_sqlplan(p_sqlplan,p_warning):
  264.     plan_title=('id','select_type','table','partitions','type','possible_keys','key','key_len','ref','rows','filtered','Extra')

  265.     print "===== SQL PLAN ====="
  266.     print "+--------+------------------+------------+------------+------------+---------------+------------+------------+------------+------------+------------+------------+"
  267.     print seq3,plan_title[0].center(6),
  268.     print seq3,plan_title[1].center(16),
  269.     print seq3,plan_title[2].center(10),
  270.     print seq3,plan_title[3].center(10),
  271.     print seq3,plan_title[4].center(10),
  272.     print seq3,plan_title[5].center(10),
  273.     print seq3,plan_title[6].center(10),
  274.     print seq3,plan_title[7].center(10),
  275.     print seq3,plan_title[8].center(10),
  276.     print seq3,plan_title[9].center(10),
  277.     print seq3,plan_title[10].center(10),
  278.     print seq3,plan_title[11].center(10),seq3
  279.     print "+--------+------------------+------------+------------+------------+---------------+------------+------------+------------+------------+------------+------------+"
  280.     for row in p_sqlplan:
  281.         print seq3,str(row[0]).rjust(6),         # id
  282.         print seq3,row[1].ljust(16), # select_type
  283.         #print seq3,row[2].ljust(10), # table
  284.         if not "NonyType" in str(type(row[2])):
  285.             print seq3,row[2].ljust(10),
  286.         else:
  287.             print seq3,"NULL".ljust(10),
  288.         
  289.         if not "NoneType" in str(type(row[3])): #partitions
  290.             print seq3,row[3].ljust(10),
  291.         else:
  292.             print seq3,"NULL".ljust(10),
  293.                               
  294.         #print seq3,row[3].ljust(10),
  295.         if not "NoneType" in str(type(row[4])): #type
  296.             print seq3,row[4].ljust(10),
  297.         else:
  298.             print seq3,"NULL".ljust(10),
  299.         
  300.         if not "NoneType" in str(type(row[5])): # possible_keys
  301.             print seq3,row[5].ljust(13),
  302.         else:
  303.             print seq3,"NULL".ljust(13),

  304.         if not "NoneType" in str(type(row[6])): # key
  305.             print seq3,row[6].ljust(10),
  306.         else:
  307.             print seq3,"NULL".ljust(10),
  308.         
  309.         if not "NoneType" in str(type(row[7])): # key_len
  310.             print seq3,row[7].ljust(10),
  311.         else:
  312.             print seq3,"NULL".ljust(10),

  313.         if not "NoneType" in str(type(row[8])): # ref
  314.             print seq3,row[8].ljust(10),
  315.         else:
  316.             print seq3,"NULL".ljust(10),

  317.         print seq3,str(row[9]).rjust(10), # rows

  318.         print seq3,str(row[10]).rjust(10), # filters

  319.         if not "NoneType" in str(type(row[11])): # Extra
  320.             print seq3,row[11].ljust(10),
  321.         else:
  322.             print seq3,"NULL".ljust(10),
  323.         print seq3

  324.     print "+--------+------------------+------------+------------+------------+---------------+------------+------------+------------+------------+------------+------------+"
  325.     print

  326.     print "===== OPTIMIZER REWRITE SQL ====="
  327.     for row in p_warning:
  328.         print sqlparse.format(row[2],reindent=True, keyword_case='upper',strip_comments=True)
  329.     print

  330. def f_get_table(p_dbinfo,p_sqltext):
  331.     r_tables=[]
  332.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  333.     cursor = db.cursor()
  334.     cursor.execute("explain "+p_sqltext)
  335.     rows = cursor.fetchall ()
  336.     for row in rows:
  337.         table_name = row[2]
  338.         if '<' in table_name:
  339.             continue
  340.         if len(r_tables)==0:
  341.             r_tables.append(table_name)
  342.         elif f_find_in_list(r_tables,table_name) == -1:
  343.             r_tables.append(table_name)
  344.     cursor.close()
  345.     db.close()
  346.     return r_tables

  347. def f_print_tableinfo(p_dbinfo,p_tablename):
  348.     plan_title=('table_name','engine','format','table_rows','avg_row','total_mb','data_mb','index_mb')
  349.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  350.     cursor = db.cursor()
  351.     stmt = "select engine,row_format as format,table_rows,avg_row_length as avg_row,round((data_length+index_length)/1024/1024,2) as total_mb,round((data_length)/1024/1024,2) as data_mb,round((index_length)/1024/1024,2) as index_mb from information_schema.tables where table_schema='"+p_dbinfo[3]+"' and table_name='"+p_tablename+"'"
  352.     cursor.execute(stmt)
  353.     rows = cursor.fetchall ()
  354.     print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  355.     print seq3,plan_title[0].center(15),
  356.     print seq3,plan_title[1].center(10),
  357.     print seq3,plan_title[2].center(10),
  358.     print seq3,plan_title[3].center(10),
  359.     print seq3,plan_title[4].center(10),
  360.     print seq3,plan_title[5].center(10),
  361.     print seq3,plan_title[6].center(10),
  362.     print seq3,plan_title[7].center(10),seq3
  363.     print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  364.     for row in rows:
  365.         print seq3,p_tablename.ljust(15),
  366.         print seq3,row[0].ljust(10),
  367.         print seq3,row[1].ljust(10),
  368.         print seq3,str(row[2]).rjust(10),
  369.         print seq3,str(row[3]).rjust(10),
  370.         print seq3,str(row[4]).rjust(10),
  371.         print seq3,str(row[5]).rjust(10),
  372.         print seq3,str(row[6]).rjust(10),seq3
  373.     print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  374.     cursor.close()
  375.     db.close()

  376. def f_print_indexinfo(p_dbinfo,p_tablename):
  377.     plan_title=('index_name','non_unique','seq_in_index','column_name','collation','cardinality','nullable','index_type')
  378.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  379.     cursor = db.cursor()
  380.     stmt = "select index_name,non_unique,seq_in_index,column_name,collation,cardinality,nullable,index_type from information_schema.statistics where table_schema='"+p_dbinfo[3]+"' and table_name='"+p_tablename+"' order by 1,3"
  381.     cursor.execute(stmt)
  382.     rows = cursor.fetchall ()
  383.     if len(rows)>0:
  384.         print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  385.         print seq3,plan_title[0].center(15),
  386.         print seq3,plan_title[1].center(15),
  387.         print seq3,plan_title[2].center(15),
  388.         print seq3,plan_title[3].center(15),
  389.         print seq3,plan_title[4].center(15),
  390.         print seq3,plan_title[5].center(15),
  391.         print seq3,plan_title[6].center(15),
  392.         print seq3,plan_title[7].center(15),seq3
  393.         print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  394.         for row in rows:
  395.             print seq3,row[0].ljust(15),
  396.             print seq3,str(row[1]).rjust(15),
  397.             print seq3,str(row[2]).rjust(15),
  398.             print seq3,str(row[3]).rjust(15),
  399.             print seq3,str(row[4]).rjust(15),
  400.             print seq3,str(row[5]).rjust(15),
  401.             print seq3,str(row[6]).rjust(15),
  402.             print seq3,str(row[7]).rjust(15),seq3
  403.         print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  404.     cursor.close()
  405.     db.close()

  406. def f_get_mysql_version(p_dbinfo):
  407.     db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  408.     cursor = db.cursor()
  409.     cursor.execute("select @@version")
  410.     records = cursor.fetchall ()
  411.     cursor.close()
  412.     db.close()
  413.     return records[0][0]

  414. def f_print_title(p_dbinfo,p_mysql_version,p_sqltext):
  415.     print '*'*100
  416.     print '*','MySQL SQL Tuning Tools v1.0 for mysql5.7(created by hanfeng modified by ycc)'.center(96),'*'
  417.     print '*'*100

  418.     print
  419.     print "===== BASIC INFORMATION ====="
  420.     title=('server_ip','user_name','db_name','db_version')
  421.     print "+----------------------+------------+------------+------------+"
  422.     print seq3,title[0].center(20),
  423.     print seq3,title[1].center(10),
  424.     print seq3,title[2].center(10),
  425.     print seq3,title[3].center(10),seq3
  426.     print "+----------------------+------------+------------+------------+"
  427.     print seq3,p_dbinfo[0].center(20),
  428.     print seq3,p_dbinfo[1].center(10),
  429.     print seq3,p_dbinfo[3].center(10),
  430.     print seq3,p_mysql_version.center(10),seq3
  431.     print "+----------------------+------------+------------+------------+"
  432.     print
  433.     print "===== ORIGINAL SQL TEXT ====="
  434.     print sqlparse.format(p_sqltext,reindent=True, keyword_case='upper')
  435.     print

  436. '''
  437. def f_print_table(p_value,p_option): #p_option "(key-n => title,max_len,align_value)"
  438.     for k in p_option.keys():
  439.         v = p_option[k]
  440.         print "+",
  441.         print int(v.split(',')[1])*"-",
  442.     print "+"
  443.     
  444.     for k in p_option.keys():
  445.         v = p_option[k]
  446.         print "|",
  447.         print v.split(',')[0].center(int(v.split(',')[0])-2),
  448.     print "|",

  449.     for k in p_option.keys():
  450.         v = p_option[k]
  451.         print "+",
  452.         print int(v.split(',')[1])*"-",
  453.     print "+"

  454.     for row in p_value:
  455.         k=0
  456.         for col in row:
  457.             k+=1
  458.             print "|",
  459.             if p_option[k].split(',')[2]=='l':
  460.                 print col.ljust(p_option[k].split(',')[1]),
  461.             elif p_option[k].split(',')[2]=='r':
  462.                 print col.rjust(p_option[k].split(',')[1]),
  463.             else
  464.                 print col.center(p_option[k].split(',')[1]),
  465.             print "|",

  466.     for k in p_option.keys():
  467.         v = p_option[k]
  468.         print "+",
  469.         print int(v.split(',')[1])*"-",
  470.     print "+"
  471. '''

  472. def timediff(timestart, timestop):
  473.         t = (timestop-timestart)
  474.         time_day = t.days
  475.         s_time = t.seconds
  476.         ms_time = t.microseconds / 1000000
  477.         usedtime = int(s_time + ms_time)
  478.         time_hour = usedtime / 60 / 60
  479.         time_minute = (usedtime - time_hour * 3600 ) / 60
  480.         time_second = usedtime - time_hour * 3600 - time_minute * 60
  481.         time_micsecond = (t.microseconds - t.microseconds / 1000000) / 1000

  482.         retstr = "%d day %d hour %d minute %d second %d microsecond " %(time_day, time_hour, time_minute, time_second, time_micsecond)
  483.         return retstr

  484. if __name__=="__main__":
  485.     dbinfo=["","","",""] #dbhost,dbuser,dbpwd,dbname
  486.     sqltext=""
  487.     option=[]
  488.     config_file=""
  489.     mysql_version=""

  490.     opts, args = getopt.getopt(sys.argv[1:], "p:s:")
  491.     for o,v in opts:
  492.         if o == "-p":
  493.             config_file = v
  494.         elif o == "-s":
  495.             sqltext = v

  496.     config = ConfigParser.ConfigParser()
  497.     config.readfp(open(config_file,"rb"))
  498.     dbinfo[0] = config.get("database","server_ip")
  499.     dbinfo[1] = config.get("database","db_user")
  500.     dbinfo[2] = config.get("database","db_pwd")
  501.     dbinfo[3] = config.get("database","db_name")

  502.     mysql_version = f_get_mysql_version(dbinfo)
  503.     
  504.     f_print_title(dbinfo,mysql_version,sqltext)

  505.     if config.get("option","sys_parm")=='ON':
  506.         parm_result = f_get_parm(dbinfo)
  507.         f_print_parm(parm_result)
  508.         f_print_optimizer_switch(dbinfo)

  509.     if config.get("option","sql_plan")=='ON':
  510.         sqlplan_result = f_get_sqlplan(dbinfo,sqltext)
  511.         f_print_sqlplan(sqlplan_result['SQLPLAN'],sqlplan_result['WARNING'])

  512.     if config.get("option","obj_stat")=='ON':
  513.         print "===== OBJECT STATISTICS ====="
  514.         for table_name in extract_tables(sqltext):
  515.             f_print_tableinfo(dbinfo,table_name)
  516.             f_print_indexinfo(dbinfo,table_name)
  517.         print

  518.     if config.get("option","ses_status")=='ON':
  519.         option.append('STATUS')

  520.     if config.get("option","sql_profile")=='ON':
  521.         option.append('PROFILING')

  522.     if config.get("option","ses_status")=='ON' or config.get("option","sql_profile")=='ON':
  523.         starttime = datetime.datetime.now()
  524.         exec_result = f_exec_sql(dbinfo,sqltext,option)
  525.         endtime = datetime.datetime.now()

  526.         if config.get("option","ses_status")=='ON':
  527.             f_print_status(exec_result['BEFORE_STATUS'],exec_result['AFTER_STATUS'])

  528.         if config.get("option","sql_profile")=='ON':
  529.             f_print_profiling(exec_result['PROFILING_DETAIL'],exec_result['PROFILING_SUMMARY


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

梨树县| 井研县| 罗山县| 都兰县| 成武县| 凤冈县| 遵义县| 清远市| 株洲市| 五莲县| 呼伦贝尔市| 婺源县| 武川县| 札达县| 连城县| 芷江| 连江县| 阳原县| 剑阁县| 定南县| 承德县| 新闻| 寿阳县| 阿巴嘎旗| 沭阳县| 沁水县| 交城县| 图木舒克市| 寻甸| 阿勒泰市| 海原县| 贵港市| 十堰市| 德清县| 南投市| 区。| 云龙县| 永春县| 灵寿县| 马鞍山市| 保山市|