Bug #117903 | Please consider allowing preparing and executing - and maybe closing - a statement in a single round-trip. | ||
---|---|---|---|
Submitted: | 7 Apr 18:28 | Modified: | 8 Apr 14:29 |
Reporter: | Jean-François Gagné | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Prepared statements | Severity: | S4 (Feature request) |
Version: | 8.x, 9.x | OS: | Any |
Assigned to: | CPU Architecture: | Any |
[7 Apr 18:28]
Jean-François Gagné
[8 Apr 6:19]
Daniël van Eeden
One possible way to optimize this as stated in [1] and [2] is to use COM_QUERY with query attributes. Currently the choice is either a. COM_QUERY: just a query string b. COM_STMT_PREPARE, COM_STMT_EXECUTE, COM_STMT_CLOSE With query attributes it would be possible to have a query string with placeholders in COM_QUERY and also send parameters for the placeholders in the same packet. And then the closing is implicit. So instead of: COM_STMT_PREPARE: "SELECT a FROM t1 WHERE a=?" COM_STMT_EXECUTE: 6 COM_STMT_CLOSE Or: COM_QUERY "SELECT a FROM t1 WHERE a=6" This would become: COM_QUERY: "SELECT a FROM t1 WHERE a=?", 6 The benefits are: - Less roundtrips (compared against regular prepared protocol) - More secure against SQL injection (compared againts COM_QUERY) - The server *could* more potentially cache the execution plan based on a hash of a query string. (when executing x times) There is also another option that is already in use: client side prepared statements. MySQL Connector/J does this based on the `useServerPrepStmts` option. This does add a lot of complexity to the connector. 1. https://databaseblog.myname.nl/2025/02/the-potential-of-query-attributes-in.html 2. https://archive.fosdem.org/2021/schedule/event/mysql_protocl/
[8 Apr 6:26]
MySQL Verification Team
Hello Jean-François, Thank you for the feature request! regards, Umesh
[8 Apr 14:29]
Jean-François Gagné
> One possible way to optimize this [...] is to use COM_QUERY with query attributes. Right, above is an alternative way of implementing parameterized queries (alternative to COM_STMT_PREPARE_N_EXECUTE with the close flag set), but I think this is a different feature request. The feature request of this bug is to allow PREPARE and EXECUTE in a single round-trip. On its own, this is a useful-enough optimization (even without implementing the close flag; though IMHO the close flag should be considered as it is a small addition allowing implementing parameterized queries).