[MySQL] describes how to optimize, analyze, check, and fix MySQL tables.

Source: Internet
Author: User

[Reproduced from: http://www.chinaz.com/program/2010/0414/111402.shtml]

The database management routine is to carry out preventive maintenance and repair the problematic content.

There are usually four main tasks to check and repair:

1. Optimize the table

2. Analyze the table (analyze and store the distribution of the keys in MyISAM and bdb tables)

3. Check the table (Check table errors and statistics of the update key for MyISAM)

4. Repair the table (repair the damaged MyISAM table)

I. Table Optimization

There are many ways to optimize a table: optimize TABLE statement, mysqlcheck tool (the server is running), or myisamchk (the server is not running or the table is not interactive)

Why optimization? As MySQL is used, tables that contain blob and varchar bytes will become redundant. Because these fields have different lengths, they occupy different sizes of space when inserting, updating, or deleting records, the record will become fragments and leave free space. Disks with fragments can reduce performance and need to be sorted out. Therefore, optimization is required.

1. Optimize the table using the optimize statement

# Mysql> optimize table name

In this way, the table name is optimized.

2. Use mysqlcheck to optimize the table

In addition to optimization, mysqlcheck can execute a large number of check and repair tasks.

# Mysqlcheck-O database name table name-uroot-p111111 (one table)

# Mysqlcheck-O database name table name 1 Table name 2-uroot-p111111 (multiple tables)

# Mysqlcheck-O database name-uroot-p111111 (for the entire database)

3. Use myisamchk to optimize the table

# Myisamchk -- quick -- check-only-changed -- Sort-index -- analyze table name

# Myisamchk-r table name (the-R parameter indicates that the table is repaired and the wasted space is also deleted)

# Myisamchk-r/usr/local/MySQL/data/testblog/Article (specify the path of the table)

The above operations must be performed when the server is closed or not interoperable with the server, you can use the myisamchk command line tool (if the server is running, use mysqladmin flush-tables to refresh the table before running this statement. Make sure that the server does not have table interoperability. Otherwise, a fault may occur ). Myisamchk is the oldest method. You must run myisamchk in the correct position or specify the path of the table.

Note: During the optimization process, the table is locked, so do not perform optimization operations when busy. Similarly, you must have enough space for optimize table. Without disk space, MySQL cannot be optimized and tables cannot be used.

Optimization is an important part of general management transactions for databases that contain MyISAM tables. It should be conducted on a regular basis.

Ii. Analysis of tables

Regular analysis of tables improves performance and should be part of routine maintenance. By updating the index information of a table, you can analyze the table to improve the database performance.

There are three ways to analyze a table:

1. When connecting to MySQL, use the analyze TABLE statement

2. Use the mysqlcheck command line tool (the server needs to run and only works for the MyISAM table)

3. Use the myisamchk command line tool (the server should not run, or there is no interoperability with the operated tables)

# Analyze table name;

# Mysqlcheck-A database name table name-uroot-p111111

# Mysqlcheck-A database name table name 1 Table name 2-uroot-p111111

If you try to analyze tables that do not support the analysis operation (such as InnoDB), the operation will not be performed.

# Myisamchk-A/usr/local/MySQL/data/database/table name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.