mysql 查询设置调优_MySQl 查询性能优化相关
0.
1.參考
緩存一切數據,讀取內存而不是硬盤IO
如果你的服務器默認情況下沒有使用MySQL查詢緩存,那么你應該開啟緩存。開啟緩存意味著MySQL 會把所有的語句和語句執行的結果保存下來,如果隨后有一條與緩存中完全相同的語句需要執行,那么MySQL 就會返回緩存的結果。緩存不會過時,因為MySQL 會在表數據更新后刷新緩存。
Look for the?[mysqld]?group header in?my.cnf?and put those lines under it
[mysqld]query_cache_type =1query_cache_size =4096M
query_cache_limit =2M
query_cache_strip_comments =1
Every change to a table requires scanning the 4GB to purge entries for that table.? 一旦數據更新,會清空內存的所有緩存并更新,所以應該合理設置緩存總大小?query_cache_size
2.MySQL 查詢緩存
當前設置
mysql> show variables like "query_cache%";
+------------------------------+---------+
| Variable_name | Value |
+------------------------------+---------+
| query_cache_limit | 1048576 | #單條最大 1MB,1000條數據返回45KB,這里設置為 51200 比較合適
| query_cache_min_res_unit | 4096 |? ? ?#100條數據返回4.8KB,這里設置為 5120 比較合適,不夠會再申請一塊
| query_cache_size | 1048576 | #總共分配1MB,修改為?52428800,即50MB,能夠緩存 1w 個 100條查詢結果。
| query_cache_type | OFF | #修改為 1 或 ON?緩存除了以 SELECT SQL_NO_CACHE 開頭的所有查詢結果。另一個選項是?2 或 DEMAND 只緩存以 SELECT SQL_CACHE 開頭的查詢結果。
| query_cache_wlock_invalidate | OFF |? #表鎖定時認為緩存不可用,修改為 ON
+------------------------------+---------+
5 rows in set (0.01 sec)
mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| OFF |
+--------------------+
1 row in set, 1 warning (0.00 sec)
mysql> set @@query_cache_type=ON;
ERROR 1651 (HY000): Query cache is disabled; restart the server with query_cache_type=1 to enable it
mysql> exit;
修改緩存設置??vi /etc/my.cnf 重啟?service mysqld restart
query_cache_limit = 50K
query_cache_min_res_unit = 5K
query_cache_size = 50M
query_cache_type = 1
query_cache_wlock_invalidate = ON
查詢緩存狀態
mysql> show status like "Qcache%";
+-------------------------+----------+
| Variable_name | Value |
+-------------------------+----------+
| Qcache_free_blocks | 1 | #太多碎片,最小分配內存單位設置不合理?
| Qcache_free_memory | 52365352 |
| Qcache_hits | 0 |? #命中緩存的查詢次數
| Qcache_inserts | 12 | #插入次數
| Qcache_lowmem_prunes | 0 |? ?#總緩存空間不足?
| Qcache_not_cached | 0 |
| Qcache_queries_in_cache | 12 |? ? #現有緩存個數
| Qcache_total_blocks | 26 |
+-------------------------+----------+
8 rows in set (0.00 sec)
設置?query_cache_min_res_unit 為 5K 或 10K 下面結果都是1:2,所以還是設置為 5K???
Qcache_queries_in_cache | 1152 |
| Qcache_total_blocks | 2307 |
3.
總結
以上是生活随笔為你收集整理的mysql 查询设置调优_MySQl 查询性能优化相关的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmu-python-重复元素判定_Py
- 下一篇: linux cmake编译源码,linu