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

Detailed explanation of MySQL 5.7.9 shutdown syntax example

mysql-5.7.9 finally provides shutdown syntax: Pre...

How to recover accidentally deleted messages files in Linux

If there are files that are being used by a proce...

How to reset the root password of Mysql in Windows if you forget it

My machine environment: Windows 2008 R2 MySQL 5.6...

How to use Docker to build a development environment (Windows and Mac)

Table of contents 1. Benefits of using Docker 2. ...

Ubuntu16.04 installation mysql5.7.22 graphic tutorial

VMware12.0+Ubuntu16.04+MySQL5.7.22 installation t...

jQuery plugin to achieve seamless carousel

Seamless carousel is a very common effect, and it...

Examples of using && and || operators in javascript

Table of contents Preface && Operator || ...

Common browser compatibility issues (summary)

Browser compatibility is nothing more than style ...

js memory leak scenarios, how to monitor and analyze them in detail

Table of contents Preface What situations can cau...

Solution to the problem of invalid width setting for label and span

By default, setting width for label and span is in...

Detailed summary of mysql sql statements to create tables

mysql create table sql statement Common SQL state...

Solution to the cross-domain problem of SpringBoot and Vue interaction

Table of contents Browser Same Origin Policy 1. V...