Detailed explanation of the simple use of MySQL query cache

Detailed explanation of the simple use of MySQL query cache

MySQL is a relational database management system developed by Swedish company MySQL AB and is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System) application software.

When we turn on the MySQL query cache, when we execute exactly the same SQL statement, the server will read the result directly from the cache. When data is modified, the previous cache will become invalid, so tables that are modified frequently are not suitable for query caching.

1. Implementation process of query cache

2. Configure query cache

1. Check whether the current MySQL database supports query caching

SHOW VARIABLES LIKE 'have_query_cache'; 

2. Check whether the query cache is currently enabled in MySQL

SHOW VARIABLES LIKE 'query_cache_type'; 

3. Check the size of the query cache

SHOW VARIABLES LIKE 'query_cache_size'; 

4. View the status variables of the query cache

SHOW STATUS LIKE 'Qcache%'; 

parameter meaning
Qcache_free_blocks The number of free memory blocks in the query cache
Qcache_free_memory Amount of memory available for query cache
Qcache_hits Query cache hit count
Qcache_inserts The number of queries added to the query cache
Qcache_lowmen_prunes The number of queries that were removed from the query cache due to low memory
Qcache_not_cached The number of non-cached queries (queries that could not be cached or were not cached due to the query_cache_type setting)
Qcache_queries_in_cache The number of queries registered in the query cache
Qcache_total_blocks The total number of blocks in the query cache

3. Enable query cache

MySQL query cache is disabled by default. You need to manually configure the parameter query_cache_type to enable query cache. query_cache_type
There are three possible values ​​for this parameter:

value meaning
OFF or 0 Query cache disabled
ON or 1 The query cache function is turned on. The SELECT result will be cached if it meets the cache conditions. Otherwise, it will not be cached. If SQL_NO_CACHE is explicitly specified, it will not be cached.
DEMAND or 2 The query cache function is performed on demand. Only SELECT statements that explicitly specify SQL_CACHE are cached; others are not cached.

1. Configure in usr/my.cnf (the configuration file may also be in /etc/my.cnf)

2. Restart the service

service mysql restart

3. Testing


4. Query Cache SELECT Options

There are two options related to query caching that can be specified in the SELECT statement:

  • SQL_CACHE : Caches query results if they are cacheable and the value of the query_cache_type system variable is ON or DEMAND .
  • SQL_NO_CACHE : The server does not use the query cache. It neither checks the query cache, nor checks if the result is already cached, nor caches the query results.
SELECT SQL_CACUE id,name FROM customer;
SELECT SQL_NO_CACHE id,name FROM customer;

5. Query cache failure

1. Inconsistent SQL statements

To hit the cache, the query SQL statement must be consistent

SQL1: select count(*) from tb_item;
SQL2: select count(*) from tb_Item;

2. The query statement has some uncertain values

SQL1: select * from tb_item where updatetime < now() limit 1;
SQL2:select user();
SQL3:select database();

3. Do not use any table query statements

select 'A';

4. Query tables in mysql, information_schema or performance_schema databases

select * from information_schema.engines;

5. Queries executed within stored functions, triggers, or event bodies

As title

6. Table changes lead to cache deletion

If a table changes, all cached queries using that table become invalid and are removed from the cache. This includes queries that use MERGE to tables that are mapped to the changed table. A table can be altered by many types of statements, such as INSERT, UPDATE, DELETE, TRUNCATE TABLE, ALTER TABLE, DROP TABLE, or DROP DATABASE.

The above is a summary of the simple use of MySQL query cache

This is the end of this article about the simple use of MySQL query cache. For more information about the use of MySQL query cache, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Tips on MySQL query cache
  • Detailed explanation of query and clear commands for MySQL cache
  • Basic learning tutorial of MySQL query cache mechanism
  • Possible methods to clear MySQL query cache
  • MySQL query cache description
  • mysql set query cache

<<:  How to use JavaScript and CSS correctly in XHTML documents

>>:  Pure js to achieve typewriter effect

Recommend

Several skills you must know when making web pages

1. z-index is invalid in IE6. In CSS, the z-index...

Analysis of the principles of Mysql dirty page flush and shrinking table space

mysql dirty pages Due to the WAL mechanism, when ...

Minimalistic website design examples

Web Application Class 1. DownForEveryoneOrJustMe ...

Full process record of Nginx reverse proxy configuration

1. Preparation Install Tomcat on Linux system, us...

Detailed explanation of JavaScript clipboard usage

(1) Introduction: clipboard.js is a lightweight J...

js to realize the mouse following game

This article shares the specific code of js to im...

Graphic tutorial on installing tomcat8 on centos7.X Linux system

1. Create the tomcat installation path mkdir /usr...

Tips for List Building for Website Maintenance Pages

And, many times, maintenance requires your website...

How to start a transaction in MySQL

Preface This article mainly introduces how to sta...

How to implement Docker to dynamically pass parameters to Springboot projects

background Recently, some friends who are new to ...

Teach you how to install docker on windows 10 home edition

When I wrote the Redis book and the Spring Cloud ...

How to achieve centered layout in CSS layout

1. Set the parent container to a table and the ch...