O perl monks,

I am trying to create a module which uses DBI to work with a database. Since the connect-prepare-execute cycle was always the same, I thought I would put that part into its own subroutine, and call it when I needed to to execute SQL statements.

I'm finding that, even though I used prepare_cached, performance is as if it were preparing each statement from scratch.

O mighty humble ones, where am I screwing up? -timallen

Here is the subroutine...
sub IS4_SQL_execute { #accept the SQL, then a list containing the bind values my $sql = shift; my @bind_values = @_; my $sth; #statement handle my $dbh; #database handle $dbh = DBI->connect('DBI:ODBC:sybase_timallen','foo','bar') or die "Couldn't connect to database: " . DBI->errstr; $dbh->{LongReadLen} = 20000; $sth = $dbh->prepare_cached($sql); $sth->execute(@bind_values) or die "Couldn't execute statement: " . $sth->errstr; return ($dbh,$sth); # I count on the calling sub finishing the statement # and disconnecting }
Here is an example of how I call it
my $sql = 'SELECT count(*) FROM products WHERE nr = ?'; my ($dbh,$sth) = IS4_SQL_execute($sql,$nr); while (my @data = $sth->fetchrow_array()) { $count = $data[0]; } $sth->finish; $dbh->disconnect;

In reply to DBI performance problem by zeno

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.