MySQL query_cache_type parameter and usage details

MySQL query_cache_type parameter and usage details

The purpose of setting up MySQL query cache is:

Cache the query results so that you can directly retrieve them from the result set the next time you execute the same query; this is much faster than searching again.

The end result of query caching is that it backfires:

There are two objective reasons why query cache does not improve performance:

1. Use the hash value of the SQL statement as the key and the result set of the SQL statement as the value; this raises a problem such as select user from mysql.user and SELECT user FROM mysql.user

These two will be treated as different SQL statements. At this time, even if the result set already exists, it will not be used.

2. When the lower-level table on which the query is based is modified, the query cache related to this table will be invalidated. If the system has a large degree of concurrency, this overhead is considerable; the operation of invalidating the result set also requires concurrency.

Access control means there will also be locks. When the concurrency is large, Waiting for query cache lock will occur.

3. Whether to use it or not depends on the business model.

How to configure the query cache:

The query_cache_type system variable controls whether the query cache feature is enabled or disabled.

When query_cache_type=0, it means closed, 1 means open, and 2 means caching only when SQL_CACHE is explicitly specified in the select.

The setting of this parameter is a bit strange. 1. If the query cache is closed beforehand, but set @@global.query_cache_type=1; an error will be reported

ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it

2. If it was opened beforehand and you try to close it, the closure is incomplete and the query will still try to find the cache.

The best way to turn off query cache is to set query_cache_type=0 in my.cnf and then restart mysql.

Query cache-related system variables:

have_query_cache indicates whether this MySQL version supports query cache.

query_cache_limit indicates the maximum value allowed to cache a single result set.

query_cache_min_res_unit The minimum memory that each cached result set should occupy.

query_cache_size The memory size used for the query cache.

How to monitor the query cache hit rate:

Qcache_free_memory The current remaining space size of the query cache.

Qcache_hits The number of query cache hits.

Qcache_inserts The number of query cache insertions.

That is to say, the cache hit rate is Qcache_hits/(Qcache_hits+Qcache_inserts)

Generally, it is not possible to set this alone. You have to combine it. It is recommended to read the following article

You may also be interested in:
  • MySQL Query Cache Graphical Explanation
  • MySQL optimization query_cache_limit parameter description
  • Reasons why MySQL cancelled Query Cache
  • MySQL cache startup method and parameter details (query_cache_size)
  • Misunderstandings about MySQL query_cache
  • Analysis of MySQL Query Cache Principle

<<:  Simple web design concept color matching

>>:  jQuery Ajax chatbot implementation case study

Recommend

Vue implements a simple calculator

This article example shares the specific code of ...

Several implementation methods of the tab bar (recommended)

Tabs: Category + Description Tag bar: Category =&...

A detailed discussion of evaluation strategies in JavaScript

Table of contents A chestnut to cover it Paramete...

How to solve the Docker container startup failure

Question: After the computer restarts, the mysql ...

Detailed explanation of Shell script control docker container startup order

1. Problems encountered In the process of distrib...

MySQL database Load Data multiple uses

Table of contents Multiple uses of MySQL Load Dat...

How to use & and nohup in the background of Linux

When we work in a terminal or console, we may not...

Mysql delete duplicate data to keep the smallest id solution

Search online to delete duplicate data and keep t...

A comprehensive understanding of Vue.js functional components

Table of contents Preface React Functional Compon...

MySQL pessimistic locking and optimistic locking implementation

Table of contents Preface Actual Combat 1. No loc...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...

How to configure multiple tomcats with Nginx load balancing under Linux

The methods of installing nginx and multiple tomc...

Example of disabling browser cache configuration in Vue project

When releasing a project, you will often encounte...