phpmysqlstored-procedures

How/when mysql compiles stored procedures?


We intend to use mysql stored procedures for handling all database work for one project. There are several applications(different languages) using same database and I am fairly sure that there is nice amount of repeated SQL code across those apps.

And then while reading something about mysq I stumbled upon
http://www.joinfu.com/2010/05/mysql-stored-procedures-aint-all-that/
Article basically says that stored procedures are compiled each time when new db connection is made.
Will mysql compile ALL(3 digits number at least) stored procedures on database?


Solution

  • No. Query and stored procedures are compiled on demand and then kept in a cache for a while. If you use a specific stored procedure repeatedly the version compiled in the cache will be used, but if it is no longer in cache it must be compiled first, just like any other query you send to a database.

    What MySQL certainly will not do is compile all x-hundred stored procedures at once - in case that was what you were afraid of.

    But all this is somewhat irrelevant The advantage of stored procedures is that you can make sure that multiple applications using the same database, use it in the same way. When your performance is such that you start looking at the compilation cache for speed, you are looking at the wrong place. Speed optimization is better done through either query or database redesign.