Hello Wise Ones-

I'm in the process of refactoring my code, and I have found a number of mistakes that we had previously made using DBI. While we are now properly using prepared statements with placeholders, in referencing the following (old) post: Tricks with DBI, I found myself getting confused on a couple of areas... Here's the general logic of what I have:
# connect parent $dbh->connect(...); #statement 1 $sth=$dbh->prepare($sql); $sth->execute; while(($var)=$sth->fetchrow_array) { # fork $pm->start and next; # connect child $dbh2->connect(...); #statement 2 $sth2->prepare($sql2); $sth2->execute; while(($var2)=$sth2->fetchrow_array) { # condition if($var) { # statement 3 that might not be called $sth3->prepare($sql3); $sth3->execute; while(($var3)=$sth3->fetchrow_array) { ... } $sth3->finish; } else { # statement 4 that might not be called $sth4->prepare($sql4); $sth4->execute; while(($var4)=$sth4->fetchrow_array) { ... } $sth4->finish; } } # finish statement 2 $sth2->finish; # disconnect child $dbh2->disconnect; # end fork $pm->finish; } # finish statement 1 $sth->finish; # disconnect parent $dbh->disconnect;
With that, I have a handful of questions:
  1. I read that I need to start a new $dbh within each child (which I have done above). Is that correct?
  2. $sth and $sth2 don't seem that they need to be in a prepare_cached(), however $sth3 and $sth4 need to be handled better since they are in a while() loop and may get called multiple times. If I use prepare_cached() here, were would I call the finish()? Or should I just use prepare() and move the prepare()/finish() statements outside of the while loop (then I'm preparing the statement even if it never gets used)?
  3. Is using $sth->bind_columns and $sth->fetch faster/more efficient/preferred to using $sth->fetchrow_array?
  4. Is there anything else that I am doing wrong or can do better with my logic above?
Thank you in advance!

In reply to DBI, prepare_cached, finish, and forks by Anonymous Monk

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.