MySQL caching mechanism

MySQL caching mechanism

 

definition

 

The caching mechanism is simply to cache the SQL text and query results. If the same SQL is run, the server will get the results directly from the cache without parsing and executing the SQL. If a table is changed, then all buffered queries using this table will no longer be valid, and the relevant entries for query cache values ​​are emptied. A change refers to any data or structure change in a table, including INSERT, UPDATE, DELETE, TRUNCATE, ALTER TABLE, DROP TABLE, or DROP DATABASE, etc. It also includes queries that use MERGE tables that map to the changed table.

 

Obviously, query cache is not suitable for frequently updated tables, but for some tables with infrequently changed data and a large number of the same SQL queries, query cache will save a lot of performance.

 

 

Points to pay attention to

 

1. The capitalization of Sql must be exactly the same;

2. The client that initiates sql must use the same character set and communication protocol;

3. sql query the same table under the same database (different databases may have tables with the same name);

4. The Sql query result must be determined, that is, it cannot have functions such as now();

5. When DML or DDL occurs in the query table, its cache is invalid;

6. Queries for mysql/information_schema/performance_schema are not cached;

7. SQL using temporary tables cannot be cached either;

 

The following sql query caches are considered different:

  SELECT * FROM tbl_name

  Select * from tbl_name

 

another major place

The query cache has a global lock. Once a session is acquired, it will block other sessions accessing the cache. Therefore, when a large amount of SQL is cached, cache invalidation may take a long time;

 

 

query cache parameters

 

mysql> SHOW VARIABLES LIKE '%query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| have_query_cache | YES | -- Whether the query cache is available
| query_cache_limit            | 1048576 | --可缓存具体查询结果的最大值
| query_cache_min_res_unit     | 4096    | 
| query_cache_size             | 599040  | --查询缓存的大小
| query_cache_type             | ON      | --阻止或是支持查询缓存
| query_cache_wlock_invalidate | OFF     | 
+------------------------------+---------+

 

 

更多缓存的配置,参考前面的文章

 http://youyu4.iteye.com/admin/blogs/2333291

 

 

 

 

参考

http://blog.itpub.net/15480802/viewspace-755582/

http://blog.csdn.net/carmazhao/article/details/7088530

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326537637&siteId=291194637