一、開啟緩存
開啟緩存,設置緩存大小,具體實施如下:
1、修改配置文件my.ini
windows下是my.ini,linux下是my.cnf;
在配置文件的最后追加上:
query_cache_type = 1 query_cache_size = 600000
需要重啟mysql生效;
2、命令方式
那么采用第二種方式;
b) 開啟緩存,兩種方式:
a)使用mysql命令:
set global query_cache_type = 1; set global query_cache_size = 600000;
如果報錯:
query cache is disabled; restart the server with query_cache_type=1...
在mysql命令行輸入
show variables like "%query_cache%" 查看是否設置成功,現在可以使用緩存了;
當然如果你的數據表有更新怎么辦,沒關系mysql默認會和這個表有關系的緩存刪掉,下次查詢的時候會直接讀表然后再緩存
下面是一個簡單的例子:
[MySQL@csdba1850 ~]$ MySQL -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or /g. Your MySQL connection id is 3 Server version: 5.0.45-community MySQL Community Edition (GPL) Type 'help;' or '/h' for help. Type '/c' to clear the buffer. MySQL> set global query_cache_size = 600000; 設置緩存內存 Query OK, 0 rows affected (0.00 sec) MySQL> set session query_cache_type = ON; 開啟查詢緩存 Query OK, 0 rows affected (0.00 sec) MySQL> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | animals | | person | +----------------+ 5 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) Qcache_hits表示mysql緩存查詢在緩存中命中的累計次數,是累加值。 mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 0 | --0次 +---------------+-------+ 8 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 1 | 表示sql在緩存中直接得到結果,不需要再去解析 +---------------+-------+ 8 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 3 | 上面的sql也是是從緩存中直接取到結果 +---------------+-------+ 1 row in set (0.00 sec) mysql> insert into animals select 9,'testsds' ; 插入數據后,跟這個表所有相關的sql緩存就會被清空掉 Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 7 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 3 | 還是等於3,說明上一條sql是沒有直接從緩存中直接得到的 +---------------+-------+ 1 row in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 7 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 4 | +---------------+-------+ 1 row in set (0.00 sec)
以上的相關內容就是對mysql緩存查詢和設置的介紹,望你能有所收獲。
二、查看是否生效
1、query_cache_type 使用查詢緩存的方式
一般,我們會把 query_cache_type 設置為 ON,默認情況下應該是ON
mysql> select @@query_cache_type; +--------------------+ | @@query_cache_type | +--------------------+ | ON | +--------------------+
query_cache_type有3個值 0代表關閉查詢緩存OFF,1代表開啟ON,2(DEMAND)代表當sql語句中有SQL_CACHE關鍵詞時才緩存,如:
select SQL_CACHE user_name from users where user_id = '100';
這樣 當我們執行 select id,name from tableName; 這樣就會用到查詢緩存。
①在 query_cache_type 打開的情況下,如果你不想使用緩存,需要指明
select sql_no_cache id,name from tableName;
②當sql中用到mysql函數,也不會緩存
當然也可以禁用查詢緩存: mysql> set session query_cache_type=off;
2、have_query_cache 設置查詢緩存是否可用
mysql> show variables like 'have_query_cache'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | have_query_cache | YES |
上面的顯示,表示設置查詢緩存是可用的。
3、query_cache_size查看緩存大小
表示查詢緩存大小,也就是分配內存大小給查詢緩存,如果你分配大小為0,
那么 第一步 和 第二步 起不到作用,還是沒有任何效果。
mysql> select @@global.query_cache_size; +---------------------------+ | @@global.query_cache_size | +---------------------------+ | 16777216 | +---------------------------+
上面是 mysql6.0設置默認的,之前的版本好像默認是0的,那么就要自己設置下。
設置
set @@global.query_cache_size=1000000;
這里是設置1M左右,900多K。
再次查看下:
select @@global.query_cache_size; +---------------------------+ | @@global.query_cache_size | +---------------------------+ | 999424 | +---------------------------+
顯示我們設置新的大小,表示設置成功。
4、query_cache_limit 控制緩存查詢結果的最大值
例如: 如果查詢結果很大, 也緩存????這個明顯是不可能的。
MySql 可以設置一個最大的緩存值,當你查詢緩存數結果數據超過這個值就不會
進行緩存。缺省為1M,也就是超過了1M查詢結果就不會緩存。
mysql> select @@global.query_cache_limit; +----------------------------+ | @@global.query_cache_limit | +----------------------------+ | 1048576 | +----------------------------+
這個是默認的數值,如果需要修改,就像設置緩存大小一樣設置,使用set
重新指定大小。
好了,通過4個步驟就可以 打開了查詢緩存,具體值的大小和查詢的方式 這個因不同
的情況來指定了。
mysql查詢緩存相關變量
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 (0.00 sec)
5、查看緩存的狀態
mysql> show status like '%Qcache%'; +-------------------------+----------+ | Variable_name | Value | +-------------------------+----------+ | Qcache_free_blocks | 11 | | Qcache_free_memory | 16610552 | | Qcache_hits | 10 | | Qcache_inserts | 155 | | Qcache_lowmem_prunes | 0 | | Qcache_not_cached | 21 | | Qcache_queries_in_cache | 111 | | Qcache_total_blocks | 256 | +-------------------------+----------+ 8 rows in set (0.00 sec)
MySQL 提供了一系列的 Global Status 來記錄 Query Cache 的當前狀態,具體如下:
Qcache_free_blocks:目前還處於空閑狀態的 Query Cache 中內存 Block 數目
Qcache_free_memory:目前還處於空閑狀態的 Query Cache 內存總量
Qcache_hits:Query Cache 命中次數
Qcache_inserts:向 Query Cache 中插入新的 Query Cache 的次數,也就是沒有命中的次數
Qcache_lowmem_prunes:當 Query Cache 內存容量不夠,需要從中刪除老的 Query Cache 以給新的 Cache 對象使用的次數
Qcache_not_cached:沒有被 Cache 的 SQL 數,包括無法被 Cache 的 SQL 以及由於 query_cache_type 設置的不會被 Cache 的 SQL
Qcache_queries_in_cache:目前在 Query Cache 中的 SQL 數量
Qcache_total_blocks:Query Cache 中總的 Block 數量
6、檢查查詢緩存使用情況
檢查是否從查詢緩存中受益的最簡單的辦法就是檢查緩存命中率
當服務器收到SELECT 語句的時候,Qcache_hits 和Com_select 這兩個變量會根據查詢緩存
的情況進行遞增
查詢緩存命中率的計算公式是:Qcache_hits/(Qcache_hits + Com_select)。
mysql> show status like '%Com_select%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_select | 1 | +---------------+-------+
query_cache_min_res_unit的配置是一柄”雙刃劍”,默認是4KB,設置值大對大數據查詢有好處,但如果你的查詢都是小數據 查詢,就容易造成內存碎片和浪費。
查詢緩存碎片率 = Qcache_free_blocks / Qcache_total_blocks * 100%
如果查詢緩存碎片率超過20%,可以用FLUSH QUERY CACHE整理緩存碎片,或者試試減小query_cache_min_res_unit,如果你的查詢都是小數據量的話。
查詢緩存利用率 = (query_cache_size - Qcache_free_memory) / query_cache_size * 100%
查詢緩存利用率在25%以下的話說明query_cache_size設置的過大,可適當減小;查詢緩存利用率在80%以上而且 Qcache_lowmem_prunes > 50的話說明query_cache_size可能有點小,要不就是碎片太多。
查詢緩存命中率 = (Qcache_hits - Qcache_inserts) / Qcache_hits * 100%
示例服務器 查詢緩存碎片率 = 20.46%,查詢緩存利用率 = 62.26%,查詢緩存命中率 = 1.94%,命中率很差,可能寫操作比較頻繁吧,而且可能有些碎片。
三、緩存使用策略和過期機制
1、緩存條件
SHOW STATUS LIKE 'Qcache_hits';
另外即使完全相同的SQL,如果使用不同的字符集、不同的協議等也會被認為是不同的查詢而分別進行緩存。