How to maintain the database index?

How to maintain the database index?

Answer: If the table is frequently updated and deleted, then it is mainly necessary to regularly maintain and check the index.

Analyze Table

When MySQL Optimizer optimizes SQL statements, it first needs to collect some relevant information, which includes the cardinality of the table (which can be translated as "hash degree"), which indicates how many different columns corresponding to an index Value-If cardinality is significantly less than the actual hash of the data, then the index is basically invalid.
We can use the SHOW INDEX statement to check the hash degree of the index:
SHOW INDEX FROM PLAYERS;
TABLE KEY_NAME COLUMN_NAME CARDINALITY
------- -------- ------------ ----------
PLAYERS PRIMARY PLAYERNO 14
Because the number of different PLAYERNOs in the PLAYER table is far more than 14, the index basically fails.
Here we use the Analyze Table statement to repair the index:
ANALYZE TABLE PLAYERS;
SHOW INDEX FROM PLAYERS; the
result is:
TABLE KEY_NAME COLUMN_NAME CARDINALITY
------- -------- --------- ------------
PLAYERS PRIMARY PLAYERNO 1000
The index has been repaired at this time, and the query efficiency has been greatly improved.
It should be noted that if binlog is enabled, then the results of the Analyze Table will also be written to the binlog. We can add the keyword local between analyze and table to cancel the write.


The disk on which Optimize Table frequently updates data needs to be defragmented, as is the database. The Optimize Table statement is valid for both MyISAM and InnoDB type tables.
If the table is updated frequently, you should run the Optimize Table statement periodically to ensure efficiency.

Check Table
databases can often encounter errors, such as errors when data is written to disk, or indexes are not updated synchronously, or the database is stopped without shutting down MySQL.
In these cases, the data may have errors:
Incorrect key file for table: ''. Try to repair it.
At this time, we can use the Check Table statement to check the table and its corresponding index.
For example, we run
CHECK TABLE PLAYERS; the
result is
TABLE OP MSG_TYPE MSG_TEXT
-------------- ----- -------- --------
TENNIS. PLAYERS check status OK
MySQL will save the time of the last check of the table. Each time you run check table, the information will be stored:
execute
SELECT TABLE_NAME, CHECK_TIME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'PLAYERS'
AND TABLE_SCHEMA = 'TENNIS'; the
result is
TABLE_NAME CHECK_TIME
---------- -------------------
PLAYERS 2006-08-21 16:44:25
Check Table can also specify other options:
UPGRADE: Used to test whether the table created in the earlier version of MySQL is compatible with the current version.
QUICK: The fastest option. When checking the data of each column, it will not check the correctness of the link. If you do not encounter any problems, you can use this option.
FAST: Only check if the table is normally closed. If you do not encounter serious problems after the system is powered off, you can use this option.
CHANGED: Only check the data updated after the last check time.
MEDIUM: The default option will check the correctness of the link between the index file and the data file.
EXTENDED: The slowest option will be fully checked.

 

 

Source: https://blog.51cto.com/holy2010/364657

Guess you like

Origin www.cnblogs.com/XiDaPuBen/p/12725907.html