使用perl实现拆分数据表(mysql)并迁移数据实例
随着业务量的增长,可能需要对表进行拆分来提高性能。
拆分迁移数据程序如下所示:
1.创建ttlsa_user_0-ttlsa_user_9表
代码如下:
#!/usr/bin/perl ################################### ### author: www.jb51.net ### ### QQ群:232608061 ### ### 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();
2.迁移数据
代码如下:
#!/usr/bin/perl ################################### ### author: www.jb51.net ### ### QQ群:232608061 ### ### 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();
3.附录
如果没有那么多数据量,可以随意插入一些数据进行测试。下面提供一个插入数据的脚本。
代码如下:
#!/usr/bin/perl ################################### ### author: www.jb51.net ### ### QQ群:232608061 ### ### E-mail:[email protected] ### ################################### 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″); $from_dbh->do(“set session autocommit=1″); $from_dbh->do(“set session unique_checks=1″); $insert_sql->finish(); $from_dbh->disconnect();
相关推荐
边城客栈学无止境 2020-07-05
Walter的学习笔记 2020-07-04
A宇 2020-06-14
边城客栈学无止境 2020-06-10
邓博学习笔记 2020-06-03
davidliu00 2020-05-26
ShiShuo 2020-05-16
Aggressivesnail 2020-05-10
ShiShuo 2020-04-26
hanxingwang00 2020-04-22
davidliu00 2020-03-06
ShiShuo 2020-03-06
ShiShuo 2020-03-05
Aggressivesnail 2020-02-28
aaLiweipeng 2020-02-01
amberom 2020-01-16
Walter的学习笔记 2020-01-06