优化MySQL导入(将详细SQL转储转换为快速转储/使用扩展插入) [英] Optimizing MySQL Import (Converting a Verbose SQL Dump to a Speedy One / use extended-inserts)

查看:133
本文介绍了优化MySQL导入(将详细SQL转储转换为快速转储/使用扩展插入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用mysqldump和选项 - 完全插入--skip-extended-insert 来创建保存在VCS中的数据库转储。我们使用这些选项(和VCS)可以轻松比较不同的数据库版本。

We are using mysqldump with the options --complete-insert --skip-extended-insert to create database dumps that are kept in VCS. We use these options (and the VCS) to have the possibility to easily compare different database versions.

现在导入转储需要相当长的一段时间,因为当然有 - - 每个数据库行单个插入。

Now importing of the dump takes quite a while because there are - of course - single inserts per database row.

是否有一种简单的方法可以将这样的详细转储转换为每个表只有一个插入的转储?有没有人可能已经有了一些脚本?

Is there an easy way to convert such a verbose dump to one with a single insert per table? Does anyone maybe already have a some script at hand?

推荐答案

我写了一个小的python脚本来转换它:

I wrote a little python script that converts this:

LOCK TABLES `actor` WRITE;
/*!40000 ALTER TABLE `actor` DISABLE KEYS */;
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (1,'PENELOPE','GUINESS','2006-02-15 12:34:33');
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (2,'NICK','WAHLBERG','2006-02-15 12:34:33');
INSERT INTO `actor` (`actor_id`, `first_name`, `last_name`, `last_update`) VALUES (3,'ED','CHASE','2006-02-15 12:34:33');

进入:

LOCK TABLES `actor` WRITE;
/*!40000 ALTER TABLE `actor` DISABLE KEYS */;
INSERT INTO `actor` VALUES(1,'PENELOPE','GUINESS','2006-02-15 12:34:33'),(2,'NICK','WAHLBERG','2006-02-15 12:34:33'),(3,'ED','CHASE','2006-02-15 12:34:33');

它不是很漂亮或经过良好测试,但它适用于 Sakila 测试数据库转储,因此它可以处理非平凡的转储文件。

It's not very pretty or well tested, but it works on the Sakila test database dumps, so it can handle non-trivial dump files.

无论如何,这是脚本:

#!/usr/bin/env python
# -*- coding: utf-8 -*- #

import re
import sys

re_insert = re.compile(r'^insert into `(.*)` \(.*\) values (.*);', re.IGNORECASE)

current_table = ''

for line in sys.stdin:
    if line.startswith('INSERT INTO'):
        m = re_insert.match(line)
        table = m.group(1)
        values = m.group(2)

        if table != current_table:
            if current_table != '':
                sys.stdout.write(";\n\n")
            current_table = table
            sys.stdout.write('INSERT INTO `' + table + '` VALUES ' + values)
        else:
            sys.stdout.write(',' + values)
    else:
        if current_table != '':
            sys.stdout.write(";\n")
            current_table = ''
        sys.stdout.write(line)

if current_table != '':
    sys.stdout.write(';')

它预计stdin上的管道输入和打印到stdout。如果您将脚本保存为 mysqldump-convert.py ,则可以像这样使用它:

It expects piped input on stdin and prints to stdout. If you saved the script as mysqldump-convert.py, you'd use it like this:

cat ./sakila-db/sakila-full-dump.sql | python mysqldump-convert.py > test.sql

请告诉我你的表现!

这篇关于优化MySQL导入(将详细SQL转储转换为快速转储/使用扩展插入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆