As
jeffa suggests, you need to break your process into 2 actions. And as I refer to
here, you might consider loading all of your values into hash keys at the beginning of your script. Handling the comparison in the hash will increase your performance, although some might argue that this doesn't scale as well. I beg to differ... my particular DBI script went from a runtime of minutes (allowing the database to handle the logic) to just under 7 seconds (loading the keys and then performing the logic in the hash). Here's an example...
my $session = shift; # value we want to query for
my $dbh = DBI->connect("DBI:mysql:$database:$server","$username","$pas
+sword");
&extract_hash;
unless ($session_hash{$session}) { &inject; }
$dbh->disconnect();
sub extract_hash {
my $select_query = "SELECT session FROM stats";
my $sth0 = $dbh->prepare($select_query);
$sth0->execute();
while (@data = $sth0->fetchrow_array) {
$session_hash{$data[0]} = 1;
}
}
sub inject {
my $insert_stmt = "INSERT INTO stats (date,
time,
user,
session,
action,
type,
zone) VALUES (?,?,?,?,
+?,?,?)";
my $sth2 = $dbh->prepare($insert_stmt);
$sth2->execute($date,$time,$user,$session,$action,$type,$zone)
+;
}
UPDATE: As
Tardis alludes to below, you might need to worry about transactions, depending on your usage. I
ass-u-me'd that you'd be performing this in a similar context to mine, where I run it in an isolated environment via cron. That may or may not be the case.
-fuzzyping
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.