MySQL查詢緩存機制是MySQL數據庫中的重要機制之一,下面將爲您深刻分析MySQL查詢緩存機制,供您參考學習之用。mysql
MySQL緩存機制簡單的說就是緩存sql文本及查詢結果,若是運行相同的sql,服務器直接從緩存中取到結果,而不須要再去解析和執行sql。若是表更改 了,那麼使用這個表的全部緩衝查詢將再也不有效,查詢緩存值的相關條目被清空。更改指的是表中任何數據或是結構的改變,包括INSERT、UPDATE、 DELETE、TRUNCATE、ALTER TABLE、DROP TABLE或DROP DATABASE等,也包括那些映射到改變了的表的使用MERGE表的查詢。顯然,這對於頻繁更新的表,查詢緩存是不適合的,而對於一些不常改變數據且有 大量相同sql查詢的表,查詢緩存會節約很大的性能。sql
查詢必須是徹底相同的(逐字節相同)纔可以被認爲是相同的。另外,一樣的查詢字符串因爲其它緣由可能認爲是不一樣的。使用不一樣的數據庫、不一樣的協議版本或者不一樣 默認字符集的查詢被認爲是不一樣的查詢而且分別進行緩存。數據庫
下面sql查詢緩存認爲是不一樣的:緩存
SELECT * FROM tbl_name Select * from tbl_name
查詢緩存相關參數服務器
mysql> SHOW VARIABLES LIKE '%query_cache%'; +------------------------------+---------+ | Variable_name | Value | +------------------------------+---------+ | have_query_cache | YES | --查詢緩存是否可用 | query_cache_limit | 1048576 | --可緩存具體查詢結果的最大值 | query_cache_min_res_unit | 4096 | | query_cache_size | 599040 | --查詢緩存的大小 | query_cache_type | ON | --阻止或是支持查詢緩存 | query_cache_wlock_invalidate | OFF | +------------------------------+---------+
下面是一個簡單的MySQL查詢緩存機制例子:session
[mysql@csdba1850 ~]$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.0.45-community MySQL Community Edition (GPL) Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> set global query_cache_size = 600000; --設置緩存內存 Query OK, 0 rows affected (0.00 sec) mysql> set session query_cache_type = ON; --開啓查詢緩存 Query OK, 0 rows affected (0.00 sec) mysql> use test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | animals | | person | +----------------+ 5 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) --Qcache_hits表示sql查詢在緩存中命中的累計次數,是累加值。 mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 0 | --0次 +---------------+-------+ 8 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 1 | --表示sql在緩存中直接獲得結果,不須要再去解析 +---------------+-------+ 8 rows in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 6 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 3 | --上面的sql也是是從緩存中直接取到結果 +---------------+-------+ 1 row in set (0.00 sec) mysql> insert into animals select 9,'testsds' ; --插入數據後,跟這個表全部相關的sql緩存就會被清空掉 Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 7 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 3 | --仍是等於3,說明上一條sql是沒有直接從緩存中直接獲得的 +---------------+-------+ 1 row in set (0.00 sec) mysql> select count(*) from animals; +----------+ | count(*) | +----------+ | 7 | +----------+ 1 row in set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache_hits'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Qcache_hits | 4 | +---------------+-------+ 1 row in set (0.00 sec)