暖冬的源码分享

 找回密码
 立即加入

QQ登录

只需一步,快速开始

搜索
热搜: 巧借
查看: 17096|回复: 0

用python修改MySQL导出文件中的innodb引擎为MyISAM

[复制链接]
发表于 2012-11-23 13:29:45 | 显示全部楼层 |阅读模式
        最近因测试需要,在本地将7G左右的MySQL文件导入到本地MySQL,然而由于里面数据量较大的表存在Innodb的表类型,拿到如速度简直就是坑爹的速度,经历了近40多个小时的折磨之后,会员表才开始~~
       气煞~~果断终止导入进程,测试MyISAM表的导入速度~~同样的一张表,440万的数据,基于Innodb时用了20多个小时,基于MyISAM时用了不到6分钟~~~狂汗啊~
       于是乎,操起python脚本,开始写相关代码,转换开始~~用了差不多21分钟的时间,将7G的数据完整的检查了一次,并把Innodb替换成了MyISAM,4000多万行处理结束了~~
  1. #! /usr/bin/env python
  2. #-*- coding:utf-8 -*-

  3. # 修改 MySQL 到处 SQL语句的引擎

  4. import os, sys, re, shutil, time, math, copy, string, datetime

  5. def write_file(file_name, content, mode = 'ab+'):
  6.     wf = open(file_name, mode)
  7.     wf.write(content)
  8.     wf.close()
  9.    
  10. def print_lines(lines):
  11.     if lines%1000==0:
  12.         print 'Found ' + str(lines/1000) + ' * 1000 records'
  13.         

  14. SCRIPT_ROOT = os.path.dirname(sys.argv[0])

  15. MySQL_FILE = r'E:\bbs_ipad.sql' # 含有Innodb 的原始数据库文件
  16. MySQL_NEW_FILE = r'E:\bbs_ipad_new.sql' # 转换后的文件保存

  17. Log_FILE = r'E:\logs.log' # 记录相关转换信息的日志文件

  18. currentLine = 1
  19. totalLine = 0
  20. emptyLine = 0
  21. eofLine = 100 # 如果连续出现15行无数据,则认为终结

  22. if os.path.isfile(MySQL_FILE)!=True:
  23.     print 'MySQL file Not exist!'
  24.     exit()
  25.    
  26. lf = open(MySQL_FILE, 'rb')
  27. nf = open(MySQL_NEW_FILE, 'ab+')

  28. if not lf or not nf:
  29.     print r'Failed when MySQL File opened '
  30.     exit()

  31. starttime = datetime.datetime.now()

  32. while True:
  33.     line = lf.readline()
  34.     line = line.strip()   
  35.    
  36.     if emptyLine > eofLine:
  37.         break
  38.    
  39.     if not line:
  40.         print_lines(currentLine)
  41.         emptyLine += 1
  42.         currentLine += 1
  43.         continue
  44.     else:
  45.         emptyLine = 0
  46.    
  47.     if not re.compile(r'(?is)^.*?ENGINE[ ]{0,5}=InnoDB[ ]{0,5}.*?).match(line):
  48.         print_lines(currentLine)
  49.         nf.write(line + "\r\n")
  50.         currentLine += 1
  51.         continue
  52.    
  53.     newLine = line.replace(r'InnoDB', r'MyISAM')
  54.     nf.write(newLine + "\r\n")
  55.     write_file(Log_FILE, str(currentLine) + "\t" + str(line) + "\tTo\t" + str(newLine) + "\r\n")
  56.     print_lines(currentLine)
  57.     currentLine += 1


  58. lf.close()
  59. nf.close()

  60. endtime = datetime.datetime.now()

  61. print (endtime - starttime)  # datetime.timedelta(0, 23, 797000)

  62. print currentLine - 100

  63. print r'Finished'
复制代码
代码仅作为阅读,为了确保不变样,下载附件吧~

贴一张记录的图,呵呵
QQ截图20121123133203.png       

MySQL_Engine.py

1.91 KB, 下载次数: 4018

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

本版积分规则

手机版|小黑屋|享码网 ( 京ICP备12003721号 )

GMT+8, 2024-3-28 21:44

Powered by Discuz! X3.5

Copyright © 2001-2021 Tencent Cloud.

快速回复 返回顶部 返回列表