♡ — MySQL Query Cache

MySQL query cache

  • When executing a query statement, the cache is first queried. Removed after MySQL 8.0 , because this function is not very practical.

  • my.cnfAdd the following configuration to restart MySQL to enable query caching:

    query_cache_type=1
    query_cache_size=600000
    
  • MySQL can also enable query caching by executing the following command:

    set global  query_cache_type=1;
    set global  query_cache_size=600000;
    
  • As above, when the query cache is enabled and the same query conditions and data are used, the result will be returned directly in the cache. The query conditions here include the query itself, the current database to be queried, the client protocol version number, and other information that may affect the results.

Query cache misses:

  1️⃣ Any two queries are different in any character
  2️⃣ The query contains any user-defined functions, stored functions, user variables, temporary tables, system tables in the MySQL
library   3️⃣ After the cache is established, MySQL 's query cache system will track each table involved in the query. If these tables (data or structure) change, all cached data related to this table will be invalid.


Summarize:

  • Although the cache can improve the query performance of the database, the cache also brings additional overhead. A cache operation must be performed after each query, and it must be destroyed after invalidation.

  • So , be cautious when enabling query caching, especially for write-intensive applications. If it is enabled, pay attention to reasonably control the size of the cache space. Generally speaking, it is 几十 MBmore appropriate to set the size to .

  • You can use sql_cacheand sql_no_cacheto control whether a query statement needs to be cached:

    SELECT sql_no_cache COUNT(*) FROM usr;
    

Guess you like

Origin blog.csdn.net/qq_33833327/article/details/129031523