Prepared statements have been with MySQL since version 4.1, including the protocol plumbing that helps support it. What I didn’t realize – until a recent expedition through a general query log – is that the mysql command-line interface doesn’t implement the protocol commands that support this explicitly. I came to this realization after observing a byproduct of this behavior.
The initial observation that triggered this exploration was noting that PREPARE and EXECUTE statements, when issued from the mysql command-line interface, result in two entries per command in the general query log:
6 Query PREPARE stmt FROM 'SELECT RAND()' 6 Prepare SELECT RAND() 6 Query EXECUTE stmt 6 Execute SELECT RAND()
Contrast this behavior with what is seen when a client sends COM_PREPARE and COM_EXECUTE, such as below with Connector/J (and useServerPrepStmts=true):
14 Prepare SELECT * FROM t1 WHERE …[Read more]