Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32811
  • 博文数量: 8
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 107
  • 用 户 组: 普通用户
  • 注册时间: 2012-11-26 14:10
文章分类

全部博文(8)

文章存档

2014年(1)

2013年(3)

2012年(4)

我的朋友

分类: Python/Ruby

2012-12-05 19:15:17

1. 本文对三种插入数据库方法比较。
a) 循环执行$sth->execute();并且自行控制$dbh->commit();
b) $sth->bind_param_array()与$sth->execute_array()
c) 利用sqlserver提供的bulkinsert与直接insert比较 

2. 代码如下:

点击(此处)折叠或打开

  1. use strict;
  2. use warnings;
  3. use DBI;
  4. use Time::HiRes qw(gettimeofday);

  5. main();

  6. sub main
  7. {    
  8.     my $data_source = "DBI:ADO:driver={SQL Server};Server=localhost; database=NepMDB;";
  9.     my $dbh=DBI->connect($data_source, 'NepUser', '1q2w3e4r%T');
  10.     $dbh->{AutoCommit} = 0;
  11.     insertdata1("record","recordData.txt", $dbh);
  12.     
  13.     $dbh->disconnect();
  14. }

  15. sub insertdata1
  16. {
  17.     local $| = 1;
  18.     my ($table ,$file, $dbh) = @_;
  19.     open IN, $file or die "cannot open file";
  20.     my @a = <IN>;
  21.     close IN;
  22.     
  23.     my $sth=$dbh->prepare("INSERT INTO $table VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or die $dbh->errstr;
  24.     my ($start_sec, $start_microsec) = gettimeofday();
  25.     my $i;
  26.     foreach (@a)
  27.     {
  28.         my ($sid, $wid, $eta, $isc, $voc, $ff, $rs, $rsh) = split /,/;
  29.         $sth->execute($sid, $wid, $eta, $isc, $voc, $ff, $rs, $rsh);
  30.         $i++;
  31.         $dbh->commit() if $i % 500==0;
  32.     }
  33.     $dbh->commit();
  34.     $sth->finish();
  35.     
  36.     my ($end_sec, $end_microsec) = gettimeofday() ;
  37.     my $timespan= ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;
  38.     print "\t$timespan\t";
  39.     
  40.     $timespan;
  41. }
  42. sub insertdata2
  43. {
  44.     local $| = 1;
  45.     my ($table ,$file, $dbh) = @_;
  46.     open IN, $file or die "cannot open file";
  47.     my @a = <IN>;
  48.     close IN;
  49.     
  50.     my $sth=$dbh->prepare("INSERT INTO $table VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or die $dbh->errstr;
  51.     my ($start_sec, $start_microsec) = gettimeofday();
  52.     my $i = 0;
  53.     my (@sid, @wid, @eta, @isc, @voc, @ff, @rs, @rsh);
  54.     
  55.     foreach (@a)
  56.     {
  57.         my ($_sid, $_wid, $_eta, $_isc, $_voc, $_ff, $_rs, $_rsh) = split /,/;
  58.         push @sid, $_sid;
  59.         push @wid, $_wid;
  60.         push @eta, $_eta;
  61.         push @isc, $_isc;
  62.         push @voc, $_voc;
  63.         push @ff, $_ff;
  64.         push @rs, $_rs;
  65.         push @rsh, $_rsh;
  66.     }
  67.     $sth->bind_param_array(1,\@sid);
  68.     $sth->bind_param_array(2,\@wid);
  69.     $sth->bind_param_array(3,\@eta);
  70.     $sth->bind_param_array(4,\@isc);
  71.     $sth->bind_param_array(5,\@voc);
  72.     $sth->bind_param_array(6,\@ff);
  73.     $sth->bind_param_array(7,\@rs);
  74.     $sth->bind_param_array(8,\@rsh);
  75.     
  76.     $sth->execute_array({ ArrayTupleStatus => \my @tuple_status } );
  77.     $dbh->commit();
  78.     $sth->finish();
  79.     
  80.     my ($end_sec, $end_microsec) = gettimeofday();
  81.     my $timespan= ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;
  82.     print "\t$timespan\t";
  83.     $timespan;
  84. }

点击(此处)折叠或打开

  1. sub bulkinsert
  2. {
  3.     local $| = 1;
  4.     my ($table ,$file, $dbh) = @_;
  5.     open IN, $file or die "cannot open file";
  6.     
  7.     my $sql = 'truncate table '.$table;
  8.     my $trunca=$dbh->prepare($sql);
  9.     $trunca->execute();
  10.     $trunca->finish();
  11.     
  12.     my @a = <IN>;
  13.     close IN;
  14.     
  15.     my ($start_sec, $start_microsec) = gettimeofday();
  16.     
  17.     my $sth=$dbh->prepare("BULK INSERT $table FROM 'c:\\recordData.csv' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\\n')") or die $dbh->errstr;
  18.     $sth->execute();

  19.     $dbh->commit();
  20.     $sth->finish();
  21.     
  22.     my ($end_sec, $end_microsec) = gettimeofday() ;
  23.     my $timespan = ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;
  24.     print "\t$timespan\t";
  25.     
  26.     $timespan;
  27. }

2. 结果:
100,000条数据用时比较:
insertdata1: 156.930s
insertdata2: 157.624s
bulkinsert:  44.018s

3. 分析
阅读(1789) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~