自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL緩存查詢的實際應(yīng)用

數(shù)據(jù)庫 MySQL
我們今天主要向大家描述的是MySQL緩存查詢與設(shè)置global query_cache_size的實際操作步驟,以下就是正文的主要內(nèi)容講述。

以下的文章主要講述的是MySQL緩存查詢與設(shè)置global query_cache_size的實際操作步驟,我們大家都知道其訪問量一增加的話,MySQL數(shù)據(jù)庫的壓力就大!如果對其減小壓力呢?首先緩存。

我這里有專業(yè)數(shù)據(jù)師來講解。

設(shè)置緩存global query_cache_size

 

  1. set global query_cache_size = 102760448;  
  2. set global query_cache_limit = 2097152;  
  3. set global query_cache_size = 600000;  

緩存機制簡單的說就是緩存sql文本及查詢結(jié)果,如果運行相同的sql,服務(wù)器直接從緩存中取到結(jié)果,而不需要再去解析和執(zhí)行sql。如果表更改了,那么使用這個表的所有緩沖查詢將不再有效,查詢緩存值的相關(guān)條目被清空。更改指的是表中任何數(shù)據(jù)或是結(jié)構(gòu)的改變,包括INSERT、UPDATE、DELETE、TRUNCATE、ALTER TABLE、DROP TABLE或DROP DATABASE等,也包括那些映射到改變了的表的使用MERGE表的查詢。顯然,這對于頻繁更新的表,MySQL緩存查詢緩存是不適合的,而對于一些不常改變數(shù)據(jù)且有大量相同sql查詢的表,查詢緩存會節(jié)約很大的性能。

查詢必須是完全相同的(逐字節(jié)相同)才能夠被認(rèn)為是相同的。另外,同樣的查詢字符串由于其它原因可能認(rèn)為是不同的。使用不同的數(shù)據(jù)庫、不同的協(xié)議版本或者不同默認(rèn)字符集的查詢被認(rèn)為是不同的查詢并且分別進行緩存。

下面sql查詢緩存認(rèn)為是不同的:
 

  1. SELECT * FROM tbl_name  
  2. Select * from tbl_name 

查詢緩存相關(guān)參數(shù)

  1. MySQL> SHOW VARIABLES LIKE '%query_cache%'; 
    +------------------------------+---------+ | Variable_name | Value | 
    +------------------------------+---------+ | have_query_cache | YES |  

查詢緩存是否可用 | query_cache_limit | 1048576 | --可緩存具體查詢結(jié)果的***值 | query_cache_min_res_unit | 4096 | | query_cache_size | 599040 | --查詢緩存的大小 | query_cache_type | ON | --阻止或是支持MySQL緩存查詢緩存

  1. | query_cache_wlock_invalidate | OFF | +------------------------------+---------+  

下面是一個簡單的例子:

 

  1. [MySQL@csdba1850 ~]$ MySQL -u root -p  
  2. Enter password:  
  3. Welcome to the MySQL monitor. Commands end with ; or \g.  
  4. Your MySQL connection id is 3  
  5. Server version: 5.0.45-community MySQL Community Edition (GPL)  
  6. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  7. MySQL> set global query_cache_size = 600000;   

設(shè)置緩存內(nèi)存

  1. Query OK, 0 rows affected (0.00 sec)  
  2. MySQL> set session query_cache_type = ON;  

 

 開啟查詢緩存
 

  1. Query OK, 0 rows affected (0.00 sec)  
  2. 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表示mysql緩存查詢在緩存中命中的累計次數(shù),是累加值。

 

  1. 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在緩存中直接得到結(jié)果,不需要再去解析

  1. +---------------+-------+ 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也是是從緩存中直接取到結(jié)果

  1. +---------------+-------+ 1 row in set (0.00 sec) mysql> insert into animals select 9,'testsds' ;  

插入數(shù)據(jù)后,跟這個表所有相關(guān)的sql緩存就會被清空掉

  1. 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. +---------------+-------+ 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)  

以上的相關(guān)內(nèi)容就是對mysql緩存查詢和設(shè)置的介紹,望你能有所收獲。
 

【編輯推薦】

  1. MySQL查詢優(yōu)化的5個好用方法
  2. MySQL修改表字段的實際應(yīng)用代碼演示
  3. MySQL數(shù)據(jù)表中字段的批量修改與復(fù)制
  4. 恢復(fù)MySQL數(shù)據(jù)庫root密碼2方案
  5. MySQL root 密碼破解好用方案介紹

 

責(zé)任編輯:佚名 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2010-06-02 13:33:19

MySQL 查詢緩存

2010-06-02 17:46:54

MySQL 查詢緩存

2010-05-04 15:15:39

Oracle分頁查詢

2024-01-23 09:57:50

MySQL數(shù)據(jù)庫

2010-06-07 10:08:52

MySQL FLUSH

2010-05-24 09:57:08

2010-06-03 08:59:50

MySQL Query

2010-10-14 15:59:30

MySQL查詢緩存變量

2010-05-18 13:34:42

MySQL條件sele

2010-05-21 16:41:22

MySQL SHOW

2010-05-18 14:21:35

MySQL視圖

2010-10-13 16:44:10

MySQL查詢緩存機制

2010-05-27 10:35:09

查詢MySQL數(shù)據(jù)

2010-05-17 16:52:14

MySQL limit

2010-05-18 16:29:44

MySQL修改表

2010-05-21 17:22:22

2010-05-13 13:49:09

MySQL數(shù)據(jù)庫

2010-05-24 09:24:15

MySQL 備份

2010-11-25 10:00:33

MySQL查詢緩存

2010-05-27 16:12:10

MySQL索引
點贊
收藏

51CTO技術(shù)棧公眾號