--MySQL表空間集
----------------------2014-09-20
1. 收縮ibdata的方法,目前MySQL依然沒有提供收縮ibdata的方法,只能重構,下面是5.7的步驟。html
Decreasing the Size of the InnoDB Tablespace Currently, you cannot remove a data file from the system tablespace. To decrease the system tablespace size, use this procedure: 1. Use mysqldump to dump all your InnoDB tables, including InnoDB tables located in the MySQL database. As of 5.6, there are five InnoDB tables included in the MySQL database: mysql> select table_name from information_schema.tables where table_schema='mysql' and engine='InnoDB'; +----------------------+ | table_name | +----------------------+ | innodb_index_stats | | innodb_table_stats | | slave_master_info | | slave_relay_log_info | | slave_worker_info | +----------------------+ 5 rows in set (0.00 sec) Stop the server. 2. Remove all the existing tablespace files (*.ibd), including the ibdata and ib_log files. Do not forget to remove *.ibd files for tables located in the MySQL database. 3. Remove any .frm files for InnoDB tables. 4. Configure a new tablespace. 5. Restart the server. 6. Import the dump files.
2. 再看一個官方說明,file-per-table的優點,同時也指出了ibdata文件的空間只能被重用,但沒法釋放給操做系統。mysql
You can reclaim operating system disk space when truncating or dropping a table. For tables created when file-per-table mode is turned off, truncating or dropping the tables creates free space internally in the ibdata files but the free space can only be used for new InnoDB data.
3. 對於file-per-table的表,回收空間,使用optimize table,實現原理以下面所示,其實就是重建+更名。sql
You can run OPTIMIZE TABLE to compact or recreate a tablespace. When you run an OPTIMIZE TABLE, InnoDB will create a new .ibd file with a temporary name, using only the space required to store actual data. When the optimization is complete, InnoDB removes the old .ibd file and replaces it with the new .ibd file. If the previous .ibd file had grown significantly but actual data only accounted for a portion of its size, running OPTIMIZE TABLE allows you to reclaim the unused space.
4. innodb的索引相關,5.5版mysql中測試索引的添加過程,看看mysql作了哪些事情。測試
CREATE TABLE `tindex` ( `id` int(11) DEFAULT NULL, `name` char(10) DEFAULT NULL, `address` varchar(20) NOT NULL, `c3` char(10) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC; --建立一個innodb的表 mysql> alter table tindex add index idx_name(name); --添加索引的name列 Query OK, 0 rows affected (0.04 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show profile for query 3; --查看profile +------------------------------+----------+ | Status | Duration | +------------------------------+----------+ | starting | 0.000087 | | checking permissions | 0.000004 | | checking permissions | 0.000004 | | init | 0.000007 | | Opening tables | 0.000022 | | System lock | 0.000008 | | setup | 0.000025 | | creating table | 0.013806 | --建立臨時表 | After create | 0.000003 | | manage keys | 0.019027 | | rename result table | 0.000236 | --完成索引建立工做和表同步後,rename結果表 | end | 0.000017 | | Waiting for query cache lock | 0.000002 | | end | 0.000004 | | query end | 0.000003 | | closing tables | 0.000006 | | freeing items | 0.000013 | | cleaning up | 0.000002 | +------------------------------+----------+ 18 rows in set (0.00 sec)
--可見就是重建了表,一樣alter table tindex engine innodb; 也就是會重建一下表(雖然表原本就是innodb的)ui
-- 還有optimize操做,實質就是doing recreate + analyze。this
For InnoDB tables prior to 5.7.4 and other table types, MySQL locks the table during the time OPTIMIZE TABLE is running. As of MySQL 5.7.4, OPTIMIZE TABLE is performed online for regular and partitioned InnoDB tables.
總之:5.6以前沒有online DDL,因此只要涉及到結構的改變都是重建!spa
有了online ddl後一些操做再也不須要重建表,參考http://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html#innodb-online-ddl-summary-grid操作系統