REPAIR TABLE `table_name` 修复表
OPTIMIZE TABLE `table_name` 优化表
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...
如果您已经删除了表的一大部分,或者如果您已经对含有可变长度行的表(含有VARCHAR, BLOB或TEXT列的表)进行了很多更改,则应使用
OPTIMIZE TABLE。被删除的记录被保持在链接清单中,后续的INSERT操作会重新使用旧的记录位置。您可以使用OPTIMIZE TABLE来重新
利用未使用的空间,并整理数据文件的碎片。
在多数的设置中,您根本不需要运行OPTIMIZE TABLE。即使您对可变长度的行进行了大量的更新,您也不需要经常运行,每周一次或每月一次
即可,只对特定的表运行。
OPTIMIZE TABLE只对MyISAM, BDB和InnoDB表起作用。
注意,在OPTIMIZE TABLE运行过程中,MySQL会锁定表
# coding=utf-8# 特别说明:
import sys,os
import time,shutil,datetimefrommysql_helper import MysqlHelper
def main():
print datetime.datetime.now()
Repair()
print datetime.datetime.now()
def Repair():
mysqlClient= MysqlHelper('1.2.3.4', 'root', '密码', "mysql")
# 取出基本满足规则的数据库名,后面再做进一步检查
sql_str= "show databases;"rows= mysqlClient.select(sql_str)
db_list=[]for row inrows:
db_name= row[0]if db_name != 'mysql' and db_name != 'information_schema':
#print"===%s===" %(db_name)
db_list.append(db_name)for db indb_list:
n= 1# 判断db是否符合规则
print"===%s===" %(db)
mysqlClient.select('use' + db + ';')
rows2= mysqlClient.select('show tables;');for row_2 inrows2:
table_name= row_2[0]
print"%s" %(table_name)
sql_repair= 'REPAIR TABLE' + table_name + ';'print sql_repair
sql_optimize= 'OPTIMIZE TABLE' + table_name + ';'print sql_optimize
mysqlClient.select(sql_repair)
mysqlClient.select(sql_optimize)if __name__ == '__main__':
main()
#!/usr/bin/python
import sys
import os
import MySQLdbclassMysqlHelper:
def __init__(self, MySqlHost, MySqlUser, MySqlPasswd, MySqlDB):try:
self._conn= MySQLdb.connect(host=MySqlHost, user=MySqlUser,passwd=MySqlPasswd,db=MySqlDB, charset='utf8')
except Exception, e:
print e
def __del__(self):if self._conn isnot None:
self._conn.close()
defselect(self,sql_str):try:
_cursor=self._conn.cursor()
_cursor.execute(sql_str)
results=_cursor.fetchall()
_cursor.close()returnresults
except Exception, e:
raise Exception,e
def no_select(self,sql_str):try:
_cursor=self._conn.cursor()
_cursor.execute(sql_str)
self._conn.commit()
_cursor.close()
except Exception, e:
raise Exception,eif __name__=='__main__':
mysql= MysqlHelper()