MySql 缓存查询原理与缓存监控 和 索引监控
MySql緩存查詢原理與緩存監控 And 索引監控
by:授客 QQ:1033553122
?
查詢緩存
1.查詢緩存操作原理
mysql執行查詢語句之前,把查詢語句同查詢緩存中的語句進行比較,且是按字節比較,僅完全一致才被認為相同。如下,這兩條語句被視為不同的查詢
SELECT * FROM tb1_name
Select * from tb1_name
?
1)不同數據庫、不同協議版本,或字符集不同的查詢被視為不同的查詢并單獨緩存。
2)以下兩種類型的查詢不被緩存
a.預處理語句
b.嵌套查詢的子查詢
3)從查詢緩存抓取查詢結果前,mysql檢查用戶對查詢涉及的所有數據庫和表是否有查詢權限,如果沒有則不使用緩存查詢結果。
4)如果從緩存查詢返回一個查詢結果,服務器遞增Qcache_hits狀態變量,而不是Com_select
5)如果表改變,所有使用了該表的緩存查詢變成不合法,從緩存移除。表可能被多種類型的語句改變,比如INSERT, UPDATE, DELETE, TRUNCATE TABLE, ALTER TABLE, DROP TABLE, 或DROP DATABASE.
?
參考連接:
http://dev.mysql.com/doc/refman/4.1/en/query-cache-operation.html
?
2.查看是否開啟了緩存查詢
SHOW VARIABLES LIKE 'have_query_cache';
?
3.從查詢緩存中移除所有查詢緩存
RESET QUERY CACHE;
?
4.查詢緩存性能監控
SHOW STATUS LIKE 'Qcache%'
輸出說明:
Qcache_free_blocks:查詢緩存中的空閑內存塊
Qcache_free_memory:查詢緩存的空閑內存數量
Qcache_hits:查詢緩存命中數量
Qcache_inserts:添加到查詢緩存的查詢的數量(不是表示沒被緩存而進行的讀,而是緩存失效而進行的讀)
Qcache_lowmen_prunes:因內存太低,從緩存查詢中刪除的查詢的數量
Qcache_not_chached:未緩存查詢的數量(未被緩存、因為querey_cache_type設置沒被緩存)
Qcache_queries_in_cache:緩存查詢中注冊的查詢的數量
Qcache_total_blocks:查詢緩存中的內存塊總數
?
SELECT查詢總數:
Com_select+Qcache_hits+ 解析錯誤的查詢數(queries with errors found by parser)
其中,Com_select表示未命中緩存數,Qcache_hits表示緩存命中數
Com_select計算公式:
Qcache_inserts+Qcache_not_cached+權限檢查錯誤數(queries with errors found during the column-privileges check)
?
索引監控
SHOW STATUS LIKE 'handler_read%';
輸出說明:
Handler_read_first
The number of times the first entry in an index was read. If this value is high, it suggests that the server is doing a lot of full index scans; for example, SELECT col1 FROM foo, assuming that col1 is indexed
索引中的第一項(the first entry in an index)被讀取的次數,如果該值很高,那表明服務器正在執行很多全索引掃描,例如 SELECT col1 FROM foo, 假設col1上建立了索引
?
Handler_read_key
The number of requests to read a row based on a key. If this value is high, it is a good indication that your tables are properly indexed for your queries.
基于某個鍵讀取一行的請求次數。如果該值很高,那很好的說明了,對于執行的請求,表采用了適當的索引。
?
Handler_read_next
The number of requests to read the next row in key order. This value is incremented if you are querying an index column with a range constraint or if you are doing an index scan.
根據鍵順序,讀取下一行的請求次數。如果你正在查詢一個帶一系列約束的索引列或者正在執行索引掃描時,該值會增加
?
Handler_read_prev
The number of requests to read the previous row in key order. This read method is mainly used to optimize ORDER BY ... DESC
根據鍵的順序,請求讀取前一行的次數。該讀取方法主要用于優化 ORDER BY ... DESC
?
Handler_read_rnd
The number of requests to read a row based on a fixed position. This value is high if you are doing a lot of queries that require sorting of the result. You probably have a lot of queries that require MySQL to scan entire tables or you have joins that do not use keys properly.
在固定位置讀取一行的請求次數。該值如果很高,那么說明正在執行許多要求對結果集排序的查詢??赡茉趫绦杏性S多要求全表掃描的查詢,或沒使用適合鍵的聯合查詢。
Handler_read_rnd_next
The number of requests to read the next row in the data file. This value is high if you are doing a lot of table scans. Generally this suggests that your tables are not properly indexed or that your queries are not written to take advantage of the indexes you have.
讀取數據文件中下一行的請求次數。該值很高,表明正在執行很多全表掃描。通常表明表沒使用適當的索引或者查詢請求沒利用現成的索引。
參考連接:
http://dev.mysql.com/doc/refman/5.7/en/dynindex-statvar.html#statvar-index-H
?
參考連接:
http://dev.mysql.com/doc/refman/4.1/en/server-status-variables.html
http://dev.mysql.com/doc/refman/4.1/en/query-cache-status-and-maintenance.html
?
轉載于:https://www.cnblogs.com/shouke/p/10157911.html
總結
以上是生活随笔為你收集整理的MySql 缓存查询原理与缓存监控 和 索引监控的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: gb酱油和gbt酱油哪个好_都是酱油,生
- 下一篇: 各类曲线的参数方程_三、常见曲线的参数方