MYSQL 緩存設置 提高MYSQL查詢性能

1.1.    配置

1.1.1.      配置文件配置

打開/etc/my.cnf,輸入一下配置:

#查詢緩存區的工作模式:0, 禁用查詢緩存區; 1,啓用查詢緩存區(默認設置); 2,”按需分配”模式,只響應SELECT SQL_CACHE命令。

query_cache_type = 1

#查詢緩存區的最大長度(默認設置是0,不開闢查詢緩存區)。

query_cache_size = 1048576

#允許臨時存放在查詢緩存區裏的查詢結果的最大長度(默認設置是1M)。

query_cache_limit = 1048576

 

重啓mysql生效:service mysqld restart

 

1.1.2.      動態配置

除上述通過配置文件配置外,也可在mysql運行過程中,動態查詢配置並做修改:

 

STEP1:登錄mysql

$ mysql –u root –p

 

STEP2:查詢是否開啓緩存

mysql> select @@query_cache_type;

+--------------------+

| @@query_cache_type |

+--------------------+

| ON                 |

+--------------------+

1 row in set (0.00 sec)

==>以上顯示 ON 表示已經開啓了緩存

 

當我們執行 selectid,name from tableName; 這樣就會用到查詢緩存。

在query_cache_type 打開的情況下,如果你不想使用緩存,需要指明select sql_no_cache id,name from tableName;

當然也可以禁用查詢緩存:

mysql> set session uery_cache_type=off;

 

STEP3:查詢緩存是否可用

mysql> show variables like'have_query_cache';

+------------------+-------+

| Variable_name    | Value |

+------------------+-------+

| have_query_cache | YES   |

+------------------+-------+

1 row in set (0.01 sec)

 

==>上面的顯示,表示設置查詢緩存是可用的。

 

STEP4:查詢可緩存空間大小

 

mysql> select @@global.query_cache_size;

+---------------------------+

| @@global.query_cache_size |

+---------------------------+

|                   1048576 |

+---------------------------+

1 row in set (0.00 sec)

 

==》上面顯示緩存的大小爲1M

 

可通過以下命令設置緩存大小爲2M,然後重新查詢:

mysql> sset@@global.query_cache_size=2097152;

Query OK, 0 rows affected (0.00 sec)

 

mysql> select @@global.query_cache_size;

+---------------------------+

| @@global.query_cache_size |

+---------------------------+

|                   2097152 |

+---------------------------+

1 row in set (0.00 sec)

 

STEP5:查詢可緩存結果大小限制

 

mysql> select@@global.query_cache_limit;

+----------------------------+

| @@global.query_cache_limit |

+----------------------------+

|                    1048576 |

+----------------------------+

1 row in set (0.00 sec)

==》上面顯示爲1M,超過這個大小的文件將不會緩存,也可以使用對應的set命令修改。

 

1.2.    驗證

在mysql中,可以使用show status like 'Qcache%'來查詢顯示緩存相關的狀態:

mysql>  show status like 'Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1      |

| Qcache_free_memory      | 2079896 |

| Qcache_hits             | 0       |

| Qcache_inserts          | 0       |

| Qcache_lowmem_prunes    | 0      |

| Qcache_not_cached       | 5      |

| Qcache_queries_in_cache | 0       |

| Qcache_total_blocks     | 1      |

+-------------------------+---------+

8 rows in set (0.00 sec)

 

輸入一個select查詢,然後在顯示狀態(可見插入了一個cache項):

mysql> select * from event where sid=5;==》該select語句執行結果會被緩存

mysql> show status like 'Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      | 1      |

| Qcache_free_memory      | 2078360 |

| Qcache_hits             | 0       |

| Qcache_inserts          | 1       |

| Qcache_lowmem_prunes    | 0      |

| Qcache_not_cached       | 6      |

| Qcache_queries_in_cache | 1       |

| Qcache_total_blocks     | 4      |

+-------------------------+---------+

8 rows in set (0.00 sec)

 

 

再次輸入相同的select語句,並顯示狀態(可見Qcache_hit 爲1,表示命中了緩存)

mysql> select * from event where sid=5;   è還是這條select

mysql> show status like 'Qcache%';

+-------------------------+---------+

| Variable_name           | Value   |

+-------------------------+---------+

| Qcache_free_blocks      |1       |

| Qcache_free_memory      | 2078360 |

| Qcache_hits             | 1       |

| Qcache_inserts          | 1       |

| Qcache_lowmem_prunes    | 0      |

| Qcache_not_cached       | 6      |

| Qcache_queries_in_cache | 1       |

| Qcache_total_blocks     | 4      |

+-------------------------+---------+

8 rows in set (0.00 sec)

1.3.    參考

mysql配置文件my.cnf詳解[部分]

http://www.cnblogs.com/toby/articles/2198697.html

 

MySQL查詢緩存設置提高MySQL查詢性能

http://www.cnblogs.com/iixiaowei/articles/2341716.html

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