Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prepared Statement Cache with MySQL & JDBC

I read that MySQL does not support server side query plan caching. So if i want to use PreparedStatements for performance benefits, what i can do is enable statement caching in JDBC Connection. So as per the docs it will enable caching of prepared statements on per connection basis.

What is the performance gain of PreparedStatement caching by JDBC connection compared to if MySQL had server side query plan caching ? So if a PreparedStatement is indeed found in the physical connection's cache, does it mean that when it gets to mysql server, mysql would not run query optimizaton on it and will be able to directly execute it ?

Should i even use Statement caching at JDBC connection level while using MySQL as my database ? I am using Hikari database connection pool with Mysql JDBC connector.

like image 981
snegi Avatar asked Feb 12 '14 01:02

snegi


People also ask

Does MySQL support prepared statement?

MySQL 8.0 provides support for server-side prepared statements. This support takes advantage of the efficient client/server binary protocol. Using prepared statements with placeholders for parameter values has the following benefits: Less overhead for parsing the statement each time it is executed.

Does MySQL do caching?

MySQL determines the queries to cache by examining the query_cache_type variable. Setting this value to 0 or OFF prevents caching or retrieval of cached queries. You can also set it to 1 to enable caching for all queries except for ones beginning with the SELECT SQL_NO_CACHE statement.

Are prepared statements cached?

The prepared and callable statements are cached and retrieved using standard connection object and statement object methods. Plain statements are not implicitly cached, because implicit statement caching uses a SQL string as a key, and plain statements are created without a SQL string.

Does MySQL cache query results?

3 The MySQL Query Cache. The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again.


1 Answers

There are two properties you could set:

  • useServerPrepStmts - that enables server-side prepared statements since, by default, prepared statements are emulated o the client-side.
  • cachePrepStmts - that enables the statement caching mechanism

Performance results

For client-side statements, the throughput is improved by enabling the cachePrepStmts setting, as illustrated by the following chart:

Client-side statement caching

And, for server-side statements, the throughput is also improved by enabling the cachePrepStmts property:

Server-side statement caching

So, the Statement Caching mechanism works for both client-side and server-side prepared statements as well.

When testing on both MySQL 8.0.22 and 8.0.18, using single-statement and multiple-statement transactions, client-side prepared statements performed better than server-side prepared statements.

Therfeofre, the following configuration options seem to yield the best results:

useServerPrepStmts=false
cachePrepStmts=true
prepStmtCacheSize=500
prepStmtCacheSqlLimit=1024

The last two properties were set so that we increase the cache limits as the default values are way too low for many data-driven applications.

like image 140
Vlad Mihalcea Avatar answered Oct 07 '22 23:10

Vlad Mihalcea