SRick has asked for the wisdom of the Perl Monks concerning the following question:
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(); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Performance issues with inserting csv into SQL Server
by roboticus (Chancellor) on Jul 11, 2013 at 17:21 UTC | |
|
Re: Performance issues with inserting csv into SQL Server
by Perlbotics (Archbishop) on Jul 11, 2013 at 17:23 UTC | |
|
Re: Performance issues with inserting csv into SQL Server
by MidLifeXis (Monsignor) on Jul 11, 2013 at 17:21 UTC | |
|
Re: Performance issues with inserting csv into SQL Server
by nikosv (Deacon) on Jul 12, 2013 at 06:56 UTC |