最新消息:

MySQL缓存的详细入门以及简单教程

未分类 admin 2282浏览 0评论

从 MySQL 4.0.1 开始,MySQL有一个重要的特征:Query Cache。 当在使用中,查询缓存会存储一个 SELECT 查询的文本与被传送到客户端的相应结果。如果之后接收到一个同样的查询,服务器将从查询缓存中检索结果,而不是再次分析和执行这个同样的查询。

注意:查询缓存绝不返回过期数据。当数据被修改后,在查询缓存中的任何相关词条均被转储清除。

在某些表并不经常更改,而你又对它执行大量的相同查询时,查询缓存将是非常有用的。

上面简单说了一下mysql缓存,下面开始说到mysql缓存设置的一些相关资料。

1.首先命令行界面,输入以下代码,查看一些mysql缓存的基本设置:

show variables like '%query_cache%';

以下这是2个mysql出现的2种状态:

mysql> SHOW VARIABLES LIKE  '%query_cache%';
+------------------------------+----------+
| Variable_name                | Value    |
+------------------------------+----------+
| have_query_cache             | YES      |
| query_cache_limit            | 1048576  |
| query_cache_min_res_unit     | 4096     |
| query_cache_size             | 16777216 |
| query_cache_type             | ON       |
| query_cache_wlock_invalidate | OFF      |
+------------------------------+----------+
6 rows in set
mysql> SHOW VARIABLES LIKE  '%query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| have_query_cache             | YES     |
| query_cache_limit            | 1048576 |
| query_cache_min_res_unit     | 4096    |
| query_cache_size             | 0       |
| query_cache_type             | ON      |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
6 rows in set

会发现其变量have_query_cache的值多是yes,MYSQL初学者很容易以为这个参数为YES就代表开启了查询缓存,实际上是不对的,该参数表示当前版本的MYSQL是否支持Query Cache。

query_cache_limit 可缓存的Select查询结果的最大值 1048576 byte /1024 = 1024kB 即最大可缓存的select查询结果必须小于1024KB。

query_cache_min_res_unit 每次给query cache结果分配内存的大小 默认是 4096 byte 也即 4kB。这个值设置越大碎片越多,设置越小IO负担增大,看你自己如何取舍,一般默认值就可以了。

query_cache_size ,是系统分配的缓存大小,该值为0,表示禁用query cache。所以说第一种情况下是开启了query cache,第2种情况是关闭了query cache。

query_cache_wlock_invalidate表写锁时,无法读取缓存。

如何设置query_cache_size呢?

大概在mysql文件夹内的my.ini第94~105行:

# Query cache is used to cache SELECT results and later return them
# without actual executing the same query once again. Having the query
# cache enabled may result in significant speed improvements, if your
# have a lot of identical queries and rarely changing tables. See the
# "Qcache_lowmem_prunes" status variable to check if the current value
# is high enough for your load.
# Note: In case your tables change very often or if your queries are
# textually different every time, the query cache may result in a
# slowdown instead of a performance improvement.
query_cache_size=25M

PS:当然你也可以增加一行query_cache_type,这个是用来控制缓存的类型的。这个值不能随便设置,必须设置为数字0,1,2

0:OFF,不缓存或重新得到结果。

1:ON,缓存所有结果,除了SELECT SQL_NO_CACHE……查询。(默认值)

2:DEMAND,仅缓存SELECT SQL_CACHE……查询。

2.显示缓存当前的状态。

show status like 'qcache%';

输入上述命令显示状态如下:

mysql> show status like 'qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 1100      |
| Qcache_free_memory      | 3030696   |
| Qcache_hits             | 419577536 |
| Qcache_inserts          | 243192497 |
| Qcache_lowmem_prunes    | 176818239 |
| Qcache_not_cached       | 9334584   |
| Qcache_queries_in_cache | 2817      |
| Qcache_total_blocks     | 8142      |
+-------------------------+-----------+
8 rows in set

Qcache_free_blocks:表示查询缓存中目前还有多少剩余的blocks。如果该值显示较大,则说明查询缓存中的内存碎片过多了,可能在一定的时间进行整理。

 

Qcache_free_memory:表示查询缓存区现在还有多少的可用内存。

Qcache_hits:表示查询缓存区的命中个数,也就是直接从查询缓存区作出响应处理的查询个数。数值越大说明缓存的效果越理想。

Qcache_inserts:表示查询缓存区此前总过缓存过多少条查询命令的结果。次数越多表示在执行新来的SQL请求在缓存中未找到不得不执行查询处理,执行查询处理后把结果insert到查询缓存中。次数越多缓存效果也就不理想。(系统刚启动的时候,这个数值会比较大,毕竟查询缓存是空的)

Qcache_lowmem_prunes:表示查询缓存区已满而从其中溢出和删除的查询结果的个数。根据可以适当的调准缓存的大小。

Qcache_not_cached:表示没有进入查询缓存区的查询命令个数。

Qcache_queries_in_cache:查询缓存区当前缓存着多少条查询命令的结果。

Qcache_total_blocks:当前缓存的block数量。

 

以上面我博客的mysql为例。

查询缓存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%

查询缓存碎片率 = 1100/8142*100% ≈ 13.51%

如果查询缓存碎片率超过20%,可以用FLUSH QUERY CACHE整理缓存碎片,或者试试减小query_cache_min_res_unit,如果你的查询都是小数据量的话。

 

查询缓存利用率 = (query_cache_size – Qcache_free_memory) / query_cache_size * 100%

查询缓存利用率 = (16777216-3030696)/16777216*100% ≈ 81.94%

查询缓存利用率在25%以下的话说明query_cache_size设置的过大,可适当减小;查询缓存利用率在80%以上而且Qcache_lowmem_prunes > 50的话说明query_cache_size可能有点小,要不就是碎片太多。

 

查询缓存命中率 = (Qcache_hits – Qcache_inserts) / Qcache_hits * 100%

查询缓存命中率 = (419577536-243192497)/419577536*100% ≈42.04%

查询缓存命中率42.04%

转载请注明:爱开源 » MySQL缓存的详细入门以及简单教程

您必须 登录 才能发表评论!