最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
perl拆分mysql表并迁移数据实例
时间:2022-06-25 02:51:27 编辑:袖梨 来源:一聚教程网
下面这个例子是将users表拆分成10个表ttlsa_user_0-ttlsa_user_9。
1.安装相关模块
# perl -MCPAN -e shell
cpan> install Module::Build
cpan> install PHP::Strings
2.创建ttlsa_user_0-ttlsa_user_9表
[codesyntax lang=”perl”]
#!/usr/bin/perl
###################################
### author: www.ttlsa.com ###
### QQ群:39514058 ###
### E-mail:[email protected] ###
###################################
use DBI;
my $driver=”DBI:mysql”;
my $from_database=”ttlsa”;
my $from_user=”root”;
my $from_password=”123456″;
my $from_host=”localhost”;
$from_dbh=DBI->connect (“$driver:$from_database:$from_host;user=$from_user;password=$from_password”) or die “cannot connect: “. DBI->errstr;
for (0..9) {
$sql=”CREATE TABLE `ttlsa_user_$_` (
`uid` int(10) NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`passwd` varchar(40) NOT NULL,
`user_name` varchar(20) NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `user_name` (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT”;
$from_dbh->do($sql);
}
$from_dbh->disconnect();
[/codesyntax]
3.迁移数据
[codesyntax lang=”perl”]
#!/usr/bin/perl
###################################
### author: www.ttlsa.com ###
### QQ群:39514058 ###
### E-mail:[email protected] ###
###################################
use DBI;
my $driver=”DBI:mysql”;
my $from_database=”ttlsa”;
my $from_user=”root”;
my $from_password=”123456″;
my $from_host=”localhost”;
$from_dbh=DBI->connect (“$driver:$from_database:$from_host;user=$from_user;password=$from_password”) or die “cannot connect: “. DBI->errstr;
$from_dbh->do(“set names ‘utf8′”);
$from_dbh->do(“set session autocommit=0″);
$from_dbh->do(“set session unique_checks=0″);
$max=600000;
$step=10000;
for ($i=1;$i $step_i=$i+$step;
$sql=”select * from ttlsa_users where user_id>=$i and user_id $sth=$from_dbh->prepare($sql);
$sth->execute;
@value=();
while ($row=$sth->fetchrow_hashref()) {
$uid=$row->{uid};
$email=$row->{email};
$passwd=$row->{passwd};
$user_name=$row->{user_name};
$key=substr($user_id,-1);
$value[$key].=”(‘$uid’,’$email’,’$passwd’,’$user_name’),”;
}
for (0..9) {
chop($value[$_]);
$sql=”insert into ttlsa_user_$_ (`uid`,`email`,`passwd`,`user_name`) values” . $value[$_];
$from_dbh->do(“$sql”);
}
}
$from_dbh->do(“set session autocommit=1″);
$from_dbh->do(“set session unique_checks=1″);
$sth->finish();
$from_dbh->disconnect();
[/codesyntax]
4.附录
如果没有那么多数据量,可以随意插入一些数据进行测试。下面提供一个插入数据的脚本。
[codesyntax lang=”perl”]
#!/usr/bin/perl
use DBI;
use Digest::SHA qw(sha1_hex);
my $driver=”DBI:mysql”;
my $from_database=”ttlsa”;
my $from_user=”root”;
my $from_password=”123456″;
my $from_host=”localhost”;
$from_dbh=DBI->connect (“$driver:$from_database:$from_host;user=$from_user;password=$from_password”) or die “cannot connect: “. DBI->errstr;
$from_dbh->do(“set names ‘utf8′”);
$from_dbh->do(“set session autocommit=0″);
$from_dbh->do(“set session unique_checks=0″);
for (1..100000) {
$insert_sql=$from_dbh->prepare(“insert into ttlsa_users (email,passwd,user_name) values (?,?,?)”);
$email=”[email protected]”;
$data=”auto-gre-$_”;
$passwd=sha1_hex($data);
$user_name=”auto-gre-$_”;
$insert_sql->execute($email,$passwd,$user_name);
}
$from_dbh->do(“set session autocommit=1″);
$from_dbh->do(“set session unique_checks=1″);
$insert_sql->finish();
$from_dbh->disconnect();
[/codesyntax]
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21