innodb_space 的git網址:https://github.com/jeremycole...node
# yum -y install ruby # gem install innodb_ruby
安裝完成後,執行以下命令驗證innodb_ruby是否安裝成功:# innodb_space --helpmysql
innodb_file_per_table=ON,innodb_file_format=Barracuda, innodb_file_format_max=Barracudagit
Against a single space file (ibdata or .ibd): Option Parameters Description -f <filename> Load the tablespace file (system or table) Against a system tablespace which will auto-load file-per-table tablespace files: Option Parameters Description -s <filename> Load the system tablespace file (e.g. ibdata1) -T <table name> Use the given table name. -I <index name> Use the given index name
雖然每一個innodb表都有frm文件,不過innodb在系統表空間中ibdata1也維護着innodb表的元數據,因此直接分析ibdata1文件便可瞭解某個表的相關存儲信息
表結構:github
CREATE TABLE `aa` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '地區ID', `name` varchar(20) NOT NULL DEFAULT '' COMMENT '名稱', `rel_id` varchar(50) NOT NULL DEFAULT '' COMMENT '關係ID', `pid` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '父ID', `level` int(11) NOT NULL DEFAULT '0' COMMENT '類別,一、省份 二、市 三、區 四、縣', PRIMARY KEY (`id`), UNIQUE KEY `UNQ_RID` (`rel_id`) USING BTREE, KEY `IDX_PID` (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='地區表' 1 row in set (0.00 sec)
表信息:sql
innodb_space -s ibdata1 system-spaces //列出系統中可用的全部表空間,包括一些基本的統計信息。這基本上是一個表的列表:
innodb_space -s ibdata1 system-spacesruby
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 system-spaces name pages indexes (system) 4864 6 monitor/monitor 6 1 monitor/db_monitor 6 1 monitor/monitor 6 1 mysql/innodb_index_stats 6 1 mysql/innodb_table_stats 6 1 mysql/slave_master_info 6 1 mysql/slave_relay_log_info 6 1 mysql/slave_worker_info 6 1 opsdb/myapp_db_instance 8 1 test/aaa 6 1 test/area 8 3 ztj/aa 9 3 ztj/aaa 8 3 ztj/dim_library 4608 4
//查看數據分佈
innodb_space -s ibdata1 -T ztj/aa space-page-type-regionsapp
[root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aa space-page-type-regions start end count type 0 0 1 FSP_HDR 1 1 1 IBUF_BITMAP 2 2 1 INODE 3 5 3 INDEX //索引分佈在page:3/4/5,其中3保存Primary信息,4保存UNQ_RID信息,5保存IDX_PID信息 6 7 2 FREE (ALLOCATED)
對於表的索引的名稱,也能夠經過information_schema庫表innodb_sys_indexes、innodb_sys_tables查看:spa
mysql> SELECT -> b.name, a.name, index_id, type, a.space, a.PAGE_NO -> FROM -> information_schema.INNODB_SYS_INDEXES a, -> information_schema.INNODB_SYS_TABLES b -> WHERE -> a.table_id = b.table_id AND a.space <> 0; +------------------------------+-------------------------------------------+----------+------+-------+---------+ | name | name | index_id | type | space | PAGE_NO | +------------------------------+-------------------------------------------+----------+------+-------+---------+ | monitor/ll_monitor | GEN_CLUST_INDEX | 21 | 1 | 5 | 3 | | monitor/db_monitor | GEN_CLUST_INDEX | 22 | 1 | 6 | 3 | | monitor/monitor | GEN_CLUST_INDEX | 20 | 1 | 4 | 3 | | mysql/innodb_index_stats | PRIMARY | 23 | 3 | 7 | 3 | | mysql/innodb_table_stats | PRIMARY | 24 | 3 | 8 | 3 | | mysql/slave_master_info | PRIMARY | 25 | 3 | 9 | 3 | | mysql/slave_relay_log_info | PRIMARY | 26 | 3 | 10 | 3 | | mysql/slave_worker_info | PRIMARY | 47 | 3 | 18 | 3 | | ztj/aa | UNQ_RID | 51 | 2 | 19 | 4 | | ztj/aa | IDX_PID | 62 | 0 | 19 | 6 | | ztj/aaa | PRIMARY | 63 | 3 | 25 | 3 | | ztj/aaa | UNQ_RID | 64 | 2 | 25 | 4 | | ztj/aaa | IDX_PID | 65 | 0 | 25 | 5 | +------------------------------+-------------------------------------------+----------+------+-------+---------+
//索引結構、數據分配狀況
innodb_space -s ibdata1 -T ztj/aa space-indexes指針
[root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aa space-indexes id name root fseg used allocated fill_factor 50 PRIMARY 3 internal 1 1 100.00% 50 PRIMARY 3 leaf 0 0 0.00% 51 UNQ_RID 4 internal 1 1 100.00% 51 UNQ_RID 4 leaf 0 0 0.00% 52 IDX_PID 5 internal 1 1 100.00% 52 IDX_PID 5 leaf 0 0 0.00%
//查看錶pages的佔比
innodb_space -s ../ibdata1 -T ztj/aa space-page-type-summarycode
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aa space-page-type-summary type count percent description INDEX 3 37.50 B+Tree index ALLOCATED 2 25.00 Freshly allocated INODE 1 12.50 File segment inode IBUF_BITMAP 1 12.50 Insert buffer bitmap FSP_HDR 1 12.50 File space header
//查看錶的索引分佈在pages的狀況
innodb_space -s ibdata1 -T ztj/aaa space-index-pages-summar |head -n 10
innodb_space -f ztj/aaa.ibd space-index-pages-summary |head -n 10
[root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aaa space-index-pages-summary |head -n 10 page index level data free records 3 63 0 948 15298 16 //主鍵B+樹的高度爲page level+1=1 4 64 0 380 15866 16 5 65 0 208 16040 16 6 0 0 0 16384 0 7 0 0 0 16384 0 [root@vm-test01 ztj]# innodb_space -f dim_library.ibd space-index-pages-summary |head -n 10 page index level data free records 3 28 2 45 16207 3 //主鍵B+樹索引的高度爲page level+1=3 4 29 1 8470 7598 385 5 30 1 11487 4575 547 6 31 1 7980 8122 380 7 28 0 7526 8690 72 8 28 0 15137 1045 144 9 28 0 15074 1108 144 10 28 0 15110 1072 144 11 28 0 15104 1080 143
//查看該page的說明信息
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aa -p 3 page-account Accounting for page 3: Page type is INDEX (B+Tree index, table and index data stored in B+Tree structure). Extent descriptor for pages 0-63 is at page 0, offset 158. Extent is not fully allocated to an fseg; may be a fragment extent. Page is marked as used in extent descriptor. Extent is in free_frag list of space. Page is in fragment array of fseg 1. Fseg is in internal fseg of index 63. Index root is page 3. Index is ztj /aa.PRIMARY. [root@vm-test01 ztj]# [root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aa -p 4 page-account Accounting for page 4: Page type is INDEX (B+Tree index, table and index data stored in B+Tree structure). Extent descriptor for pages 0-63 is at page 0, offset 158. Extent is not fully allocated to an fseg; may be a fragment extent. Page is marked as used in extent descriptor. Extent is in free_frag list of space. Page is in fragment array of fseg 3. Fseg is in internal fseg of index 64. Index root is page 4. Index is ztj/aa.UNQ_RID.
//查看輔助索引存儲信息
innodb_space -s ibdata1 -T ztj/aa -p 4 page-records
[root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aa -p 4 page-records Record 126: (rel_id="10000") → (id=10000) Record 141: (rel_id="1000010001") → (id=10001) Record 161: (rel_id="100001000110002") → (id=10002) Record 186: (rel_id="100001000110003") → (id=10003) Record 211: (rel_id="100001000110004") → (id=10004) Record 236: (rel_id="100001000110005") → (id=10005) Record 261: (rel_id="100001000110006") → (id=10006) Record 286: (rel_id="100001000110007") → (id=10007) Record 311: (rel_id="100001000110008") → (id=10008) Record 336: (rel_id="1000010009") → (id=10009) Record 356: (rel_id="100001000910010") → (id=10010) Record 381: (rel_id="100001000910011") → (id=10011) Record 406: (rel_id="100001000910012") → (id=10012) Record 431: (rel_id="100001000910013") → (id=10013) Record 456: (rel_id="100001000910014") → (id=10014) Record 481: (rel_id="100001000910015") → (id=10015) innodb_space -s ibdata1 -T ztj/aa -p 5 page-records [root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aa -p 5 page-records Record 125: (pid=0) → (id=10000) Record 138: (pid=10000) → (id=10001) Record 151: (pid=10000) → (id=10009) Record 164: (pid=10001) → (id=10002) Record 177: (pid=10001) → (id=10003) Record 190: (pid=10001) → (id=10004) Record 203: (pid=10001) → (id=10005) Record 216: (pid=10001) → (id=10006) Record 229: (pid=10001) → (id=10007) Record 242: (pid=10001) → (id=10008) Record 255: (pid=10009) → (id=10010) Record 268: (pid=10009) → (id=10011) Record 281: (pid=10009) → (id=10012) Record 294: (pid=10009) → (id=10013) Record 307: (pid=10009) → (id=10014) Record 320: (pid=10009) → (id=10015)
//主鍵索引的存儲信息
innodb_space -s ibdata1 -T ztj/aa -p 3 page-records
[root@vm-test01 data]# innodb_space -s ibdata1 -T ztj/aa -p 3 page-records Record 127: (id=10000) → (name="\xE5\x8C\x97\xE4\xBA\xAC", rel_id="10000", pid=0, level=1) Record 170: (id=10001) → (name="\xE6\x9C\x9D\xE9\x98\xB3\xE5\x8C\xBA", rel_id="1000010001", pid=10000, level=2) Record 221: (id=10002) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE4\xBB\xA5\xE5\x86\x85", rel_id="100001000110002", pid=10001, level=3) Record 280: (id=10003) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE5\x88\xB0\xE5\x9B\x9B\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110003", pid=10001, level=3) Record 348: (id=10004) → (name="\xE5\x9B\x9B\xE7\x8E\xAF\xE5\x88\xB0\xE4\xBA\x94\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110004", pid=10001, level=3) Record 416: (id=10005) → (name="\xE4\xBA\x94\xE7\x8E\xAF\xE5\x88\xB0\xE5\x85\xAD\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110005", pid=10001, level=3) Record 484: (id=10006) → (name="\xE7\xAE\xA1\xE5\xBA\x84", rel_id="100001000110006", pid=10001, level=3) Record 537: (id=10007) → (name="\xE5\x8C\x97\xE8\x8B\x91", rel_id="100001000110007", pid=10001, level=3) Record 590: (id=10008) → (name="\xE5\xAE\x9A\xE7\xA6\x8F\xE5\xBA\x84", rel_id="100001000110008", pid=10001, level=3) Record 646: (id=10009) → (name="\xE6\xB5\xB7\xE6\xB7\x80\xE5\x8C\xBA", rel_id="1000010009", pid=10000, level=2) Record 697: (id=10010) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE4\xBB\xA5\xE5\x86\x85", rel_id="100001000910010", pid=10009, level=3) Record 756: (id=10011) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE5\x88\xB0\xE5\x9B\x9B\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910011", pid=10009, level=3) Record 824: (id=10012) → (name="\xE5\x9B\x9B\xE7\x8E\xAF\xE5\x88\xB0\xE4\xBA\x94\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910012", pid=10009, level=3) Record 892: (id=10013) → (name="\xE4\xBA\x94\xE7\x8E\xAF\xE5\x88\xB0\xE5\x85\xAD\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910013", pid=10009, level=3) Record 960: (id=10014) → (name="\xE5\x85\xAD\xE7\x8E\xAF\xE4\xBB\xA5\xE5\xA4\x96", rel_id="100001000910014", pid=10009, level=3) Record 1019: (id=10015) → (name="\xE8\xA5\xBF\xE4\xB8\x89\xE6\x97\x97", rel_id="100001000910015", pid=10009, level=3)
結論:輔助索引存儲了主鍵值。主鍵page存儲了全部數據
//遞歸整個B+樹,掃描全部頁面
innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY index-recurse
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY index-recurse ROOT NODE #3: 16 records, 948 bytes RECORD: (id=10000) → (name="\xE5\x8C\x97\xE4\xBA\xAC", rel_id="10000", pid=0, level=1) RECORD: (id=10001) → (name="\xE6\x9C\x9D\xE9\x98\xB3\xE5\x8C\xBA", rel_id="1000010001", pid=10000, level=2) RECORD: (id=10002) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE4\xBB\xA5\xE5\x86\x85", rel_id="100001000110002", pid=10001, level=3) RECORD: (id=10003) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE5\x88\xB0\xE5\x9B\x9B\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110003", pid=10001, level=3) RECORD: (id=10004) → (name="\xE5\x9B\x9B\xE7\x8E\xAF\xE5\x88\xB0\xE4\xBA\x94\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110004", pid=10001, level=3) RECORD: (id=10005) → (name="\xE4\xBA\x94\xE7\x8E\xAF\xE5\x88\xB0\xE5\x85\xAD\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000110005", pid=10001, level=3) RECORD: (id=10006) → (name="\xE7\xAE\xA1\xE5\xBA\x84", rel_id="100001000110006", pid=10001, level=3) RECORD: (id=10007) → (name="\xE5\x8C\x97\xE8\x8B\x91", rel_id="100001000110007", pid=10001, level=3) RECORD: (id=10008) → (name="\xE5\xAE\x9A\xE7\xA6\x8F\xE5\xBA\x84", rel_id="100001000110008", pid=10001, level=3) RECORD: (id=10009) → (name="\xE6\xB5\xB7\xE6\xB7\x80\xE5\x8C\xBA", rel_id="1000010009", pid=10000, level=2) RECORD: (id=10010) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE4\xBB\xA5\xE5\x86\x85", rel_id="100001000910010", pid=10009, level=3) RECORD: (id=10011) → (name="\xE4\xB8\x89\xE7\x8E\xAF\xE5\x88\xB0\xE5\x9B\x9B\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910011", pid=10009, level=3) RECORD: (id=10012) → (name="\xE5\x9B\x9B\xE7\x8E\xAF\xE5\x88\xB0\xE4\xBA\x94\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910012", pid=10009, level=3) RECORD: (id=10013) → (name="\xE4\xBA\x94\xE7\x8E\xAF\xE5\x88\xB0\xE5\x85\xAD\xE7\x8E\xAF\xE4\xB9\x8B\xE9\x97\xB4", rel_id="100001000910013", pid=10009, level=3) RECORD: (id=10014) → (name="\xE5\x85\xAD\xE7\x8E\xAF\xE4\xBB\xA5\xE5\xA4\x96", rel_id="100001000910014", pid=10009, level=3) RECORD: (id=10015) → (name="\xE8\xA5\xBF\xE4\xB8\x89\xE6\x97\x97", rel_id="100001000910015", pid=10009, level=3)
//像index-record同樣遞歸整個索引樹,不過只打印出index page的偏移量
innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY index-record-offsets
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY index-record-offsets page_offset record_offset 3 127 3 170 3 221 3 280 3 348 3 416 3 484 3 537 3 590 3 646 3 697 3 756 3 824 3 892 3 960 3 1019
//遞歸整個B+樹,掃描全部頁面
innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY index-recurse
[root@vm-test01 ztj]# innodb_space -s ../ibdata1 -T ztj/aaa -I PRIMARY -p 3 -R 127 record-history Transaction Type Undo record (n/a) insert (id=10000) → ()
page內的數據:邏輯上有序的,物理磁盤上⼀個page內是⽆序的,以指針的形式鏈接數據,達到邏輯有序 【正常1, 2, 3, 4, 5. 存儲是:1, 3 ,2, 5, 4。1連接2連接3鏈接4連接5】⻚和⻚之間也是:邏輯上有序,物理上⽆序。 ⾃增ID,邏輯上也是有序的。