Description of OPTIMIZE in the manual: OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLEtbl_name [, tbl_name] if you have deleted a majority of tables, alternatively, if you have already described OPTIMIZE for a table that contains variable-length rows (including VARCHAR, BLOB, or OPTIMIZE in the manual:
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name]...
If you have deleted a majority of tables, or if you have made many changes to a table containing variable-length rows (tables containing VARCHAR, BLOB, or TEXT columns), you should use
Optimize table. The deleted records are kept in the link list. Subsequent INSERT operations will reuse the old record location. You can use optimize table again
Use unused space and organize data file fragments.
In most settings, you do not need to run optimize table. Even if you update a large number of rows with a variable length, you do not need to run them frequently, once a week or once a month.
You can only run on a specific table.
Optimize table only applies to MyISAM, BDB, and InnoDB tables.
Note: During the optimize table operation, MySQL locks the TABLE.
Instance description: optimize table is very important when optimizing MySQL.
I. raw data
1. data volume
When using mysql> select count (*) as total from ad_visit_history;
+ --------- +
| Total |
+ --------- +
| 1187096 | // more than 1.18 million data entries in total
+ --------- +
1 row in set (0.04 sec)
2. size of the table files stored in the hard disk
Zookeeper [root @ test1] # ls | grep visit | xargs-I du {}
382020 ad_visit_history.MYD // data files account for 380 M
127116 ad_visit_history.MYI // index files account for 127 MB
12 ad_visit_history.frm // The structure file occupies 12 K
3. check the index information.
Export indexes mysql> show index from ad_visit_history from test1; // You can view the index information of the table.
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
| Ad_visit_history | 0 | PRIMARY | 1 | id | A | 1187096 | NULL | BTREE |
| Ad_visit_history | 1 | ad_code | 1 | ad_code | A | 46 | NULL | YES | BTREE |
| Ad_visit_history | 1 | unique_id | 1 | unique_id | A | 1187096 | NULL | YES | BTREE |
| Ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 46 | NULL | YES | BTREE |
| Ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 30438 | NULL | YES | BTREE |
| Ad_visit_history | 1 | ip_ind | 1 | ip | A | 593548 | NULL | YES | BTREE |
| Ad_visit_history | 1 | port_ind | 1 | port | A | 65949 | NULL | YES | BTREE |
| Ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 1187096 | NULL | YES | BTREE |
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
8 rows in set (0.28 sec)
Description of the columns in the index information.
Table: The name of the Table.
Non_unique: 0 if the index cannot contain duplicate words. If yes, it is 1.
Key_name: name of the index.
Seq_in_index: The column serial number in the index, starting from 1.
Column_name: column name.
Collation: how the column is stored in the index. In MySQLSHOW INDEX syntax, values 'A' (ascending) or NULL (unclassified) are available ).
Cardinality: the number of unique values in the index. You can update analyze table or myisamchk-a by running analyze table. The base number is counted based on the statistical data stored as an integer. Therefore, this value is not required to be accurate even for small tables. The larger the base, the larger the chance for MySQL to use the index for Union.
Sub_part: If a column is partially indexed, it is the number of indexed characters. If the entire column is indexed, the value is NULL.
Packed: indicates how the keywords are compressed. If it is not compressed, it is NULL.
Null: if the column contains NULL, YES is included. If no, it is null.
Index_type: method for storing index data structures (BTREE, FULLTEXT, HASH, RTREE)
II. delete half of the data
Restore half of mysql> delete from ad_visit_history where id> 598000; // delete half of data
Query OK, 589096 rows affected (4 min 28.06 sec)
[Root @ www.linuxidc.com test1] # ls | grep visit | xargs-I du {}// the corresponding MYD and MYI file size remain unchanged
382020 ad_visit_history.MYD
127116 ad_visit_history.MYI
12 ad_visit_history.frm
In general, if half of the data is deleted in the database, the corresponding. MYD and. MYI files should also be half of the previous one. However, after half of the data is deleted,. MYD. MYI has not been reduced by 1 KB, which is terrible.
Let's take a look at the index information.
Zookeeper mysql> show index from ad_visit_history;
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
| Ad_visit_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | BTREE |
| Ad_visit_history | 1 | ad_code | 1 | ad_code | A | 23 | NULL | YES | BTREE |
| Ad_visit_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | YES | BTREE |
| Ad_visit_history | 1 | ad_code_ind | 1 | ad_code | A | 23 | NULL | YES | BTREE |
| Ad_visit_history | 1 | from_page_url_ind | 1 | from_page_url | A | 15333 | NULL | YES | BTREE |
| Ad_visit_history | 1 | ip_ind | 1 | ip | A | 299000 | NULL | YES | BTREE |
| Ad_visit_history | 1 | port_ind | 1 | port | A | 33222 | NULL | YES | BTREE |
| Ad_visit_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | YES | BTREE |
+ ------------------ + ------------ + ------------------- + -------------- + --------------- + ----------- + ------------- + ---------- + -------- + -------------- + ----------- +
8 rows in set (0.00 sec)
In comparison, the data in this index query and the last index query is basically the last one, which is still reasonable.