注意此缓存非Hibernate的缓存,而是指mysql的查询缓存。
mysqld参数配置query_cache_size=43M
---
测试系统采用hibernate+mysql,配置参数如下:
----------
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.use_query_cache=true
hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
hibernate.query.substitutions=true 1, false 0
----------
写java测试代码运行,控制台显示如下sql语句:select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status=2 and topic0_.fcate_id=3;以上测试代码重复运行100次,控制每次亦重复显示如上面所示一致无误的sql语句100次!!!切换到mysql控制台下运行mysql> show status like 'qcache%';输出如下信息:
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 45073576 |
| Qcache_hits             | 1        |
| Qcache_inserts          | 3        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 104      |
| Qcache_queries_in_cache | 1        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+
8 rows in set (0.00 sec)
---------------------------------------
以上测试结果说明Hibernate根本没有使用到mysql的查询缓存!!!
为了验证不是mysql本身的问题,在mysql控制台下面拷贝重复执行上面测试代码运行时输出的sql语句,运行结果如下:
---------------------------------------
mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.03 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> select count(*) as col_0_0_ from topic topic0_ where topic0_.topic_status
=2 and topic0_.fcate_id=3;
+----------+
| col_0_0_ |
+----------+
|       26 |
+----------+
1 row in set (0.00 sec)mysql> show status like 'qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 45073575 |
| Qcache_hits             | 9        |
| Qcache_inserts          | 4        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 105      |
| Qcache_queries_in_cache | 2        |
| Qcache_total_blocks     | 2        |
+-------------------------+----------+
8 rows in set (0.00 sec)----------------------------------------
以上运行结果我想能说明白了问题吧?爆汗,到底哪里出了问题,请高手告知,谢谢。

解决方案 »

  1.   

    +-------------------------+----------+ 
    ¦ Variable_name            ¦ Value     ¦ 
    +-------------------------+----------+ 
    ¦ Qcache_free_blocks       ¦ 1         ¦ 
    ¦ Qcache_free_memory       ¦ 45073576  ¦ 
    ¦ Qcache_hits              ¦ 1         ¦ 
    ¦ Qcache_inserts           ¦ 3         ¦ 
    ¦ Qcache_lowmem_prunes     ¦ 0         ¦ 
    ¦ Qcache_not_cached        ¦ 104       ¦ 
    ¦ Qcache_queries_in_cache  ¦ 1         ¦ 
    ¦ Qcache_total_blocks      ¦ 1         ¦ 
    +-------------------------+----------+ 
    -------------------------------------------------------------
    +-------------------------+----------+ 
    ¦ Variable_name            ¦ Value     ¦ 
    +-------------------------+----------+ 
    ¦ Qcache_free_blocks       ¦ 1         ¦ 
    ¦ Qcache_free_memory       ¦ 45073575  ¦ 
    ¦ Qcache_hits              ¦ 9         ¦ 
    ¦ Qcache_inserts           ¦ 4         ¦ 
    ¦ Qcache_lowmem_prunes     ¦ 0         ¦ 
    ¦ Qcache_not_cached        ¦ 105       ¦ 
    ¦ Qcache_queries_in_cache  ¦ 2         ¦ 
    ¦ Qcache_total_blocks      ¦ 2         ¦ 
    +-------------------------+----------+ 
      

  2.   

    虽然我不晓得你的mysql的查询缓存是怎么一回事
    但我觉得你的这个地方.
    可以通过设置 Hibernate 的 batch-size  批量处理来提高你的性能.
      

  3.   

    以上测试代码重复运行100次
    你放到一个session里面重复运行看看,也就是连续查询。
    因为你的mysql控制台是连续查询的,且在一个session里面。
      

  4.   

    注意此缓存非Hibernate的缓存,而是指mysql的查询缓存。 
    mysqld参数配置query_cache_size=43M 
    --- 
    测试系统采用hibernate+mysql,配置参数如下: 
    ---------- 
    hibernate.dialect=org.hibernate.dialect.MySQLDialect 
    hibernate.show_sql=true 
    hibernate.format_sql=true 
    hibernate.cache.use_query_cache=true 
    hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider 
    hibernate.query.substitutions=true 1, false 0 
    这个缓存只能是在配制文件里定义的SQL才可以使用的.
    所以这个缓存也是很少用到的.