zoukankan      html  css  js  c++  java
  • mysql中利用show profile很直观的看到查询缓存的作用。

    1、首先,开启mysql的查询缓存。

      查看查询缓存情况:

    MariaDB [test]> 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 | 1048576 |
    | query_cache_strip_comments | OFF |
    | query_cache_type | ON |
    | query_cache_wlock_invalidate | OFF |
    +------------------------------+---------+
    7 rows in set (0.00 sec)

    如果不是ON,修改配置文件以开启查询缓存:
    > vi /etc/my.cnf
    [mysqld]中添加:
    query_cache_size = 20M
    query_cache_type = ON
    重启mysql服务:
    > service mysql restart
    查看缓存使用情况:

    MariaDB [test]> show status like 'qcache%';
    +-------------------------+---------+
    | Variable_name | Value |
    +-------------------------+---------+
    | Qcache_free_blocks | 1 |
    | Qcache_free_memory | 1039880 |
    | Qcache_hits | 0 |
    | Qcache_inserts | 0 |
    | Qcache_lowmem_prunes | 0 |
    | Qcache_not_cached | 2 |
    | Qcache_queries_in_cache | 0 |
    | Qcache_total_blocks | 1 |
    +-------------------------+---------+
    8 rows in set (0.00 sec)

    其中各个参数的意义如下:  

    1、Qcache_free_blocks:缓存中相邻内存块的个数。数目大说明可能有碎片。FLUSH QUERY CACHE会对缓存中的碎片进行整理,从而得到一个空闲块。  

    2、Qcache_free_memory:缓存中的空闲内存。  

    3、Qcache_hits:每次查询在缓存中命中时就增大  

    4、Qcache_inserts:每次插入一个查询时就增大。命中次数除以插入次数就是不中比率。  

    5、Qcache_lowmem_prunes:缓存出现内存不足并且必须要进行清理以便为更多查询提供空间的次数。这个数字最好长时间来看;如果这个 数字在不断增长,就表示可能碎片非常严重,或者内存很少。(上面的 free_blocks和free_memory可以告诉您属于哪种情况)  

    6、Qcache_not_cached:不适合进行缓存的查询的数量,通常是由于这些查询不是 SELECT 语句或者用了now()之类的函数。  

    7、Qcache_queries_in_cache:当前缓存的查询(和响应)的数量。  

    8、Qcache_total_blocks:缓存中块的数量。 

    下面我们来使用一下查询缓存,看看有没有效果。

    我们开启show profiles。这个上一篇文章中有提到,不会的可以看一下我的上一篇文章。

    查询背景如下:表为query_test,表结构如下(包含了大概25万条数据,由存储过程循环插入,存储过程的循环插入,我前面的文章有提到。):

    MariaDB [test]> desc query_test;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+----------------+
    | id | int(11) | NO | PRI | NULL | auto_increment |
    | name | varchar(30) | YES | | NULL | |
    | sex | int(11) | YES | | NULL | |
    | age | int(4) | YES | | NULL | |
    +-------+-------------+------+-----+---------+----------------+
    4 rows in set (0.02 sec)

    重复执行一条sql语句。

     select * from query_test where name='e8203';

    结果如下:

      

    我们可以看到,时间由0.13减小到了0.00029.足足提升了454.438754倍的效率。

    朋友们,看吧。开启查询缓存后,这种情况下,效率提升的有多吓人。

    但是,在实际工作中,查询缓存的命中极低,所以,需要我们很好的优化sql语句,以提高查询缓存的命中率。

     
  • 相关阅读:
    大搬家--百度之星 (递推)
    Scrambled Polygon--poj2007(极角排序模板)
    Space Ant--poj1696(极角排序)
    A. Link/Cut Tree--cf614A ()
    Ultra-QuickSort--POJ2299(归并排序求逆序数对)
    An Easy Problem?!--
    C. The Two Routes---cf602C(Dij)
    java 中jar的使用
    两种方法解决tomcat的 Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
    Ajax(6) Ajax向servlet请求数据库操作 并显示到当前页面 这个未经测试
  • 原文地址:https://www.cnblogs.com/573734817pc/p/10767015.html
Copyright © 2011-2022 走看看