MelaOS has asked for the wisdom of the Perl Monks concerning the following question:

Hi Fellow monks, I have a script which with four sql statements getting the latest id, inserting into the main table and inserting into two other linked tables.
what is puzzling me is that i have two servers, one for testing and the other production. When i run the same script connecting to the testing server, it's super fast but when i ran it on the production server each sql takes a long time.
As the server is quite new and we didn't really configure it much, could there be something i need to tweak to run perl scripts?
Attached is a the namespace that i used, the connection opening function and some part of the main code, as it would been too long to paste the whole thing :) thanks!!
use strict; use Win32::ODBC; use Win32::OLE; #i call this to open the connection sub OpenMTPDB { my $Pwd = 'W*+h0rW/oU;'; if ($Connection_Type eq 'P'){ # Server=pgssqlea01; Database=MTPDB unless($db = new Win32::ODBC("dsn=MTPDB_P; UID=EAutoma +tion; PWD=$Pwd")){ print "DB Connection Error: " . Win32::ODBC::E +rror() . "\n"; exit; } } elsif ($Connection_Type eq 'D'){ # Server=pgssqlea01; Database=MTPDB unless($db = new Win32::ODBC("dsn=MTPDB_D; UID=EAutoma +tion; PWD=$Pwd")){ print "DB Connection Error: " . Win32::ODBC::E +rror() . "\n"; exit; } } return $db; } # my main part of the code #Get the FK_lot id $sql = "SELECT TOP 1 IDX FROM ECAST_LOT ORDER BY IDX DESC "; ## Only get the id once for one summary if ($id eq ""){ print "GOTI IN\n"; if ($db->Sql($sql)){ print "Error executing the sql: $sql\n"; $isSqlFailed = 1; last; } else{ my %temp; while($db->FetchRow()){ %temp = $db->DataHash(); $id = $temp{IDX}; } print "ID: $id\n\n"; } #end if db->Sql } $id++; #Insert data into the Ecast_Lot table $sql = <<SQL_USAGE; INSERT INTO ECAST_LOT(LOT,OPERATION,SUMMARY,DEVREV +STEP,DATETIME,TIU,TEST_PROGRAM,[TRIGGER],TOTAL_TESTED,TESTER_ID,HANDL +ER_ID,COLOR) values('$lot','$oper','$sum','$dev','$date','$tiu' +,'$tp','$trigger','$ttested','$tester','$handler','$color') SQL_USAGE print "SQL: $sql\n"; if ($db->Sql($sql)){ print "Error executing the sql: $sql\n"; $isSqlFailed = 1; last; }

Replies are listed 'Best First'.
Re: ActivePerl sql connection~PLEASE HELP!!
by runrig (Abbot) on Aug 25, 2007 at 16:30 UTC

    You have a race condition in there...what if someone inserts into the ECAST_LOT table after you've selected the latest id, but before you've inserted into it? Look at DBIx-Sequence for a correct way of doing this sort of thing, or use built in sequences, auto-increment or unique-id columns, or whatever your database supports.

    And SELECT TOP 1 IDX FROM ECAST_LOT ORDER BY IDX DESC seems like an inefficient way of doing SELECT max(IDX) FROM ECAST_LOT (how big is the table on each server? is there an index on the column or is it a primary key?). Also, I hope none of the fields being inserted has single quotes in it.

    .
Re: ActivePerl sql connection~PLEASE HELP!!
by SFLEX (Chaplain) on Aug 25, 2007 at 12:38 UTC
    Think its time to tweak the New server
    if a perl script runs faster on 1 server then the other there isnt much you can do to the code to make it faster.
    Using strict & warnings and making the script not produce any warnings or errors can speed up the script a little more, but its most likely not going to make up for server lag or a 2 sec time loss...
Re: ActivePerl sql connection~PLEASE HELP!!
by SFLEX (Chaplain) on Aug 25, 2007 at 13:08 UTC
    Another thing u can try & others have found a lot of speed gained is using a Chache modules.
    but some are memory intensive and you would have to find and test out the module that works best for you
    memory