Perl插入sqlserver資料庫方法比較

來源:互聯網
上載者:User
 Perl插入sqlserver資料庫方法比較分類: Python/Ruby1. 本文對三種插入資料庫方法比較。a) 迴圈執行$sth->execute();並且自行控制$dbh->commit();b) $sth->bind_param_array()與$sth->execute_array()c) 利用sqlserver提供的bulkinsert與直接insert比較 2. 代碼如下:點擊(此處)摺疊或開啟use strict;use warnings;use DBI;use Time::HiRes qw(gettimeofday);main();sub main{        my $data_source = "DBI:ADO:driver={SQL Server};Server=localhost; database=NepMDB;";    my $dbh=DBI->connect($data_source, 'NepUser', '1q2w3e4r%T');    $dbh->{AutoCommit} = 0;    insertdata1("record","recordData.txt", $dbh);        $dbh->disconnect();}sub insertdata1{    local $| = 1;    my ($table ,$file, $dbh) = @_;     open IN, $file or die "cannot open file";     my @a = <IN>;    close IN;        my $sth=$dbh->prepare("INSERT INTO $table VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or die $dbh->errstr;    my ($start_sec, $start_microsec) = gettimeofday();    my $i;     foreach (@a)    {        my ($sid, $wid, $eta, $isc, $voc, $ff, $rs, $rsh) = split /,/;        $sth->execute($sid, $wid, $eta, $isc, $voc, $ff, $rs, $rsh);        $i++;        $dbh->commit() if $i % 500==0;    }    $dbh->commit();    $sth->finish();        my ($end_sec, $end_microsec) = gettimeofday() ;    my $timespan= ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;    print "\t$timespan\t";        $timespan;}sub insertdata2{    local $| = 1;    my ($table ,$file, $dbh) = @_;     open IN, $file or die "cannot open file";    my @a = <IN>;    close IN;        my $sth=$dbh->prepare("INSERT INTO $table VALUES (?, ?, ?, ?, ?, ?, ?, ?)") or die $dbh->errstr;    my ($start_sec, $start_microsec) = gettimeofday();    my $i = 0;    my (@sid, @wid, @eta, @isc, @voc, @ff, @rs, @rsh);        foreach (@a)    {        my ($_sid, $_wid, $_eta, $_isc, $_voc, $_ff, $_rs, $_rsh) = split /,/;        push @sid, $_sid;        push @wid, $_wid;        push @eta, $_eta;        push @isc, $_isc;        push @voc, $_voc;        push @ff, $_ff;        push @rs, $_rs;        push @rsh, $_rsh;    }    $sth->bind_param_array(1,\@sid);    $sth->bind_param_array(2,\@wid);    $sth->bind_param_array(3,\@eta);    $sth->bind_param_array(4,\@isc);    $sth->bind_param_array(5,\@voc);    $sth->bind_param_array(6,\@ff);    $sth->bind_param_array(7,\@rs);    $sth->bind_param_array(8,\@rsh);        $sth->execute_array({ ArrayTupleStatus => \my @tuple_status } );    $dbh->commit();    $sth->finish();        my ($end_sec, $end_microsec) = gettimeofday();    my $timespan= ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;    print "\t$timespan\t";    $timespan;}sub bulkinsert{    local $| = 1;    my ($table ,$file, $dbh) = @_;     open IN, $file or die "cannot open file";        my $sql = 'truncate table '.$table;    my $trunca=$dbh->prepare($sql);    $trunca->execute();    $trunca->finish();        my @a = <IN>;    close IN;        my ($start_sec, $start_microsec) = gettimeofday();        my $sth=$dbh->prepare("BULK INSERT $table FROM 'c:\\recordData.csv' WITH (FIELDTERMINATOR = ',',ROWTERMINATOR = '\\n')") or die $dbh->errstr;    $sth->execute();    $dbh->commit();    $sth->finish();        my ($end_sec, $end_microsec) = gettimeofday() ;    my $timespan = ($end_microsec - $start_microsec) / 1000 + ($end_sec - $start_sec) * 1000;    print "\t$timespan\t";        $timespan;}2. 結果:100,000條資料用時比較:insertdata1: 156.930sinsertdata2: 157.624sbulkinsert:  44.018s

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.