stat_max_sql.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/python
  2. #-*- coding:utf-8 -*-
  3. import os
  4. import sys
  5. import re
  6. import json
  7. import traceback
  8. #reload(sys)
  9. #sys.setdefaultencoding('utf-8')
  10. def getTraceStackMsg():
  11. tb = sys.exc_info()[2]
  12. msg = ''
  13. for i in traceback.format_tb(tb):
  14. msg += i
  15. return msg
  16. sql_dic = {}
  17. filepath = sys.argv[1]
  18. file = open(filepath)
  19. while 1:
  20. line = file.readline()
  21. if not line:
  22. break
  23. start = line.find('["SELECT')
  24. if start < 0:
  25. start = line.find('["UPDATE')
  26. if start < 0:
  27. start = line.find('["DELETE')
  28. if start < 0:
  29. start = line.find('["INSERT')
  30. if start < 0:
  31. start = line.find('["select')
  32. if start < 0:
  33. start = line.find('["update')
  34. if start < 0:
  35. start = line.find('["delete')
  36. if start < 0:
  37. start = line.find('["insert')
  38. if start < 0:
  39. continue;
  40. sql = line[start:]
  41. end1 = sql.find(",[")
  42. end2 = sql.find("where")
  43. end3 = sql.find("WHERE")
  44. end4 = sql.find("VALUES")
  45. end5 = sql.find("values")
  46. end1 = end1 if end1 >= 0 else 9999999
  47. end2 = end2 if end2 >= 0 else 9999999
  48. end3 = end3 if end3 >= 0 else 9999999
  49. end4 = end4 if end4 >= 0 else 9999999
  50. end5 = end5 if end5 >= 0 else 9999999
  51. end = min(end1,end2,end3,end4,end5)
  52. sql = sql[0:end]
  53. if sql in sql_dic:
  54. sql_dic[sql]+=1
  55. else:
  56. sql_dic[sql]=1
  57. fpa=open("sql_sort.txt","w")
  58. res_dic=sorted(sql_dic.items(), lambda x, y: cmp(x[1], y[1]), reverse=True)
  59. for k,v in res_dic:
  60. print >> fpa, k, v
  61. fpa.close()