<p>MySQL 查询缓存的实际应用代码示例<br />
以下的文章主要介绍的是MySQL 查询缓存的实际应用代码以及查看MySQL 查询缓存的大小 ,碎片整理,清除缓存以及监视MySQL 查询缓存性能的相关内容的描述,以下就是具体内容的描述,希望在你今后的学习中会有所帮助。<br />
<br />
MySQL> select @@query_cache_type;<br />
+--------------------+<br />
| @@query_cache_type |<br />
+--------------------+<br />
| ON |<br />
+--------------------+<br />
MySQL> set query_cache_type=off;<br />
MySQL> set query_cache_type=on; php100.Com<br />
MySQL><br />
MySQL> select sql_cache id, title, body from article;<br />
MySQL> select sql_no_cache id, title, body from article;<br />
MySQL> show variables like 'have_query_cache';<br />
+------------------+-------+<br />
| Variable_name | Value |<br />
+------------------+-------+<br />
| have_query_cache | YES |<br />
+------------------+-------+ phP100.Com<br />
1 row in set (0.00 sec)<br />
查看MySQL 查询缓存的大小<br />
<br />
MySQL> select @@global.query_cache_size;<br />
+---------------------------+<br />
| @@global.query_cache_size |<br />
+---------------------------+<br />
| 16777216 |<br />
+---------------------------+<br />
1 row in set (0.00 sec)<br />
MySQL> select @@query_cache_size;<br />
+--------------------+ phP100.Com<br />
| @@query_cache_size |<br />
+--------------------+<br />
| 16777216 |<br />
+--------------------+<br />
1 row in set (0.00 sec)<br />
查看最大缓存结果,如果结果集大于该数,不缓存。<br />
<br />
MySQL> select @@global.query_cache_limit;<br />
+----------------------------+<br />
| @@global.query_cache_limit |<br />
+----------------------------+<br />
| 1048576 |<br />
+----------------------------+<br />
1 row in set (0.00 sec)<br />
碎片整理<br />
<br />
MySQL> flush query cache<br />
-> ;<br />
Query OK, 0 rows affected (0.00 sec)<br />
清除缓存<br />
<br />
MySQL> reset query cache phP100.Com<br />
-> ;<br />
Query OK, 0 rows affected (0.00 sec<br />
监视MySQL 查询缓存性能:<br />
<br />
MySQL> flush tables;<br />
Query OK, 0 rows affected (0.04 sec)<br />
MySQL> show status like 'qcache%';<br />
+-------------------------+----------+<br />
| Variable_name | Value |<br />
+-------------------------+----------+<br />
| Qcache_free_blocks | 1 |<br />
| Qcache_free_memory | 16768408 |<br />
| Qcache_hits | 6 |<br />
| Qcache_inserts | 36 | PhP100.Com<br />
| Qcache_lowmem_prunes | 0 |<br />
| Qcache_not_cached | 86 |<br />
| Qcache_queries_in_cache | 0 |<br />
| Qcache_total_blocks | 1 |<br />
+-------------------------+----------+<br />
8 rows in set (0.06 sec)<br />
看看当前缓存中有多少条信息:<br />
<br />
MySQL> show status like 'qcache_q%';<br />
+-------------------------+-------+<br />
| Variable_name | Value |<br />
+-------------------------+-------+<br />
| Qcache_queries_in_cache | 0 |<br />
+-------------------------+-------+<br />
1 row in set (0.00 sec)<br />
MySQL> select sql_cache id, title, body from article;<br />
MySQL> show status like 'qcache_q%'; PhP100.Com<br />
+-------------------------+-------+<br />
| Variable_name | Value |<br />
+-------------------------+-------+<br />
| Qcache_queries_in_cache | 1 |<br />
+-------------------------+-------+<br />
1 row in set (0.00 sec)<br />
MySQL> show status like 'qcache_f%';<br />
+--------------------+----------+<br />
| Variable_name | Value |<br />
+--------------------+----------+ PhP100.cOm<br />
| Qcache_free_blocks | 1 |<br />
| Qcache_free_memory | 16766728 |<br />
+--------------------+----------+<br />
2 rows in set (0.00 sec)<br />
以上的相关内容就是对MySQL 查询缓存的介绍,望你能有所收获。<br />
</p>