1. 查问缓存设置

1) 验证服务器是否反对查问缓存

show variables like '%have_query_cache%';

| Variable_name | Value |

| have_query_cache | YES |

2) 查问缓存会受到以下3个零碎变量值的影响

show variables like 'query_cache%';

| Variable_name | Value |

| query_cache_limit | 1048576 | //可能缓存的最大后果集大小

| query_cache_min_res_unit | 4096 |

| query_cache_size | 1048576 | //决定为查问缓存调配的内存大小

| query_cache_type | OFF | //决定查问缓存的操作形式

| query_cache_wlock_invalidate | OFF |

那么mysql到底是怎么决定到底要不要把查问后果放到查问缓存中呢?是依据query_cache_type这个变量来决定的。

这个变量有三个取值:0,1,2,别离代表了off、on、demand。

mysql默认为开启 on (1)

如果是0,那么query cache 是敞开的。

如果是1,那么查问总是先到查问缓存中查找,即便应用了sql_no_cache依然查问缓存,因为sql_no_cache只是不缓存查问后果,而不是不应用查问后果。

如果是2,DEMAND没有应用sql_cache,如同依然应用了查问缓存

论断:只有query_cache_type没有敞开,sql查问总是会应用查问缓存,如果缓存没有命中则开始查问的执行打算到表中查问数据。

  1. sql_cahce和sql_no_cache hints的应用

为了测试sql语句的效率,有时候要不必缓存来查问。

应用SELECT SQL_NO_CACHE ...语法即可

SQL_NO_CACHE的真正作用是禁止缓存查问后果,但并不意味着cache不作为后果返回给query。

目前的SQL_NO_CACHE有两种解释:

1.对以后query不应用手游数据库已有缓存来查问,则以后query破费工夫会多点

2.对以后query的产生的后果集不缓存至零碎query cache里,则下次雷同query破费工夫会多点

sql_cache意思是说,查问的时候应用缓存。

对SQL_NO_CACHE的解释及测试如下:

SQL_NO_CACHE means that thewww.diuxie.com query result is not cached. It does not meanthat the cache is not used to answer the query.

You may use RESET QUERY CACHE to remove all queries from the cache and then your next query should be slow again. Same effect if you change

the table, because this makes all cached queries invalid.

mysql> select count(*) from users where email = 'hello';

| count(*) |

| 0 |

1 row in set (7.22 sec)

mysql> select count(*) from users where email = 'hello';

| count(*) |

| 0 |

1 row in set (0.45 sec)

mysql> select count(*) from users where email = 'hello';

| count(*) |

| 0 |

1 row in set (0.45 sec)

mysql> select SQL_NO_CACHE count(*) from users where email = 'hello';

| count(*) |

| 0 |

1 row in set (0.43 sec)