|Just another Perl shrine|
DBI prepare_cached and DBD::Sybaseby mpeppler (Vicar)
|on May 20, 2002 at 17:58 UTC||Need Help??|
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.