当前位置:嗨网首页>书籍在线阅读

08-使用查询缓存

  
选择背景色: 黄橙 洋红 淡粉 水蓝 草绿 白色 选择字体: 宋体 黑体 微软雅黑 楷体 选择字体大小: 恢复默认

16.2.2 使用查询缓存

如果query_cache_type设置的是ON⑴或者DEMAND⑵,那么用户在进行查询的时候,服务器会自动缓存查询结果。

在查询的时候有两个查询选项可以使用,分别是SQL_CACHE和SQL_NO_CACHE。其中SQL_CACHE选项表示使用查询缓存,SQL_NO_CACHE表示不使用查询缓存,示例如下。

01 SELECT SQL_CACHE课程名FROM KC; 02 SELECT SQL_NO_CACHE课程名FROM KC;

第01句表是使用查询结果缓存,第02句表示不使用查询结果缓存。

下面是使用查询缓存的过程。

001 mysql>show databases; 002 +--------------------+ 003 |Database | 004 +--------------------+ 005 |information_schema| 006 |mysql | 007 |performance_schema| 008 |test | 009 |xscj | 010 +--------------------+ 011 5 rows in set 012 mysql>use xscj;--选中xscj数据库 013 Database changed 014 mysql>show tables; 015 +----------------+ 016 |Tables_in_xscj| 017 +----------------+ 018 |kc | 019 |xs | 020 |xs_kc | 021 |xs_kc_st | 022 +----------------+ 023 4 rows in set 024 mysql>show variables like'%query_cache%';--查看缓存是否可用 025 +------------------------------+---------+ 026 |Variable_name |Value | 027 +------------------------------+---------+ 028 |have_query_cache |YES | 029 |query_cache_limit |1048576| 030 |query_cache_min_res_unit |4096 | 031 |query_cache_size |0 | 032 |query_cache_type |ON | 033 |query_cache_wlock_invalidate|OFF | 034 +------------------------------+---------+ 035 6 rows in set 036 037 mysql>set global query_cache_size=50000;--设置缓存大小 038 Query OK,0 rows affected 039 mysql>show variables like'%query_cache%'; 040 +------------------------------+---------+ 041 |Variable_name |Value | 042 +------------------------------+---------+ 043 |have_query_cache |YES | 044 |query_cache_limit |1048576| 045 |query_cache_min_res_unit |4096 | 046 |query_cache_size |499712 | 047 |query_cache_type |ON | 048 |query_cache_wlock_invalidate|OFF | 049 +------------------------------+---------+ 050 6 rows in set 051 mysql>selectfrom kc;--第一次查询,建立查询结果缓存 052 +--------+----------------+----------+------+------+ 053 |课程号|课程名 |开课学期|学时|学分| 054 +--------+----------------+----------+------+------+ 055 |101 |计算机基础 | 1| 80| 5| 056 |102 |程序设计与语言| 2| 68| 4| 057 |206 |离散数学 | 4| 68| 4| 058 |208 |数据结构 | 5| 68| 4| 059 |209 |操作系统 | 6| 68| 4| 060 |210 |计算机原理 | 5| 85| 5| 061 |212 |数据库原理 | 7| 68| 4| 062 |301 |计算机网络 | 7| 51| 3| 063 |302 |软件工程 | 7| 51| 3| 064 +--------+----------------+----------+------+------+ 065 9 rows in set 066 mysql>show status like'qcache_hits';--命中率为0 067 +---------------+-------+ 068 |Variable_name|Value| 069 +---------------+-------+ 070 |Qcache_hits |0 | 071 +---------------+-------+ 072 1 row in set 073 mysql>selectfrom kc;--第二次输入同样的查询 074 +--------+----------------+----------+------+------+ 075 |课程号|课程名 |开课学期|学时|学分| 076 +--------+----------------+----------+------+------+ 077 |101 |计算机基础 | 1| 80| 5| 078 |102 |程序设计与语言| 2| 68| 4| 079 |206 |离散数学 | 4| 68| 4| 080 |208 |数据结构 | 5| 68| 4| 081 |209 |操作系统 | 6| 68| 4| 082 |210 |计算机原理 | 5| 85| 5| 083 |212 |数据库原理 | 7| 68| 4| 084 |301 |计算机网络 | 7| 51| 3| 085 |302 |软件工程 | 7| 51| 3| 086 +--------+----------------+----------+------+------+ 087 9 rows in set 088 089 mysql>show status like'qcache_hits';--命中率为1,缓存生效 090 +---------------+-------+ 091 |Variable_name|Value| 092 +---------------+-------+ 093 |Qcache_hits |1 | 094 +---------------+-------+ 095 1 row in set 096 mysql>select sql_no_cachefrom kc;--不使用缓存 097 +--------+----------------+----------+------+------+ 098 |课程号|课程名 |开课学期|学时|学分| 099 +--------+----------------+----------+------+------+ 100 |101 |计算机基础 | 1| 80| 5| 101 |102 |程序设计与语言| 2| 68| 4| 102 |206 |离散数学 | 4| 68| 4| 103 |208 |数据结构 | 5| 68| 4| 104 |209 |操作系统 | 6| 68| 4| 105 |210 |计算机原理 | 5| 85| 5| 106 |212 |数据库原理 | 7| 68| 4| 107 |301 |计算机网络 | 7| 51| 3| 108 |302 |软件工程 | 7| 51| 3| 109 +--------+----------------+----------+------+------+ 110 9 rows in set 111 112 mysql>show status like'qcache_hits';--命中率不变,还是1 113 +---------------+-------+ 114 |Variable_name|Value| 115 +---------------+-------+ 116 |Qcache_hits |1 | 117 +---------------+-------+ 118 1 row in set 119 mysql>insert into kc values('304','嵌入式软件开发技术1',7,64,4);--插入一条记录,缓存被清除 120 Query OK,1 row affected 121 122 mysql>selectfrom kc;--再次发送同样的查询 123 +--------+---------------------+----------+------+------+ 124 |课程号|课程名 |开课学期|学时|学分| 125 +--------+---------------------+----------+------+------+ 126 |101 |计算机基础 | 1| 80| 5| 127 |102 |程序设计与语言 | 2| 68| 4| 128 |206 |离散数学 | 4| 68| 4| 129 |208 |数据结构 | 5| 68| 4| 130 |209 |操作系统 | 6| 68| 4| 131 |210 |计算机原理 | 5| 85| 5| 132 |212 |数据库原理 | 7| 68| 4| 133 |301 |计算机网络 | 7| 51| 3| 134 |302 |软件工程 | 7| 51| 3| 135 |303 |嵌入式软件开发技术 | 7| 64| 4| 136 |304 |嵌入式软件开发技术1| 7| 64| 4| 137 +--------+---------------------+----------+------+------+ 138 11 rows in set 139 140 mysql>show status like'%qcache_hits%';--命中率不变 141 +---------------+-------+ 142 |Variable_name|Value| 143 +---------------+-------+ 144 |Qcache_hits |1 | 145 +---------------+-------+ 146 1 row in set