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

深度解析MySQL查詢緩存機(jī)制

數(shù)據(jù)庫 MySQL
查詢是MySQL數(shù)據(jù)庫的核心,而查詢緩存機(jī)制簡(jiǎn)單的說就是緩存sql文本及查詢結(jié)果,如果運(yùn)行相同的sql,服務(wù)器直接從緩存中取到結(jié)果,而不需要再去解析和執(zhí)行sql。

MySQL查詢緩存機(jī)制是MySQL數(shù)據(jù)庫中的重要機(jī)制之一,下面將為您深入分析MySQL查詢緩存機(jī)制,供您參考學(xué)習(xí)之用。

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

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

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

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

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

  1. mysql> SHOW VARIABLES LIKE '%query_cache%';  
  2. +------------------------------+---------+  
  3. | Variable_name                | Value   |  
  4. +------------------------------+---------+  
  5. | have_query_cache             | YES     | --查詢緩存是否可用  
  6. | query_cache_limit            | 1048576 | --可緩存具體查詢結(jié)果的最大值  
  7. | query_cache_min_res_unit     | 4096    |   
  8. | query_cache_size             | 599040  | --查詢緩存的大小  
  9. | query_cache_type             | ON      | --阻止或是支持查詢緩存  
  10. | query_cache_wlock_invalidate | OFF     |   
  11. +------------------------------+---------+  

下面是一個(gè)簡(jiǎn)單的MySQL查詢緩存機(jī)制例子:

  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.  
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.  
  8.  
  9. mysql> set global query_cache_size = 600000; --設(shè)置緩存內(nèi)存  
  10. Query OK, 0 rows affected (0.00 sec)  
  11.  
  12. mysql> set session query_cache_type = ON; --開啟查詢緩存  
  13. Query OK, 0 rows affected (0.00 sec)  
  14.  
  15. mysql> use test  
  16. Reading table information for completion of table and column names  
  17. You can turn off this feature to get a quicker startup with -A  
  18.  
  19. Database changed  
  20. mysql> show tables;  
  21. +----------------+  
  22. | Tables_in_test |  
  23. +----------------+  
  24. | animals        |   
  25. | person         |   
  26. +----------------+  
  27. 5 rows in set (0.00 sec)  
  28.  
  29. mysql> select count(*) from animals;  
  30. +----------+  
  31. | count(*) |  
  32. +----------+  
  33. |        6 |   
  34. +----------+  
  35. 1 row in set (0.00 sec)  
  36.  
  37. --Qcache_hits表示sql查詢?cè)诰彺嬷忻械睦塾?jì)次數(shù),是累加值。  
  38. mysql> SHOW STATUS LIKE 'Qcache_hits';  
  39. +---------------+-------+  
  40. | Variable_name | Value |  
  41. +---------------+-------+  
  42. | Qcache_hits   | 0     |  --0次  
  43. +---------------+-------+  
  44. 8 rows in set (0.00 sec)  
  45.  
  46. mysql>  select count(*) from animals;  
  47. +----------+  
  48. | count(*) |  
  49. +----------+  
  50. |        6 |   
  51. +----------+  
  52. 1 row in set (0.00 sec)  
  53.  
  54. mysql>  SHOW STATUS LIKE 'Qcache%';  
  55. +---------------+-------+  
  56. | Variable_name | Value |  
  57. +---------------+-------+  
  58. | Qcache_hits   | 1     | --表示sql在緩存中直接得到結(jié)果,不需要再去解析  
  59. +---------------+-------+  
  60. 8 rows in set (0.00 sec)  
  61.  
  62. mysql> select count(*) from animals;  
  63. +----------+  
  64. | count(*) |  
  65. +----------+  
  66. |        6 |   
  67. +----------+  
  68. 1 row in set (0.00 sec)  
  69.  
  70. mysql> select count(*) from animals;  
  71. +----------+  
  72. | count(*) |  
  73. +----------+  
  74. |        6 |   
  75. +----------+  
  76. 1 row in set (0.00 sec)  
  77.  
  78. mysql> SHOW STATUS LIKE 'Qcache_hits';  
  79. +---------------+-------+  
  80. | Variable_name | Value |  
  81. +---------------+-------+  
  82. | Qcache_hits   | 3     |    --上面的sql也是是從緩存中直接取到結(jié)果  
  83. +---------------+-------+  
  84. 1 row in set (0.00 sec)  
  85.  
  86. mysql> insert into animals select 9,'testsds' ; --插入數(shù)據(jù)后,跟這個(gè)表所有相關(guān)的sql緩存就會(huì)被清空掉  
  87. Query OK, 1 row affected (0.00 sec)  
  88. Records: 1  Duplicates: 0  Warnings: 0  
  89.  
  90. mysql> select count(*) from animals;  
  91. +----------+  
  92. | count(*) |  
  93. +----------+  
  94. |        7 |   
  95. +----------+  
  96. 1 row in set (0.00 sec)  
  97.  
  98. mysql> SHOW STATUS LIKE 'Qcache_hits';  
  99. +---------------+-------+  
  100. | Variable_name | Value |  
  101. +---------------+-------+  
  102. | Qcache_hits   | 3    |  --還是等于3,說明上一條sql是沒有直接從緩存中直接得到的  
  103. +---------------+-------+  
  104. 1 row in set (0.00 sec)  
  105.  
  106. mysql> select count(*) from animals;  
  107. +----------+  
  108. | count(*) |  
  109. +----------+  
  110. |        7 |   
  111. +----------+  
  112. 1 row in set (0.00 sec)  
  113.  
  114. mysql> SHOW STATUS LIKE 'Qcache_hits';   
  115. +---------------+-------+  
  116. | Variable_name | Value |  
  117. +---------------+-------+  
  118. | Qcache_hits   | 4     |   
  119. +---------------+-------+  
  120. 1 row in set (0.00 sec)  

 

 

 

【編輯推薦】

MySQL無重復(fù)查詢的實(shí)現(xiàn)

五種常用的MySQL命令行

修復(fù)mysql表的兩種方法

php中數(shù)組插入mysql表的方法

MySQL條件查詢語句的用法

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

2010-10-13 16:44:10

MySQL查詢緩存機(jī)制

2024-12-20 16:46:22

Spring三級(jí)緩存

2011-08-02 18:07:03

iPhone 內(nèi)省 Cocoa

2025-02-25 10:21:15

2025-03-27 04:10:00

2011-06-22 16:50:09

Qt 進(jìn)程 通信機(jī)制

2016-10-09 14:41:40

Swift開發(fā)ARC

2010-10-14 15:59:30

MySQL查詢緩存變量

2024-05-06 00:00:00

GAC代碼緩存

2010-11-23 11:36:15

MySQL創(chuàng)建關(guān)聯(lián)表

2024-11-14 07:10:00

2025-01-20 08:20:00

redo logMySQL數(shù)據(jù)庫

2009-09-17 09:11:26

LINQ查詢

2019-07-11 08:45:00

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

2010-11-25 10:00:33

MySQL查詢緩存

2009-06-17 15:43:03

Hibernate緩存

2023-02-24 16:46:25

Glide緩存機(jī)制

2010-06-02 13:33:19

MySQL 查詢緩存

2010-05-19 12:44:58

2020-12-21 09:00:04

MySQL緩存SQL
點(diǎn)贊
收藏

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