MySQl 查詢性能優化相關

0.

 

 

1.參考

提升網站訪問速度的 SQL 查詢優化技巧

緩存一切數據,讀取內存而不是硬盤IO

如果你的服務器默認情況下沒有使用MySQL查詢緩存,那麼你應該開啓緩存。開啓緩存意味着MySQL 會把所有的語句和語句執行的結果保存下來,如果隨後有一條與緩存中完全相同的語句需要執行,那麼MySQL 就會返回緩存的結果。緩存不會過時,因爲MySQL 會在表數據更新後刷新緩存。

MySQL緩存--服務器緩存query cache

https://dba.stackexchange.com/questions/109030/cant-enable-mysql-5-6-query-cache

Look for the [mysqld] group header in my.cnf and put those lines under it

[mysqld]
query_cache_type = 1
query_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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章