Definition of MySQL Query Cache
MySQL Query Cache is a noticeable MySQL feature that is responsible to hustle up the data recovery from a database server. This is attained by keeping the MySQL statements SELECT together with the fetched record collection in memory so that when a client requests matching queries, then it can assist the data quicker without running the commands from the database again. If we compare to data reading from disk, the cached information from the RAM takes a littler access time that diminishes the latency, thus refining I/O(Input/Output) operations. Suppose, if we take an illustration of a WordPress website or any E-commerce portal/website having high read calls along with irregular data changes, then here query cache will boost the presentation of the MySQL database server extremely making it more walkable.
If we want to set up MySQL Query Cache, then we will follow the syntax of it explained as follows:
Except MariaDB has been definitely a built-in absence of query cache but it will be present always while inactive. We can view the availability of query cache on the server with the variable have_query_cache described below:
SHOW VARIABLES LIKE ‘have_query_cache’;
Here, when executed this query if it is set to NO, user cannot allow the query cache except you recreate or reinstall a MariaDB version having cache availability. While the query cache is allowed in MariaDB by default for versions up to 10.1.6 but opening with version 10.1.7 it is disabled and if required we can support it by setting the server variable to query_cache_type to 1.
Though assisted in the versions of MariaDB prior to 10.1.7, the variable query_cache_size will be by default 0KB, which disables the query cache efficiently. But after this version, the cache size defaults to 1MB. If a user wants to set or configure the query cache size too a big enough quantity, it can be done as follows:
SET GLOBAL query_cache_size = 5000000;
Opening from MariaDB version 10.1.7, the query_cache_type is set to ON spontaneously when the server is initiated with setting the query_cache_size to a non-zero or non-default value.
How does Query Cache work in MySQL?
In MySQL, a fresh SELECT query statement is processed when the query cache is allowed so that the query cache is observed to view whether the query performs in the cache or not. Here, if the queries use the identical database server with a similar protocol version and also identical default character set then, those queries are measured as identical. The prepared statements are often deliberated as different compared to non-prepared statements.
If any similar query does not exist in the cache, then the query will be administered usually and stored with its result set within the query cache. If the query exists in the cache, then the results can be dragged from the cache that is much faster than handling it usually. The queries are to be inspected in a case-sensitive way, therefore:
SELECT * FROM z;
This query is different from following:
Select * from z;
In this context the comments are also measured and will make the queries vary:
/* Demo1 */ SELECT * FROM z;
This is different from below one:
/* Demo2 */ Select * from z;
If you want to strip comments before searching as an option, then view the server variable query_cache_strip_comments.
Whenever any changes are done to the records in a table, the entire affected result sets in the query cache are cleared. Thus, it cannot be possible to fetch data from the query cache. If the space enabled for the query cache is drained, then the eldest outcomes will be released from the cache.
If we implement query_cache_type = NO with the query specifying SQL_NO_CACHE i.e. case sensitive, then the server cannot cache the query and cannot retrieve results from the query cache. If we implement query_cache_type = DEMAND i.e. after feature MDEV-6631 request, and the query states SQL_CACHE, then MySQL server will perform the query cache.
MySQL Query Cache is normally responsible for tuning the MySQL performance and one can view the variables of query cache in the database server using the following commands:
SHOW VARIABLES LIKE %query_cache%;
One can even modify these variables applying SET SESSION or SET GLOBAL query statements shown below:
SET GLOBAL query_cache_size = 18777216;
Query cache is an important feature for query optimization improving performance. You can also display the status of query cache variable working in the server as:
SHOW STATUS LIKE “qcache%”;
The MySQL query cache is defined as a query results cache. It matches the incoming queries which initialize with SEL to a hash table, then if there exists a match it will return from the preceding execution of the query. But you will find few restrictions mentioned below:
- The query cache escapes parsing and matches byte for byte.
- Implementing non-deterministic features can result in not caching of the query that comprises temporary tables, RAND(), NOW(), UDFs, and user variables.
- This query cache was intended for not serving the stale results. If any modifications occur to the underlying table(s) will result in all cache being cancelled for those tables.
- Suppose if the query cache is applied for InnoDB to esteem MVCC when you have a transaction open, thus, the cache may not signify the data in your projected view.
Here, non-deterministic features denotes the query functions not being cached such as BENCHMARK(), CONVERT_TZ(), CONNECTION_ID(), CURRENT_TIME(), CURDATE(), ENCRYPT(), GET_LOCK(), CURTIME(), FOUND_ROWS(), DATABASE(), etc.
A query will not be cached if:
- It implements no table or generates a warning.
- It applies stored functions or local or user-defined variables.
- Transaction having serialization at isolated level.
- Having forms like:
- SELECT with SQL_NO_CACHE
- SELECT with INTO OUTFILE
- SELECT with INTO DUMPFILE
- SELECT FOR UPDATE
In this article, we have discussed MySQL Query Cache which is a feature supported in MySQL which when configured in the server boosts up the SQL queries and enhances the performance of operations. Though it is being deprecated in the MySQL version 5.7.20 and also eliminated in MySQL 8.0 query cache is still a robust tool if a user is using the supported MySQL versions. For the latest versions, one may also use a third-party tool such as ProxySQL for optimizing the MySQL database performance.
This is a guide to MySQL Query Cache. Here we discuss the definition of MySQL Query Cache, How does Query Cache works in MySQL along the examples respectively. You may also have a look at the following articles to learn more –