利用mysql general log日志找出查询次数最多的SQL句子

卢松松博客 关注创业者、自媒体人和站长的网站

我们知道不管是apache,iis及mysql都可以通过日志能判断程序性能与错误定位了,今天我们来介绍利用mysql general log日志找出查询次数最多的SQL句子.

查询最多的sql语句,开启general log:

  1. mysql>showvariableslike'%general%';
  2. +------------------+-------------------------------------+
  3. |Variable_name|Value|
  4. +------------------+-------------------------------------+
  5. |general_log|OFF|
  6. |general_log_file|/usr/local/mysql/data/localhost.log|
  7. +------------------+-------------------------------------+
  8. mysql>setglobalgeneral_log="ON";

analysis-general-log.py脚本:

  1. #!/usr/bin/python
  2. #sortandcountmysqlgenerallog
  3. #Author:Jason
  4. #Created:UTC2015-02-1517:51:53
  5. importre
  6. importsys
  7. importos
  8. iflen(sys.argv)==2:
  9. logPath=sys.argv[1]
  10. ifnotos.path.exists(logPath):
  11. print("file"+logPath+"doesnotexists.")
  12. sys.exit(1)
  13. else:
  14. print("Usage:"+sys.argv[0]+"logPath")
  15. sys.exit(1)
  16. logFo=open(logPath)
  17. match=0
  18. forlineinlogFo:
  19. line=re.sub(r"\n","",line)
  20. ifmatch==0:
  21. #matchlinebeginwithnumbers
  22. lineMatch=re.match(r"\s+[0-9]+\s+.*",line,flags=re.I)
  23. iflineMatch:
  24. lineTmp=lineMatch.group(0)
  25. match=match+1
  26. continue
  27. elifmatch==1:
  28. #matchlinebeginwithnumbers
  29. lineMatch=re.match(r"\s+[0-9]+\s+.*",line,flags=re.I)
  30. iflineMatch:--phpfensi.com
  31. #matchonlyquery
  32. lineMatchQuery=re.match(r".*Query\s+(.*)",lineTmp,flags=re.I)
  33. iflineMatchQuery:
  34. lineTmp=lineMatchQuery.group(1)
  35. #removeextraspace
  36. lineTmp=re.sub(r"\s+","",lineTmp)
  37. #replacevalues(value)tovalues(x)
  38. lineTmp=re.sub(r"values\s*\(.*?\)","values(x)",lineTmp,flags=re.I)
  39. #replacefiled='value'tofiled='x'
  40. lineTmp=re.sub(r"(=|>|<|>=|<=)\s*('|\").*?\2","\\1'x'",lineTmp)
  41. #replacefiled=valuetofiled=x
  42. lineTmp=re.sub(r"(=|>|<|>=|<=)\s*[0-9]+","\\1x",lineTmp)
  43. #replacelike'value'tolike'x'
  44. lineTmp=re.sub(r"like\s+('|\").*?\1","like'x'",lineTmp,flags=re.I)
  45. #replacein(value)toin(x)
  46. lineTmp=re.sub(r"in\s+\(.*?\)","in(x)",lineTmp,flags=re.I)
  47. #replacelimitx,ytolimit
  48. lineTmp=re.sub(r"limit.*","limit",lineTmp,flags=re.I)
  49. print(lineTmp)
  50. match=1
  51. lineTmp=lineMatch.group(0)
  52. else:
  53. lineTmp+=line
  54. match=1
  55. logFo.close()

使用方法:

  1. analysis-general-log.pygeneral.log|sort|uniq-c|sort-nr
  2. 1032SELECT*FROMwp_commentsWHERE(comment_approved='x'ORcomment_approved='x')ANDcomment_post_ID=xORDERBYcomment_date_gmtDESC
  3. 653SELECTpost_id,meta_key,meta_valueFROMwp_postmetaWHEREpost_idin(x)ORDERBYmeta_idASC
  4. 527SELECTFOUND_ROWS()
  5. 438SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomy='x'ANDt.term_id=xlimit
  6. 341SELECToption_valueFROMwp_optionsWHEREoption_name='x'limit
  7. 329SELECTt.*,tt.*,tr.object_idFROMwp_termsAStINNERJOINwp_term_taxonomyASttONtt.term_id=t.term_idINNERJOINwp_term_relationshipsAStrONtr.term_taxonomy_id=tt.term_taxonomy_idWHEREtt.taxonomyin(x)ANDtr.object_idin(x)ORDERBYt.nameASC
  8. 311SELECTwp_posts.*FROMwp_postsWHERE1=xANDwp_posts.IDin(x)ANDwp_posts.post_type='x'AND((wp_posts.post_status='x'))ORDERBYwp_posts.post_dateDESC
  9. 219SELECTwp_posts.*FROMwp_postsWHEREIDin(x)
  10. 218SELECTtr.object_idFROMwp_term_relationshipsAStrINNERJOINwp_term_taxonomyASttONtr.term_taxonomy_id=tt.term_taxonomy_idWHEREtt.taxonomyin(x)ANDtt.term_idin(x)ORDERBYtr.object_idASC
  11. 217SELECTwp_posts.*FROMwp_postsWHERE1=xANDwp_posts.IDin(x)ANDwp_posts.post_type='x'AND((wp_posts.post_status='x'))ORDERBYwp_posts.menu_orderASC
  12. 202SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ORDERBYwp_posts.post_dateDESClimit
  13. 118SETNAMESutf8
  14. 115SETSESSIONsql_mode='x'
  15. 115SELECT@@SESSION.sql_mode
  16. 112SELECToption_name,option_valueFROMwp_optionsWHEREautoload='x'
  17. 111SELECTuser_id,meta_key,meta_valueFROMwp_usermetaWHEREuser_idin(x)ORDERBYumeta_idASC
  18. 108SELECTYEAR(min(post_date_gmt))ASfirstdate,YEAR(max(post_date_gmt))ASlastdateFROMwp_postsWHEREpost_status='x'
  19. 108SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomyin(x)ANDtt.count>xORDERBYtt.countDESClimit
  20. 107SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomyin(x)ANDt.term_idin(x)ORDERBYt.nameASC
  21. 107SELECT*FROMwp_usersWHEREID='x'
  22. 106SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ANDpost_date>'x'ORDERBYwp_posts.post_dateDESClimit
  23. 106SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ANDpost_date>'x'ORDERBYRAND()DESClimit
  24. 105SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ANDpost_date>'x'ORDERBYwp_posts.comment_countDESClimit

mysql general log日志清除技巧

mysql general log日志不能直接删除,间接方法.

  1. USEmysql;
  2. CREATETABLEgn2LIKEgeneral_log;
  3. RENAMETABLEgeneral_logTOoldLogs,gn2TOgeneral_log;

相关广告
  • 利用mysql general log日志找出查询次数最多的SQL句子 利用mysql general log日志找出查询次数最多的SQL句子 利用mysql general log日志找出查询次数最多的SQL句子
相关阅读

利用mysql general log日志找出查询次数最多的SQL句子

2019/10/10 17:32:25 | 谷歌SEO算法 | 5G标准出炉