1. 程式人生 > >MySQL/MariaDB的查詢緩存

MySQL/MariaDB的查詢緩存

mit oba 能夠 ins 註釋 nbsp 自動 run size

查詢緩存:

緩存的數據:k/v,即鍵值對;

key:查詢語句的hash值;

value:查詢語句的查詢結果;


判斷緩存是否命中標準:

通過整個查詢語句的hash值的比較,完全相同則命中;


有些查詢結果是不能被緩的:

要查詢的數據庫中可能包含敏感信息

在查詢語句中包含有用戶自定義的函數(UDF);

存儲函數;

用戶自定義變量;

對於臨時表發起的查詢請求;

包含列級別授權的查詢;

有著不確定結果值的mysql的內建函數:

如:NOW(), CURRENT_DATE(), CURRENT_TIME(), CURRENT_USER(),...


在mysql數據庫查詢緩存相關的服務器變量:

MariaDB [(none)]> show global variables like 'query_cache%';
+------------------------------+---------+
| Variable_name                | Value   |
+------------------------------+---------+
| query_cache_limit            | 1048576 |
| query_cache_min_res_unit     | 4096    |
| query_cache_size             | 0       |
| query_cache_strip_comments   | OFF     |
| query_cache_type             | ON      |
| query_cache_wlock_invalidate | OFF     |
+------------------------------+---------+
6 rows in set (0.00 sec)

query_cache_limit:能夠緩存的查詢結果的最大字節數上限

query_cache_min_res_unit:查詢緩存中內存塊的最小分配單元;

query_cache_size:查詢緩存申請的在內存中的總可用空間;

query_cache_strip_comments:用於控制是否去掉SQL查詢語句中的註釋部分之後在作為key存入查詢緩存中。

query_cache_type:緩存功能開啟與否的開關:

query_cache_wlock_invalidate:如果某個連接會話對某表施加了寫鎖,是否依然可以從緩存中查詢並返回查詢結果;


想要查看緩存服務器狀態參數,就要設置查詢緩存申請的內存空間的大小,必須是1024的整數倍;

MariaDB [(none)]> set @@global.query_cache_size=134217728;
Query OK, 0 rows affected (0.00 sec)

MariaDB [hellodb]> show global status like 'Qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 1         |
| Qcache_free_memory      | 134198384 |
| Qcache_hits             | 1         |
| Qcache_inserts          | 1         |
| Qcache_lowmem_prunes    | 0         |
| Qcache_not_cached       | 1         |
| Qcache_queries_in_cache | 1         |
| Qcache_total_blocks     | 4         |
+-------------------------+-----------+
8 rows in set (0.00 sec)

Qcache_free_blocks:表示查詢緩存中目前還剩余多少個blocks;

Qcache_free_memory:查詢緩存中還空閑的內存空間;

Qcache_hits:表示查詢緩存中查詢語句的命中次數。數字越大,緩存效果越理想;

Qcache_lowmem_prunes:該參數記錄了有多少條查詢請求是因為內存空間不足而基於LRU算法被移出緩存的;如果該數值較大,則表示為查詢緩存分配的內存空間太小;

Qcache_not_cached:取決於query_cache_type變量的設置的作用下,沒有被緩存的查詢請求的數量;

Qcache_queries_in_cache:當前查詢緩存中緩存的查詢請求的結果的數量;

Qcache_total_blocks:當前查詢緩存中總計分配了多少個block;


緩存命中率的計算:

Qcache_hits/(Qcache_hits + Qcache_inserts)


通過查看服務器參數區別,來判斷是否命中緩存;

首先,查看表內容,然後查看參數,在查看表內容,在查看參數;

MariaDB [hellodb]> select * from coc;
+----+---------+----------+
| ID | ClassID | CourseID |
+----+---------+----------+
|  1 |       1 |        2 |
|  2 |       1 |        5 |
|  3 |       2 |        2 |
|  4 |       2 |        6 |
|  5 |       3 |        1 |
|  6 |       3 |        7 |
|  7 |       4 |        5 |
|  8 |       4 |        2 |
|  9 |       5 |        1 |
| 10 |       5 |        9 |
| 11 |       6 |        3 |
| 12 |       6 |        4 |
| 13 |       7 |        4 |
| 14 |       7 |        3 |
+----+---------+----------+
14 rows in set (0.00 sec)

MariaDB [hellodb]> show global status like 'Qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 1         |
| Qcache_free_memory      | 134198384 |
| Qcache_hits             | 0         |
| Qcache_inserts          | 1         |
| Qcache_lowmem_prunes    | 0         |
| Qcache_not_cached       | 1         |
| Qcache_queries_in_cache | 1         |
| Qcache_total_blocks     | 4         |
+-------------------------+-----------+
8 rows in set (0.00 sec)

MariaDB [hellodb]> select * from coc;
+----+---------+----------+
| ID | ClassID | CourseID |
+----+---------+----------+
|  1 |       1 |        2 |
|  2 |       1 |        5 |
|  3 |       2 |        2 |
|  4 |       2 |        6 |
|  5 |       3 |        1 |
|  6 |       3 |        7 |
|  7 |       4 |        5 |
|  8 |       4 |        2 |
|  9 |       5 |        1 |
| 10 |       5 |        9 |
| 11 |       6 |        3 |
| 12 |       6 |        4 |
| 13 |       7 |        4 |
| 14 |       7 |        3 |
+----+---------+----------+
14 rows in set (0.00 sec)

MariaDB [hellodb]> show global status like 'Qcache%';
+-------------------------+-----------+
| Variable_name           | Value     |
+-------------------------+-----------+
| Qcache_free_blocks      | 1         |
| Qcache_free_memory      | 134198384 |
| Qcache_hits             | 1         |
| Qcache_inserts          | 1         |
| Qcache_lowmem_prunes    | 0         |
| Qcache_not_cached       | 1         |
| Qcache_queries_in_cache | 1         |
| Qcache_total_blocks     | 4         |
+-------------------------+-----------+
8 rows in set (0.00 sec)


通過Qcache_hits參數比較,可以發現,通過Qcache_inserts添加緩存之後,再次輸入查詢語句,是從緩存中提取的結果;

需要註意,如果對表進行了寫操作(增、刪、改),則MySQL會自動將查詢緩存中與該表相關的所有緩存項全部清除;因此,與此表相關的查詢請求必須重新構建緩存內容;




MySQL/MariaDB的查詢緩存