mysql database query caching summary

Outline

Query cache (Query Cache, referred QC), storing the results of the SELECT statement and the resulting data. Nothing else to do something this summary also be a memo!

Ultra-detailed summary of mysql database query cache, worth collecting

 


working principle

The query cache works as follows:

SELECT result set caching operations and SQL statements, key to sql, value for the query result set;

If new to the SELECT statement to this as the key to the cache sql query, if the match, put the cached result set returned;

Matching criteria: whether the cache exactly the same SQL statement, sql letters are case-sensitive and space in the middle, simply understood as a key-value storage structure, key to sql, value for the sql query results, using Java String match the equals (), for example:

select age from user to select AGE from user will not match because different case; 
the SELECT Age from use with select age from user will not match because the different spaces;

sql The spaces can be ignored, it can be considered further after the equals comparisons of key operations conducted trim.


View mysql setup parameters

carried out

show variables like '%query_cache%';
Ultra-detailed summary of mysql database query cache, worth collecting

 

You can see the relevant parameters:

query_cache_type: 0- query cache is not enabled; 1-Enable, 2-enabled, the default value is 0; 
query_cache_size: Chief set the cache size, a minimum allowed value is set query_cache_size 40K, default 1M, recommended to: 64M / 128M;
query_cache_limit: limit the maximum cache can cache a single query record set size, the default setting is 1M

query_cache_type to 1, provided that they meet the requirements of the query cache, client query and record set can be cached, if the SQL plus SQL_NO_CACHE will not cache;

query_cache_type is 2, as long as the SQL parameters added: SQL_CACHE, and meet the requirements of the query cache, the client query and set records, it can be cached.


View cache usage

show status like '%Qcache%%';
Ultra-detailed summary of mysql database query cache, worth collecting

 

You can see the relevant parameters:

Qcache_hits: cache hits; 
Qcache_inserts: insert the number of times the cache, the cache every time plus 1, note that this number is not cached;

Enabling query caching

Setting Options query_cache_type = 1, and set query_cache_size = 67108864;

Note: The value is set to query_cache_size of less than 100MB. MySQL query cache is controlled by in a global lock, each update query cache memory blocks need to be locked.


Close the query cache

Setting options query_cache_type = 0, and set query_cache_size = 0.


Applicable scene

Frequent submitted for the same statement, and the table data is not very frequent scene changes, such as some static page, or a piece of information changes infrequently occurring page.

Due to the latest data query cache to cache results, so any change in the data table (insert, update, delete or otherwise change may generate operational data), will result in the query cache is refreshed. Thus, the frequency is very low and very high frequency read-only queries at the scene, open the query cache is quite advantageous for an update.


NA scene

The query cache stringent requirements of 2 SQL requests to exactly the same, including the SQL statement will affect, connected database, protocol version, character sets and other factors. The following query cache does not apply to a few scenarios:

  1. Subqueries;
  2. Procedure, a function, trigger, event called in SQL, or you refer to these results;
  3. When the query involves a number of special functions, for example: BENCHMARK (), CURDATE (), CURRENT_TIME (), CURRENT_TIMESTAMP (), NOW (), SLEEP (), CONNECTION_ID (), CURRENT_DATE (), CURRENT_USER (), PASSWORD (), RAND (), UUID (), ENCRYPT (), LAST_INSERT_ID () and the like;
  4. Queries related to mysql, information_schema or performance_schema.
  5. 类似SELECT…LOCK IN SHARE MODE、SELECT…FOR UPDATE、SELECT..INTO OUTFILE/DUMPFILE、SELECT * FROM ... WHERE autoincrement_col IS NULL的查询;
  6. SELECT execution plan to use a temporary table;
  7. Query does not reference any table, e.g. SELECT 1 + 2;
  8. Query produces a warning (warnings);
  9. There is SQL_NO_CACHE keyword SELECT statement;
  10. Related to the partition table.

Visible, using cached query limit very much. When used in the scene as read-only, and few have updated the situation, then consider enabling query caching.

Guess you like

Origin www.cnblogs.com/ct20150811/p/11277118.html