利用mysql general log日志找出查询次数最多的SQL句子
2019/10/10/17:32:25 阅读:1816 来源:谷歌SEO算法 标签:
5G标准出炉
我们知道不管是apache,iis及mysql都可以通过日志能判断程序性能与错误定位了,今天我们来介绍利用mysql general log日志找出查询次数最多的SQL句子.
查询最多的sql语句,开启general log:
- mysql>showvariableslike'%general%';
- +------------------+-------------------------------------+
- |Variable_name|Value|
- +------------------+-------------------------------------+
- |general_log|OFF|
- |general_log_file|/usr/local/mysql/data/localhost.log|
- +------------------+-------------------------------------+
- mysql>setglobalgeneral_log="ON";
analysis-general-log.py脚本:
- #!/usr/bin/python
- #sortandcountmysqlgenerallog
- #Author:Jason
- #Created:UTC2015-02-1517:51:53
- importre
- importsys
- importos
- iflen(sys.argv)==2:
- logPath=sys.argv[1]
- ifnotos.path.exists(logPath):
- print("file"+logPath+"doesnotexists.")
- sys.exit(1)
- else:
- print("Usage:"+sys.argv[0]+"logPath")
- sys.exit(1)
- logFo=open(logPath)
- match=0
- forlineinlogFo:
- line=re.sub(r"\n","",line)
- ifmatch==0:
- #matchlinebeginwithnumbers
- lineMatch=re.match(r"\s+[0-9]+\s+.*",line,flags=re.I)
- iflineMatch:
- lineTmp=lineMatch.group(0)
- match=match+1
- continue
- elifmatch==1:
- #matchlinebeginwithnumbers
- lineMatch=re.match(r"\s+[0-9]+\s+.*",line,flags=re.I)
- iflineMatch:--phpfensi.com
- #matchonlyquery
- lineMatchQuery=re.match(r".*Query\s+(.*)",lineTmp,flags=re.I)
- iflineMatchQuery:
- lineTmp=lineMatchQuery.group(1)
- #removeextraspace
- lineTmp=re.sub(r"\s+","",lineTmp)
- #replacevalues(value)tovalues(x)
- lineTmp=re.sub(r"values\s*\(.*?\)","values(x)",lineTmp,flags=re.I)
- #replacefiled='value'tofiled='x'
- lineTmp=re.sub(r"(=|>|<|>=|<=)\s*('|\").*?\2","\\1'x'",lineTmp)
- #replacefiled=valuetofiled=x
- lineTmp=re.sub(r"(=|>|<|>=|<=)\s*[0-9]+","\\1x",lineTmp)
- #replacelike'value'tolike'x'
- lineTmp=re.sub(r"like\s+('|\").*?\1","like'x'",lineTmp,flags=re.I)
- #replacein(value)toin(x)
- lineTmp=re.sub(r"in\s+\(.*?\)","in(x)",lineTmp,flags=re.I)
- #replacelimitx,ytolimit
- lineTmp=re.sub(r"limit.*","limit",lineTmp,flags=re.I)
- print(lineTmp)
- match=1
- lineTmp=lineMatch.group(0)
- else:
- lineTmp+=line
- match=1
- logFo.close()
使用方法:
- analysis-general-log.pygeneral.log|sort|uniq-c|sort-nr
- 1032SELECT*FROMwp_commentsWHERE(comment_approved='x'ORcomment_approved='x')ANDcomment_post_ID=xORDERBYcomment_date_gmtDESC
- 653SELECTpost_id,meta_key,meta_valueFROMwp_postmetaWHEREpost_idin(x)ORDERBYmeta_idASC
- 527SELECTFOUND_ROWS()
- 438SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomy='x'ANDt.term_id=xlimit
- 341SELECToption_valueFROMwp_optionsWHEREoption_name='x'limit
- 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
- 311SELECTwp_posts.*FROMwp_postsWHERE1=xANDwp_posts.IDin(x)ANDwp_posts.post_type='x'AND((wp_posts.post_status='x'))ORDERBYwp_posts.post_dateDESC
- 219SELECTwp_posts.*FROMwp_postsWHEREIDin(x)
- 218SELECTtr.object_idFROMwp_term_relationshipsAStrINNERJOINwp_term_taxonomyASttONtr.term_taxonomy_id=tt.term_taxonomy_idWHEREtt.taxonomyin(x)ANDtt.term_idin(x)ORDERBYtr.object_idASC
- 217SELECTwp_posts.*FROMwp_postsWHERE1=xANDwp_posts.IDin(x)ANDwp_posts.post_type='x'AND((wp_posts.post_status='x'))ORDERBYwp_posts.menu_orderASC
- 202SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ORDERBYwp_posts.post_dateDESClimit
- 118SETNAMESutf8
- 115SETSESSIONsql_mode='x'
- 115SELECT@@SESSION.sql_mode
- 112SELECToption_name,option_valueFROMwp_optionsWHEREautoload='x'
- 111SELECTuser_id,meta_key,meta_valueFROMwp_usermetaWHEREuser_idin(x)ORDERBYumeta_idASC
- 108SELECTYEAR(min(post_date_gmt))ASfirstdate,YEAR(max(post_date_gmt))ASlastdateFROMwp_postsWHEREpost_status='x'
- 108SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomyin(x)ANDtt.count>xORDERBYtt.countDESClimit
- 107SELECTt.*,tt.*FROMwp_termsAStINNERJOINwp_term_taxonomyASttONt.term_id=tt.term_idWHEREtt.taxonomyin(x)ANDt.term_idin(x)ORDERBYt.nameASC
- 107SELECT*FROMwp_usersWHEREID='x'
- 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
- 106SELECTSQL_CALC_FOUND_ROWSwp_posts.IDFROMwp_postsWHERE1=xANDwp_posts.post_type='x'AND(wp_posts.post_status='x')ANDpost_date>'x'ORDERBYRAND()DESClimit
- 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日志不能直接删除,间接方法.
- USEmysql;
- CREATETABLEgn2LIKEgeneral_log;
- RENAMETABLEgeneral_logTOoldLogs,gn2TOgeneral_log;
热门评论