Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Optimieren des MySQL-Imports (Konvertieren eines ausführlichen SQL-Dumps in einen schnellen / erweiterte Einfügungen verwenden)

Ich habe ein kleines Python-Skript geschrieben, das dies konvertiert:

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');

hinein:

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');

Es ist nicht sehr hübsch oder gut getestet, aber es funktioniert auf dem Sakila teste Datenbank-Dumps , damit es mit nicht-trivialen Dump-Dateien umgehen kann.

Wie auch immer, hier ist das Skript:

#!/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(';')

Es erwartet eine Pipe-Eingabe auf stdin und druckt auf stdout. Wenn Sie das Skript als mysqldump-convert.py gespeichert haben , würden Sie es wie folgt verwenden:

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

Lass mich wissen, wie es dir geht!