Within Java, we access MySQL (v5.5.40) via JDBC — a prepare statement, setting parameters then calling the stored procedure. It appears that every execute() has the overhead of 4 meta-calls (per MySQL query log) preceding the actual SP call:
SELECT name, type, comment FROM mysql.proc WHERE name like...
SHOW FUNCTION STATUS LIKE 'sp_one'
SHOW PROCEDURE STATUS LIKE 'sp_one'
SHOW CREATE PROCEDURE `db-name`.`sp_one`
CALL sp_one()
Is this the expected behavior on the server side? It just doesn't seem very efficient.
Here is the example of how we use the API:
conn = DriverManager.getConnection(..);
stmt = conn.prepareCall("{call sp_one(?,?)}");
stmt.setString(1, "a");
stmt.setString(2, "b");
stmt.execute();
Please note that the following is true:
- CallableStatement.getMetadata() is not used
- Parameters are accessed by index, not by name
Thanks!
Best Answer
Percona wrote up a nice article MySQL Prepared Statements explaining pros and cons
Since what you are experiencing is the expected behavior, you should not use Prepare Statements. You are making many calls to execute a single command. If you simply ran this
the complete parsing of the SQL, preparing for execution, and the parameters already being passed in through the SQL statement are all done on the server side with one call.
Please read that Percona blog for more details.