I、 Parameters innodb_file_per_table
OFF | The data of the table is placed in the shared table space of the system , It's with the data dictionary |
ON | Every InnoDB Table data is stored in one with .ibd In the file with suffix ,V5.6.6- Default |
1、 Suggest ON, because , It's easier to manage a table as a single file , and drop table Command to delete this file directly . And if it's in a shared table space , Even if the table is deleted , Space is not recycled .
2、 Delete the entire table with drop table Command reclaim table space . But deleting some rows will encounter problems : The data in the table has been deleted , But the table space is not reclaimed .
II、 Why is the data in the table deleted , But the table space is not reclaimed
1、 Deleting a row is just marked for deletion , Actual disk space is not recycled , Reusable .
2、delete The result of deleting the data of the entire table by the command is , All data pages will be marked as reusable . But on the disk , Files don't get smaller .
These can be reused , And there is no space to be used , It looks like “ empty ”.
3、 Empty reason :
① Delete table record , The deleted record is only marked for deletion , The space where the index value is located can be reused , But there is no real deletion ; ② Add table record , If the index value is randomly scattered , Then it will cause the data page to split , It can also cause voids ;
③ Update values on Index , It's actually marking the old value as deleted , Then add a new value , Although old values can be reused , But it still creates a void .
III、 Rebuild table alter table A engine=InnoDB To remove voids , Shrink the space
MySQL 5.6 Previously, it was required in the whole DDL In the process , surface A There cannot be updates in . in other words , This DDL No Online Of . If in the process , There is new data to write to the table A Words , Will cause data loss .
Online DDL(V5.6-)
1、 Create a temporary file , Scan table A All data pages of the primary key ;
2、 Use the table on the data page A Record generation of B+ Trees , Store in a temporary file ;
3、 In the process of generating temporary files , Put all right A The operation is recorded in a log file (row log) in , It corresponds to state2 The state of ;
4、 After temporary file generation , Apply the actions in the log file to the temporary file , Get a logical data up with table A Same data file , That corresponds to state3 The state of ;
5、 Replace the table with a temporary file A Data files for .
Online DDL In fact, it will get MDL Write lock , And then degenerate into MDL Read the lock ;
but MDL Write lock holding time is relatively short , So it can be called Online;
and MDL Read the lock , Do not prevent data addition, deletion, query and modification , But it will prevent other threads from modifying the table structure ;
IV、Online and inplace
1、DDL If the process is Online Of , It must be inplace Of ;
2、 The reverse is not necessarily , in other words inplace Of DDL, Maybe not Online Of . By the end of MySQL 8.0, add
Add full text index (FULLTEXT index) And spatial index (SPATIAL index) This is the case .
V、optimize table、analyze table and alter table
1、 from MySQL 5.6 Version start ,alter table t engine = InnoDB( That is to say recreate) The default is the above figure 4 The process ;
2、analyze table t It's not a reconstruction table , Just re statistics the index information of the table , No data changes , In the process, I added MDL Read the lock ;
3、optimize table t be equal to recreate+analyze.
VI、 Be careful :
1、 If you want to shrink a table , It's just delete If you drop the unused data in the table , The size of the table file does not change , You have to pass alter table Command rebuild table , In order to achieve the purpose of table file smaller .
2、 Two implementations of rebuilding tables ,Online DDL We can consider using it in the low peak period of business , and MySQL 5.5 And previous versions , This command will block DML Of , Special care is required .