- 论坛徽章:
- 3
|
本帖最后由 grshrd49 于 2013-08-27 15:12 编辑
自己写个...看....
100000条数据一次插入5000条
用时34秒
- use DBI;
- use strict;
- #my @uhomedb_test = ("dbname","ip","post","用户名","密码");
- my @uhomedb_test = ("uhomedb_test","xx.xx.xx.xx","3306","uhomer","uhome");
- my $dbh;
- $dbh = &connection(@uhomedb_test);
- my ($datatotal,$d);
- $d = $datatotal = 5000; #设置每次插入的记录总数
- my $total = 100000; #循环最大值
- my $datastring;
- for(1..$total){
- my $uname = "\'yangxm".$_."\'";
- my $passwd = "\'11111111\'";
- #my $xx = xx;
- #这里可以添加字段,添加后要放到@dataele数组中,并在$sql1的sql语句中添加字段名称
-
- my @dataele = ($uname,$passwd);
- $datatotal = 1 if($_ == $total);
- ($datastring, $datatotal) = &insertdata(\@dataele,$datastring,$datatotal);
- if(!$datatotal || ($_ == $total)){
- my $sql1 = "INSERT INTO uhomedb_test.yxm_test (username,password) VALUES $datastring;";
- print "$_\n";
- #print "$sql1\n";
- &operate_mysql($dbh,"$sql1","insert");
- }
- next if($datatotal);
-
- $datatotal = $d; #还原数据总量
- $datastring = ""; #清空数据串
- }
- #以下是工具函数,连接数据库、等...
- sub insertdata
- {
- my ($datatemp,$datastring,$datatotal) = @_;
- my @dataele = @$datatemp;
- $datastring = $datastring . "(";
- foreach(my $i=0; $i<=$#dataele; $i++){
- $datastring = $datastring . $dataele[$i];
- $datastring = $datastring . "," if($i < $#dataele);
- }
- $datastring = $datastring . ")";
- $datatotal--;
- #$datastring = $datastring . ",";
- $datastring = $datastring . "," if($datatotal);
-
- return ($datastring , $datatotal);
- }
- sub connection
- {
- #@uhomedb_test = ("uhomedb_test","116.228.70.232","3306","uhomer","uhome");
- my ($dbname,$location,$port,$db_user,$db_pass);
- my ($database,$dbh);
- ($dbname,$location,$port,$db_user,$db_pass) = @_;
- $database = "DBI:mysql:$dbname:$location:$port";
- return $dbh = DBI->connect($database,$db_user,$db_pass);
- }
- sub operate_mysql
- {
- #&operate_mysql($dbh,"$sql_word","select|insert");
- my ($dbh,$sql_word,$control);
- ($dbh,$sql_word,$control) = @_;
- if ( $control eq "select" ){
- my $sth = $dbh -> prepare($sql_word);
- $sth -> execute() or print $dbh->errstr;
- my @result ;
- while (my @value = $sth -> fetchrow_array){
- push @result, @value;
- #print "value : @value\n";
- }
- return @result;
- }
- if ( $control eq "insert" ){
- my $sth = $dbh -> prepare($sql_word);
- $sth -> execute() or print $dbh->errstr;
- }
- }
复制代码 这是表结构
|
|