Hits statistics for queries in query cache (for query cache information plugin) #441
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request adds HITS column to QUERY_CACHE_INFO table of qc_info plugin.
Our case is that we plan to turn off query cache on our production server, because we believe it becomes a bottleneck in some situations.
Load testing shows that server becomes overloaded if query cache is turned off. It is because there are some queries that begin to kill server when not using query cache. The problem is to figure out what exactly are those queries.
Currently there is no way to figure out whether the query has used query cache or not. The query_cache_info plugin shows the contents of the query cache, but it does not show any usage statistics. Moreover there is no session stat variable that could help understand whether the last query result is taken directly from query cache or not.
One can save snapthots of query_cache_info table and try to make some guess by comparing this snapshots over time but this way is neither accurate nor convenient.
Another case is to use this hits statistics to tune query cache usage. The queries that have low or zero hits can be disabled with SQL_NO_CACHE hint thus increasing query cache efficiency, I think it’s pretty straightforward.
I've found an issue MDEV-4682 that is somewhat related and addresses this problem.
The issue has a pull request (which almost completely rewrites qc_info plugin) that has never been merged.
Inspired by that request I’ve made a patch which is a way smaller and adds just few lines to qc_info plugin and to sql_cache (to increment hits when query result are read from cache) and one additional column to query_cache_info table. The tests are updated accordingly.
We are using exactly this patch on MariaDb-10.1.24 at production server with almost 1TB of data, thousands of tables and about 15K tps, it is working without any problems.
I think HITS statistic for query cache should be useful for many real world cases. If you think so, please review this PR and let me know if it needs any updates.
Thanks!