前言
InnoDB采用按表空間(tablespace)的方式進(jìn)行存儲(chǔ)數(shù)據(jù), 默認(rèn)配置情況下會(huì)有一個(gè)初始大小為10MB, 名字為ibdata1的文件, 該文件就是默認(rèn)的表空間文件(tablespce file),用戶(hù)可以通過(guò)參數(shù)innodb_data_file_path對(duì)其進(jìn)行設(shè)置,可以有多個(gè)數(shù)據(jù)文件,如果沒(méi)有設(shè)置innodb_file_per_table的話(huà), 那些Innodb存儲(chǔ)類(lèi)型的表的數(shù)據(jù)都放在這個(gè)共享表空間中,而系統(tǒng)變量innodb_file_per_table=1的話(huà),那么InnoDB存儲(chǔ)引擎類(lèi)型的表就會(huì)產(chǎn)生一個(gè)獨(dú)立表空間,獨(dú)立表空間的命名規(guī)則為:表名.idb. 這些單獨(dú)的表空間文件僅存儲(chǔ)該表的數(shù)據(jù)、索引和插入緩沖BITMAP等信息,其它信息還是存放在共享表空間中,那么如何判別數(shù)據(jù)庫(kù)中哪些表是獨(dú)立表空間,哪些表是共享表空間呢?
InnoDB邏輯存儲(chǔ)結(jié)構(gòu)

方法1:通過(guò)ibd文件判別
如果表的存儲(chǔ)引擎是InnoDB,而且表空間(tablespace)是共享表空間的話(huà),那么數(shù)據(jù)庫(kù)對(duì)應(yīng)目錄下面是沒(méi)有"表名.ibd"文件的。獨(dú)立表空間的表的話(huà),則有"表名.ibd"文件。只是這個(gè)方法很笨,對(duì)于生產(chǎn)環(huán)境,大量的表通過(guò)這種方式判別,確實(shí)不是一個(gè)好方法。
| mysql> show variables like 'innodb_file_per_table';+-----------------------+-------+| Variable_name | Value |+-----------------------+-------+| innodb_file_per_table | ON |+-----------------------+-------+1 row in set (0.01 sec) mysql> use MyDB;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> create table Independent_tablespace(name varchar(64));Query OK, 0 rows affected (0.03 sec) mysql> exit [root@DB-Server ~]# cd /data/mysql/MyDB/[root@DB-Server MyDB]# ls -lrt Independent_tablespace*-rw-rw---- 1 mysql mysql 8560 Aug 21 22:05 Independent_tablespace.frm-rw-rw---- 1 mysql mysql 98304 Aug 21 22:05 Independent_tablespace.ibd[root@DB-Server MyDB]# | 
在配置文件my.cnf里面設(shè)置innodb_file_per_table=0,重啟MySQL服務(wù),創(chuàng)建表common_tablespace,你會(huì)在數(shù)據(jù)目錄看到只有common_tablespace.frm文件。
| mysql> show variables like 'innodb_file_per_table';+-----------------------+-------+| Variable_name | Value |+-----------------------+-------+| innodb_file_per_table | OFF |+-----------------------+-------+1 row in set (0.00 sec) mysql> use MyDB;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> create table common_tablespace(name varchar(64));Query OK, 0 rows affected (0.02 sec) mysql> exitBye[root@DB-Server MyDB]# ls -lrt common_tablespace*-rw-rw---- 1 mysql mysql 8560 Aug 21 22:08 common_tablespace.frm[root@DB-Server MyDB]# |