Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1372496
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: 系统运维

2011-04-06 21:57:58

近日需要对syslog表进行按日期分表处理。我的想法是每隔10天分一次表(大概数据量在500--800万)加上合理的索引应该能够快速查询。先写一个分表的小脚本。
  1. #!/usr/bin/python2.4
  2. #coding:utf-8
  3. import time
  4. import MySQLdb
  5. """max(len(line) for line in open('file.txt')) """
  6. def _getcurrtablename():
  7.     """get current table name releate to system time."""
  8.     leap = lambda year : (year % 400 == 0) or (year%4==0 and year%100!=0)
  9.     year = time.strftime("%Y")
  10.     month = time.strftime("%m")
  11.     date = time.strftime("%d")
  12.     tablename = ""
  13.     if month == "02":
  14.         if leap(year) and date == "29":
  15.             tablename = "syslog_"+year+"_"+month+"_3"
  16.         elif not leap(year) and date == "28":
  17.             tablename = "syslog_"+year+"_"+month+"_3"
  18.         elif date == "01":
  19.             tablename = "syslog_"+year+"_"+month+"_1"
  20.         elif date == "10":
  21.             tablename = "syslog_"+year+"_"+month+"_2"
  22.         else:pass
  23.     if month in ["01","03","05","07","08","10","12"]:
  24.         if date == "31":
  25.             tablename = "syslog_"+year+"_"+month+"_3"
  26.         elif date == "01":
  27.             tablename = "syslog_"+year+"_"+month+"_1"
  28.         elif date == "10":
  29.             tablename = "syslog_"+year+"_"+month+"_2"
  30.     if month in ["04","06","09","11"]:
  31.         if date == "30":
  32.             tablename = "syslog_"+year+"_"+month+"_3"
  33.         elif date == "01":
  34.             tablename = "syslog_"+year+"_"+month+"_1"
  35.         elif date == "10":
  36.             tablename = "syslog_"+year+"_"+month+"_2"
  37.     return tablename

  38. """
  39. Connection to mysql
  40. """
  41. conn= MySQLdb.connect("127.0.0.1","root","321","test",port=3306,connect_timeout=10,compress=True)
  42. currtablename = _getcurrtablename()
  43. if currtablename == "":pass
  44. else:
  45.     """rename table"""
  46.     sql = "ALTER TABLE syslog RENAME TO " + currtablename
  47.     cursor = conn.cursor()
  48.     cursor.execute(sql)
  49.     sql = "create table `syslog` like " +currtablename
  50.     cursor.execute(sql)
  51.     cursor = None
  52.     conn = None

现在数据库test里面有许多个表,在应用层查询其表列表。
PHP实现方法如下:
  1. $tablesql = "show tables";
  2. $tableresulttmp = $CI->db->query($tablesql);
  3. $data['tablelist'] = $tableresulttmp->result();

  view.py
   表列表:
   
类似的ASP实现:
  1. :<select name="table">
  2. <% while not jifei.eof %>
  3. <option ><% response.write jifei("Tables_in_test") %></option>
  4. <%jifei.movenext
  5. Wend
  6. %>
这样在应用层依据用户选择不同的表进行范围过滤。可以解决目前的问题~
阅读(962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~