I need to create a perl script which reads in log files (.csv compressed as .gz) then inserts them into the appropriate table in a SQL Server database based on the filename. I am fairly new to perl but have come up with a solution that works, albeit incredibly slowly.

These log files range from 70,000-150,000 records per file and we get 12-16 logs per day. It took over 30 minutes to complete the script on one file of ~75,000 records. Can someone look at my code and see if there is anything which could be changed to improve performance?

use strict; use warnings; use DBI; use Compress::Zlib; use File::Find; # DBD::ODBC my $dsn = 'DBI:ODBC:Driver={SQL Server}'; my $host = 'host ip, port'; my $database = 'ProjectTest'; my $user = 'user'; my $auth = 'password'; # Connect via DBD::ODBC by specifying the DSN dynamically. my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, +$auth, { RaiseError => 1, AutoCommit => 1}) || die "Database connecti +on not made: $DBI::errstr"; # Find files in specified directory my $dir = 'C:\Users\user\Desktop\Timing Logs'; find(\&print_name, $dir); # Read found files into database sub print_name { if(-f){ #Read csv file line by line my $file = $_; my $gz = gzopen($file, "rb") or die "Cannot open $file: $gzerrno\n +"; while ($gz->gzreadline($_) > 0) { my $line = $_; chomp($line); my @array = split /,/, $line; #Match log type # if ($file =~ m/jips/){ # my $sql = "INSERT INTO dbo.JIPS VALUES (?,?,?,?,?,?,?,?,?,?, +?,?,?,?,?);"; # my $sth = $dbh->prepare( $sql ); # $sth->execute(@array); # } if ($file =~ m/ltms/){ my $sql = "INSERT INTO dbo.LTMS VALUES (?,?,?,?,?,?,?,?,?,?,?, +?,?,?,?,?,?,?,?,?,?);"; my $sth = $dbh->prepare( $sql ); $sth->execute(@array); } } die "Error reading $file: $gzerrno" if $gzerrno != Z_STREAM_END ; $gz->gzclose(); } }

In reply to Performance issues with inserting csv into SQL Server by SRick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.