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

淺析MySQL數據碎片的產生

原創(chuàng)
運維 數據庫運維 MySQL
MySQL列表,包括MyISAM和InnoDB這兩種最常見的類型,而根據經驗來說,其碎片的產生及消除都是隨機的。碎片會在你的表格中留下明顯的空白,而這會給列表掃描工作帶來相當大的困擾。對你的列表進行優(yōu)化,這樣會使列表的全面及分區(qū)掃描工作進行得更有效率。

【51CTO獨家譯文】本文淺析MySQL數據碎片的產生:定義,時間及成因。

MySQL列表,包括MyISAMInnoDB這兩種最常見的類型,而根據經驗來說,其碎片的產生及消除都是隨機的。碎片會在你的表格中留下明顯的空白,而這會給列表掃描工作帶來相當大的困擾。對你的列表進行優(yōu)化,這樣會使列表的全面及分區(qū)掃描工作進行得更有效率。

碎片——實例

MySQL具有相當多不同種類的存儲引擎來實現列表中的數據存儲功能。每當MySQL從你的列表中刪除了一行內容,該段空間就會被留空。而在一段時間內的大量刪除操作,會使這種留空的空間變得比存儲列表內容所使用的空間更大。當MySQL對數據進行掃描時,它掃描的對象實際是列表的容量需求上限,也就是數據被寫入的區(qū)域中處于峰值位置的部分。如果進行新的插入操作,MySQL將嘗試利用這些留空的區(qū)域,但仍然無法將其徹底占用。

這種額外的破碎的存儲空間在讀取效率方面比正常占用的空間要低得多。讓我們看一個實例。

我們將創(chuàng)建一個數據庫(有時也稱其為大綱)及一個測試用的列表:

  1. (root@localhost) [test]> create database frag_test;  
  2. Query OK, 1 row affected (0.03 sec)  
  3.  
  4. (root@localhost) [test]> use frag_test;  
  5. Database changed  
  6.  
  7. (root@localhost) [frag_test]> create table frag_test (c1 varchar(64));  
  8. Query OK, 0 rows affected (0.05 sec) 

現在讓我們在列表中加入如下幾行:

  1. (root@localhost) [frag_test]> insert into frag_test values ('this is row 1');  
  2. Query OK, 1 row affected (0.01 sec)  
  3.  
  4. (root@localhost) [frag_test]> insert into frag_test values ('this is row 2');  
  5. Query OK, 1 row affected (0.00 sec)  
  6.  
  7. (root@localhost) [frag_test]> insert into frag_test values ('this is row 3');  
  8. Query OK, 1 row affected (0.00 sec) 

現在我們進行碎片查看:

  1. (root@localhost) [frag_test]> show table status from frag_test\G;  
  2. *************************** 1. row ***************************  
  3.            Name: frag_test  
  4.          Engine: MyISAM  
  5.         Version: 10  
  6.      Row_format: Dynamic 
  7.            Rows: 3  
  8.  Avg_row_length: 20  
  9.     Data_length: 60  
  10. Max_data_length: 281474976710655  
  11.    Index_length: 1024  
  12.       Data_free: 0  
  13.  Auto_increment: NULL 
  14.     Create_time: 2011-02-23 14:55:27  
  15.     Update_time: 2011-02-23 15:06:55  
  16.      Check_time: NULL 
  17.       Collation: latin1_swedish_ci  
  18.        Checksum: NULL 
  19.  Create_options:   
  20.         Comment:   
  21. 1 row in set (0.00 sec) 

現在我們刪除一行,并再次檢測:

  1. (root@localhost) [frag_test]> delete from frag_test where c1 = 'this is row 2';  
  2. Query OK, 1 row affected (0.00 sec)  
  3.  
  4. (root@localhost) [frag_test]> show table status from frag_test\G;  
  5. *************************** 1. row ***************************  
  6.            Name: frag_test  
  7.          Engine: MyISAM  
  8.         Version: 10  
  9.      Row_format: Dynamic 
  10.            Rows: 2  
  11.  Avg_row_length: 20  
  12.     Data_length: 60  
  13. Max_data_length: 281474976710655  
  14.    Index_length: 1024  
  15.       Data_free: 20  
  16.  Auto_increment: NULL 
  17.     Create_time: 2011-02-23 14:55:27  
  18.     Update_time: 2011-02-23 15:07:49  
  19.      Check_time: NULL 
  20.       Collation: latin1_swedish_ci  
  21.        Checksum: NULL 
  22.  Create_options:   
  23.         Comment:   
  24. 1 row in set (0.00 sec) 

需要注意的是,“data_free”一欄顯示出了我們刪除第二行后所產生的留空空間。想象一下如果你有兩萬行指令的話,結果是什么樣的。以此推算,它們將耗費四十萬字節(jié)的存儲空間?,F在如果你將兩萬條命令行刪到只剩一行,列表中有用的內容將只占二十字節(jié),但MySQL在讀取中會仍然將其視同于一個容量為四十萬字節(jié)的列表進行處理,并且除二十字節(jié)以外,其它空間都被白白浪費了。

清理碎片

幸運的是一旦你鎖定了這一問題,MySQL提供了一種簡便的修正方法。這就是所謂的優(yōu)化列表,具體內容如下:

  1. (root@localhost) [frag_test]> optimize table frag_test;  
  2. +---------------------+----------+----------+----------+  
  3. Table               | Op       | Msg_type | Msg_text |  
  4. +---------------------+----------+----------+----------+  
  5. | frag_test.frag_test | optimize | status   | OK       |   
  6. +---------------------+----------+----------+----------+  
  7. 1 row in set (0.00 sec)  
  8.  
  9. (root@localhost) [frag_test]> show table status from frag_test\G;  
  10. *************************** 1. row ***************************  
  11.            Name: frag_test  
  12.          Engine: MyISAM  
  13.         Version: 10  
  14.      Row_format: Dynamic 
  15.            Rows: 2  
  16.  Avg_row_length: 20  
  17.     Data_length: 40  
  18. Max_data_length: 281474976710655  
  19.    Index_length: 1024  
  20.       Data_free: 0  
  21.  Auto_increment: NULL 
  22.     Create_time: 2011-02-23 14:55:27  
  23.     Update_time: 2011-02-23 15:11:05  
  24.      Check_time: 2011-02-23 15:11:05  
  25.       Collation: latin1_swedish_ci  
  26.        Checksum: NULL 
  27.  Create_options:   
  28.         Comment:   
  29. 1 row in set (0.00 sec) 

性能考量

 “優(yōu)化列表”功能在進行中會對整個列表進行鎖定。對于小型列表,這一功能的效果非常好,因為整個列表的讀取和修改速度都會很快。但對于那些體積巨大的列表來說,這一過程將消耗很長時間,并且其間會中斷或減少可用的應用程序數量。怎么辦?

再一次,MySQL幸運地提供了一項堪稱偉大的功能,名為“主-主復制”。在這種配置之下,你的后臺數據庫實際上成為兩個單獨的數據庫,一個主動可調用的,一個被動可調整的。這兩個數據庫在各方面來說都是完全相同的。要實現各種在線操作——包括“優(yōu)化列表”操作——只需在你的被動數據庫中即可進行。這將不會對你的應用程序造成絲毫影響。一旦優(yōu)化操作完成,主、被動數據庫將互相轉換,以便應用程序直接指向二號數據庫,對還未進行優(yōu)化的主動數據庫部分自動開始優(yōu)化工作。

這時,兩套數據庫的角色已經互換,而應用程序也將順利指向二號數據庫,執(zhí)行與在一號數據庫上相同的列表優(yōu)化。而現在主動已經轉換為被動,因此不會中斷主要任務處理。

其它命令

顯示你數據庫中存在碎片的全部列表:

  1. (root@localhost) [(none)]> select table_schema, table_name, data_free, engine from information_schema.tables where table_schema not in ('information_schema''mysql'and data_free > 0;  
  2. +--------------+-----------------------------+-----------+--------+  
  3. | table_schema | table_name                  | data_free | engine |  
  4. +--------------+-----------------------------+-----------+--------+  
  5. | aitc         | wp_comments                 |    346536 | MyISAM |   
  6. | aitc         | wp_options                  |     64308 | MyISAM |   
  7. | aitc         | wp_postmeta                 |       124 | MyISAM |   
  8. | cactidb      | poller_item                 |       160 | MyISAM |   
  9. | cactidb      | poller_output               |       384 | MyISAM |   
  10. | drupal       | sessions                    |     30976 | MyISAM |   
  11. | drupal       | users                       |        92 | MyISAM |   
  12. | drupal       | variable                    |        20 | MyISAM |   
  13. | gg           | wp_comments                 |       232 | MyISAM |   
  14. | gg           | wp_options                  |       696 | MyISAM |   
  15. | gg           | wp_postmeta                 |       560 | MyISAM |   
  16. | ihi          | wp_comments                 |       536 | MyISAM |   
  17. | ihi          | wp_options                  |       444 | MyISAM |   
  18. | ihi          | wp_postmeta                 |       288 | MyISAM |   
  19. | ihi          | wp_redirection_items        |      1292 | MyISAM |   
  20. | ihi          | wp_redirection_logs         |    140352 | MyISAM |   
  21. | nds          | wp_comments                 |      4704 | MyISAM |   
  22. | nds          | wp_options                  |    150580 | MyISAM |   
  23. | nds          | wp_postmeta                 |        76 | MyISAM |   
  24. | oos          | wp_comments                 |    317124 | MyISAM |   
  25. | oos          | wp_options                  |     88196 | MyISAM |   
  26. | oos          | wp_postmeta                 |        76 | MyISAM |   
  27. | phplist      | phplist_listuser            |       252 | MyISAM |   
  28. | phplist      | phplist_sendprocess         |        52 | MyISAM |   
  29. | phplist      | phplist_user_user           |     32248 | MyISAM |   
  30. | phplist      | phplist_user_user_attribute |       120 | MyISAM |   
  31. | phplist      | phplist_user_user_history   |       288 | MyISAM |   
  32. | phplist      | phplist_usermessage         |      1428 | MyISAM |   
  33. | pn_nds       | nuke_session_info           |     12916 | MyISAM |   
  34. | psa          | exp_event                   |     10024 | MyISAM |   
  35. | test         | active_sessions             |     30144 | MyISAM |   
  36. +--------------+-----------------------------+-----------+--------+  
  37. 31 rows in set (0.26 sec) 

如果你更改了某個列表的存儲引擎,你也應該對這一列表進行碎片清理。這是因為MySQL的工作原理導致其必須讀取整個列表,然后利用新的存儲引擎將內容寫回磁盤,而在此過程中碎片所在的位置及影響到的數據都對執(zhí)行效率造成了嚴重的不良影響。

上述情況如下所示:

  1. (root@localhost) [frag_test]> alter table frag_test engine = innodb;  
  2. Query OK, 2 rows affected (0.17 sec)  
  3. Records: 2  Duplicates: 0  Warnings: 0  
  4.  
  5. (root@localhost) [frag_test]> show table status from frag_test  
  6.     -> \G;  
  7. *************************** 1. row ***************************  
  8.            Name: frag_test  
  9.          Engine: InnoDB  
  10.         Version: 10  
  11.      Row_format: Compact  
  12.            Rows: 2  
  13.  Avg_row_length: 8192  
  14.     Data_length: 16384  
  15. Max_data_length: 0  
  16.    Index_length: 0  
  17.       Data_free: 0  
  18.  Auto_increment: NULL 
  19.     Create_time: 2011-02-23 15:41:12  
  20.     Update_time: NULL 
  21.      Check_time: NULL 
  22.       Collation: latin1_swedish_ci  
  23.        Checksum: NULL 
  24.  Create_options:   
  25.         Comment: InnoDB free: 7168 kB  
  26. 1 row in set (0.00 sec) 

結論

如果你發(fā)現一些列表中包含了大量的數據留空現象,那么對其進行優(yōu)化是絕對值得的,因為這一過程會大大提升列表的讀取性能及應用表現。

原文地址:http://www.databasejournal.com/features/mysql/article.php/3927871/article.htm

 

【編輯推薦】

  1. MySQL數據庫的優(yōu)化(下)MySQL數據庫的高可用架構方案
  2. MySQL數據庫的優(yōu)化(上)單機MySQL數據庫的優(yōu)化
  3. MySQL技巧:結合相關參數 做好Limit優(yōu)化
  4. 詳解MySQL limit查詢優(yōu)化的實際操作步驟
責任編輯:艾婧 來源: 51CTO
相關推薦

2020-07-07 07:57:39

Linux內存碎片化

2011-04-21 16:28:11

噴墨打印機

2012-10-25 14:35:04

OpenStack

2009-07-31 18:16:09

ASP.NET中的Se

2010-06-07 13:30:15

2017-09-14 10:10:55

數據庫MySQL架構

2024-07-05 09:19:52

2010-05-20 10:00:58

MySQL 中文亂碼

2013-08-02 11:24:47

Android碎片化圖解生態(tài)碎Androi

2009-07-10 18:02:05

MyEclipseMySQL

2021-06-28 10:25:47

MySQL語句接口

2021-06-28 10:00:32

JDBC數據庫MySQL

2023-08-06 23:26:39

2010-05-13 09:59:50

MySQL數據庫

2010-05-31 10:45:09

MySQL+tomca

2024-07-16 08:03:43

2011-06-09 18:05:00

QT MySql

2022-05-24 07:39:09

MySQL數據庫日志

2018-02-02 13:58:59

數據存儲

2011-05-19 13:25:12

Oracle數據庫碎片
點贊
收藏

51CTO技術棧公眾號