千家信息网

mysql的性能优化工具(源自韩锋大师,针对5.7修改)

发表于:2024-11-19 作者:千家信息网编辑
千家信息网最后更新 2024年11月19日,韩锋大师分享了一个mysql的性能优化工具,一个能自动采集SQL优化相关各种信息的python脚本,对于优化人员而言,这能省不少事,赞!试用了一下,发现在mysql5.7下,运行不起来,细节上还是有好
千家信息网最后更新 2024年11月19日mysql的性能优化工具(源自韩锋大师,针对5.7修改)韩锋大师分享了一个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. if not "NoneType" in str(type(row[3])): #partitions
  289. print seq3,row[3].ljust(10),
  290. else:
  291. print seq3,"NULL".ljust(10),

  292. #print seq3,row[3].ljust(10),
  293. if not "NoneType" in str(type(row[4])): #type
  294. print seq3,row[4].ljust(10),
  295. else:
  296. print seq3,"NULL".ljust(10),

  297. if not "NoneType" in str(type(row[5])): # possible_keys
  298. print seq3,row[5].ljust(13),
  299. else:
  300. print seq3,"NULL".ljust(13),

  301. if not "NoneType" in str(type(row[6])): # key
  302. print seq3,row[6].ljust(10),
  303. else:
  304. print seq3,"NULL".ljust(10),

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

  309. if not "NoneType" in str(type(row[8])): # ref
  310. print seq3,row[8].ljust(10),
  311. else:
  312. print seq3,"NULL".ljust(10),

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

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

  315. if not "NoneType" in str(type(row[11])): # Extra
  316. print seq3,row[11].ljust(10),
  317. else:
  318. print seq3,"NULL".ljust(10),
  319. print seq3

  320. print "+--------+------------------+------------+------------+------------+---------------+------------+------------+------------+------------+------------+------------+"
  321. print

  322. print "===== OPTIMIZER REWRITE SQL ====="
  323. for row in p_warning:
  324. print sqlparse.format(row[2],reindent=True, keyword_case='upper',strip_comments=True)
  325. print

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

  343. def f_print_tableinfo(p_dbinfo,p_tablename):
  344. plan_title=('table_name','engine','format','table_rows','avg_row','total_mb','data_mb','index_mb')
  345. db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  346. cursor = db.cursor()
  347. 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+"'"
  348. cursor.execute(stmt)
  349. rows = cursor.fetchall ()
  350. print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  351. print seq3,plan_title[0].center(15),
  352. print seq3,plan_title[1].center(10),
  353. print seq3,plan_title[2].center(10),
  354. print seq3,plan_title[3].center(10),
  355. print seq3,plan_title[4].center(10),
  356. print seq3,plan_title[5].center(10),
  357. print seq3,plan_title[6].center(10),
  358. print seq3,plan_title[7].center(10),seq3
  359. print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  360. for row in rows:
  361. print seq3,p_tablename.ljust(15),
  362. print seq3,row[0].ljust(10),
  363. print seq3,row[1].ljust(10),
  364. print seq3,str(row[2]).rjust(10),
  365. print seq3,str(row[3]).rjust(10),
  366. print seq3,str(row[4]).rjust(10),
  367. print seq3,str(row[5]).rjust(10),
  368. print seq3,str(row[6]).rjust(10),seq3
  369. print "+-----------------+------------+------------+------------+------------+------------+------------+------------+"
  370. cursor.close()
  371. db.close()

  372. def f_print_indexinfo(p_dbinfo,p_tablename):
  373. plan_title=('index_name','non_unique','seq_in_index','column_name','collation','cardinality','nullable','index_type')
  374. db = MySQLdb.connect(host=p_dbinfo[0], user=p_dbinfo[1], passwd=p_dbinfo[2],db=p_dbinfo[3])
  375. cursor = db.cursor()
  376. 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"
  377. cursor.execute(stmt)
  378. rows = cursor.fetchall ()
  379. if len(rows)>0:
  380. print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  381. print seq3,plan_title[0].center(15),
  382. print seq3,plan_title[1].center(15),
  383. print seq3,plan_title[2].center(15),
  384. print seq3,plan_title[3].center(15),
  385. print seq3,plan_title[4].center(15),
  386. print seq3,plan_title[5].center(15),
  387. print seq3,plan_title[6].center(15),
  388. print seq3,plan_title[7].center(15),seq3
  389. print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  390. for row in rows:
  391. print seq3,row[0].ljust(15),
  392. print seq3,str(row[1]).rjust(15),
  393. print seq3,str(row[2]).rjust(15),
  394. print seq3,str(row[3]).rjust(15),
  395. print seq3,str(row[4]).rjust(15),
  396. print seq3,str(row[5]).rjust(15),
  397. print seq3,str(row[6]).rjust(15),
  398. print seq3,str(row[7]).rjust(15),seq3
  399. print "+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+-----------------+"
  400. cursor.close()
  401. db.close()

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

  410. def f_print_title(p_dbinfo,p_mysql_version,p_sqltext):
  411. print '*'*100
  412. print '*','MySQL SQL Tuning Tools v1.0 for mysql5.7(created by hanfeng modified by ycc)'.center(96),'*'
  413. print '*'*100

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

  432. '''
  433. def f_print_table(p_value,p_option): #p_option "(key-n => title,max_len,align_value)"
  434. for k in p_option.keys():
  435. v = p_option[k]
  436. print "+",
  437. print int(v.split(',')[1])*"-",
  438. print "+"

  439. for k in p_option.keys():
  440. v = p_option[k]
  441. print "|",
  442. print v.split(',')[0].center(int(v.split(',')[0])-2),
  443. print "|",

  444. for k in p_option.keys():
  445. v = p_option[k]
  446. print "+",
  447. print int(v.split(',')[1])*"-",
  448. print "+"

  449. for row in p_value:
  450. k=0
  451. for col in row:
  452. k+=1
  453. print "|",
  454. if p_option[k].split(',')[2]=='l':
  455. print col.ljust(p_option[k].split(',')[1]),
  456. elif p_option[k].split(',')[2]=='r':
  457. print col.rjust(p_option[k].split(',')[1]),
  458. else
  459. print col.center(p_option[k].split(',')[1]),
  460. print "|",

  461. for k in p_option.keys():
  462. v = p_option[k]
  463. print "+",
  464. print int(v.split(',')[1])*"-",
  465. print "+"
  466. '''

  467. def timediff(timestart, timestop):
  468. t = (timestop-timestart)
  469. time_day = t.days
  470. s_time = t.seconds
  471. ms_time = t.microseconds / 1000000
  472. usedtime = int(s_time + ms_time)
  473. time_hour = usedtime / 60 / 60
  474. time_minute = (usedtime - time_hour * 3600 ) / 60
  475. time_second = usedtime - time_hour * 3600 - time_minute * 60
  476. time_micsecond = (t.microseconds - t.microseconds / 1000000) / 1000

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

  479. if __name__=="__main__":
  480. dbinfo=["","","",""] #dbhost,dbuser,dbpwd,dbname
  481. sqltext=""
  482. option=[]
  483. config_file=""
  484. mysql_version=""

  485. opts, args = getopt.getopt(sys.argv[1:], "p:s:")
  486. for o,v in opts:
  487. if o == "-p":
  488. config_file = v
  489. elif o == "-s":
  490. sqltext = v

  491. config = ConfigParser.ConfigParser()
  492. config.readfp(open(config_file,"rb"))
  493. dbinfo[0] = config.get("database","server_ip")
  494. dbinfo[1] = config.get("database","db_user")
  495. dbinfo[2] = config.get("database","db_pwd")
  496. dbinfo[3] = config.get("database","db_name")

  497. mysql_version = f_get_mysql_version(dbinfo)

  498. f_print_title(dbinfo,mysql_version,sqltext)

  499. if config.get("option","sys_parm")=='ON':
  500. parm_result = f_get_parm(dbinfo)
  501. f_print_parm(parm_result)
  502. f_print_optimizer_switch(dbinfo)

  503. if config.get("option","sql_plan")=='ON':
  504. sqlplan_result = f_get_sqlplan(dbinfo,sqltext)
  505. f_print_sqlplan(sqlplan_result['SQLPLAN'],sqlplan_result['WARNING'])

  506. if config.get("option","obj_stat")=='ON':
  507. print "===== OBJECT STATISTICS ====="
  508. for table_name in extract_tables(sqltext):
  509. f_print_tableinfo(dbinfo,table_name)
  510. f_print_indexinfo(dbinfo,table_name)
  511. print

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

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

  516. if config.get("option","ses_status")=='ON' or config.get("option","sql_profile")=='ON':
  517. starttime = datetime.datetime.now()
  518. exec_result = f_exec_sql(dbinfo,sqltext,option)
  519. endtime = datetime.datetime.now()

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

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


0