The DBI module has a prepare_cached() method that is used heavily in some applications (in particular with Oracle), to streamline the client code. You can just call prepare_cached() with a query and if a statement handle with this query already exists it will be returned without the overhead of actually preparing the statement again.
This is great - but it doesn't work with Sybase, because of the limitation of one active statement handle per connection.

I ran into this problem last week - a fairly large Sybase database is being converted to Oracle, and the conversion script had a lot of prepare_cached() calls for two pretty simple queries which often return no rows. But this script is run in parallel (40 to 50 parallel sessions), and the total number of items to be converted is around 3-4 million or so.

What happened here is that the database server (running on a 12 processor E4500) started showing serious locking contention issues for the production code that is still running during the conversion. Hitting the server this hard with prepare() calls that include placeholders (which is more work than just executing a simple SQL statement) was causing excessive CPU usage, and the locking needed for the reqular processing was just taking a little longer to clear, causing everything to back up.

The solution: write two very small stored procedures, and call these instead. Now the server could handle 30-40 instances of the conversion script in addition to the normal work without noticing any difference.

To get back to prepare_cached() - I really understand its need, and I can see that an application would want to use it, but Sybase's support of placeholders is (maybe) somewhat limited in this respect. I'll try to find a way to cache statements effectively, but this may not be easy (and each cached statement means an open connection to the database), but in the meantime if you find yourself using prepare_cached() with Sybase consider writing a small stored procedure instead - it will be much faster.

Michael


In reply to DBI prepare_cached and DBD::Sybase by mpeppler

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.