Unit 3
Unit 3
The ANALYZE TABLE command in MySQL is used to analyze and update the key distribution
statistics for a table. These statistics help the MySQL query optimizer to make better decisions about
how to execute queries, particularly for selecting the best indexes.
Purpose of ANALYZE TABLE:
1. Update Statistics: It updates the index statistics of the table, which can improve the
performance of queries by ensuring that the optimizer has current information about data
distribution.
2. Query Optimization: Accurate statistics help the query optimizer to estimate the number of
rows that will be returned by queries and choose the most efficient execution plan.
Syntax:
sql
Copy code
ANALYZE TABLE table_name [, table_name2, ...];
• table_name: The name of the table you want to analyze.
• You can specify multiple tables in a single command, separated by commas.
Usage Example:
sql
Copy code
ANALYZE TABLE employees;
This command will analyze the employees table and update its index statistics.
What Happens When You Run ANALYZE TABLE:
• InnoDB Tables:
o Updates statistics about the distribution of values in the index columns.
o Collects information about index cardinality, which is used by the optimizer to estimate
the number of rows that will match a given query.
• MyISAM Tables:
o Rebuilds the index and updates index statistics.
o Fixes any inconsistencies in the index data.
Additional Considerations:
• Locking: The ANALYZE TABLE command locks the table while it performs the analysis,
which may impact performance if the table is large or heavily used.
• Frequency: It’s generally not necessary to run ANALYZE TABLE frequently. However, you
might want to run it after significant data modifications, such as large inserts, updates, or
deletions, to ensure that the optimizer has accurate statistics.
Example with Multiple Tables:
sql
Copy code
ANALYZE TABLE employees, departments, salaries;
This command will analyze the employees, departments, and salaries tables in one go.
Checking Table Status:
You can check the status of a table after running ANALYZE TABLE using:
sql
Copy code
SHOW TABLE STATUS LIKE 'employees';
This command will show you various details about the table, including the Data_free (unused space)
and Update_time.
Automated Statistics Updates:
• InnoDB automatically updates statistics periodically. For MyISAM tables, manual updates with
ANALYZE TABLE are often required.
Summary:
The ANALYZE TABLE command is a useful tool for maintaining accurate index statistics, which can
help improve query performance by providing the optimizer with up-to-date information. While it’s not
something that needs to be run daily, it’s a good practice to use it after making significant changes to
the data.
QUICK Applies to InnoDB & Do not scan the rows for incorrect links.
MyISAM
FAST Applies to MyISAM. Check only tables that have not been properly closed.
Ignored for InnoDB.
CHANGED Applies to MyISAM. Check tables that have either been modified since the last check or have
Ignored for InnoDB. not been properly closed.
MEDIUM Applies to MyISAM. Scan rows to verify the validity of deleted links. This process also
Ignored for InnoDB. calculates a key checksum for the rows and checks it against the
calculated checksum for the keys.
EXTENDED Applies to MyISAM. Perform a complete key lookup for all keys associated with each row.
Ignored for InnoDB. This guarantees 100% consistency in the table but is a time-consuming
process.
The CHECK TABLE statement also checks views for problems. For example, it can check if a view
references tables that do not exist.
The CHECK TABLE returns a result set that includes four columns:
Column Value
Op Always check
The CHECK TABLE statement might generate multiple rows of information for each checked table or
view. The last row should have a Msg_type value of status and the Msg_text should typically be OK
MySQL CHECK TABLE statement examples
Let’s take some examples of using the CHECK TABLE statement.
1) Using the CHECK TABLE to check for errors in tables
First, log in to the MySQL database server using the root account:
mysql -u root -pCode language: SQL (Structured Query Language) (sql)
It’ll prompt you to enter the password. Enter the valid password to log in:
Enter password: ********Code language: SQL (Structured Query Language) (sql)
Second, switch the current database to classicmodels sample database:
use classicmodels;Code language: SQL (Structured Query Language) (sql)
Third, check the tables customers and products for errors:
CHECK TABLE customers, products;Code language: SQL (Structured Query Language) (sql)
Output:
+-------------------------+-------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+-------------------------+-------+----------+----------+
| classicmodels.customers | check | status | OK |
| classicmodels.products | check | status | OK |
+-------------------------+-------+----------+----------+
2 rows in set (0.03 sec)Code language: SQL (Structured Query Language) (sql)
The output shows that both tables have no errors.
2) Using the MySQL CHECK TABLE to check for errors in a view
First, create a database called test:
CREATE DATABASE IF NOT EXISTS test;Code language: SQL (Structured Query Language) (sql)
Second, switch to the test database:
USE test;Code language: PHP (php)
Third, create a table called employees in the test database:
CREATE TABLE employees(
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
phone VARCHAR(255) NOT NULL
);Code language: SQL (Structured Query Language) (sql)
Fourth, create a view called contacts based on the employees table:
CREATE VIEW contacts AS
SELECT
concat_ws(' ', first_name, last_name) as name,
email,
phone
FROM
employees;Code language: SQL (Structured Query Language) (sql)
Fifth, drop the table employees:
DROP TABLE employees;Code language: SQL (Structured Query Language) (sql)
Finally, check the view contacts for the error:
CHECK TABLE contacts\G;Code language: SQL (Structured Query Language) (sql)
Output:
*************************** 1. row ***************************
Table: test.contacts
Op: check
Msg_type: Error
Msg_text: View 'test.contacts' references invalid table(s) or column(s) or function(s) or definer/invoker
of view lack rights to use them
*************************** 2. row ***************************
Table: test.contacts
Op: check
Msg_type: error Msg_text: Corrupt
2 rows in set (0.00 sec)
ERROR:
No query specifiedCode language: SQL (Structured Query Language) (sql)
Note that the \G instructs mysql to display the result in a more readable, vertical format rather than a
traditional horizontal, table-based layout.
The first row indicates that the view references an invalid table and the second row specifies that the
view is corrupt.
Summary
• Use the MySQL CHECK TABLE statement to check one or more tables or views for errors.
Introduction to MySQL REPAIR TABLE statement
During operation, your tables may be corrupted due to various reasons, such as hardware failures,
unexpected shutdowns, or software bugs.
To repair the possibly corrupted tables, you use the REPAIR TABLE statement. The REPAIR
TABLE statement can repair only tables that use MyISAM, ARCHIVE, or CSV storage engines.
Here’s the syntax of the REPAIR TABLE statement:
REPAIR TABLE table_name [, table_name] ...
[QUICK] [EXTENDED] [USE_FRM];Code language: SQL (Structured Query Language) (sql)
In this syntax:
• table_name: The name of the table you want to repair. The statement allows you to repair
multiple tables at once.
• QUICK: This is the default option that allows you to perform a quick repair. It is suitable for
most cases.
• EXTENDED: This option allows you to perform an extended repair. It may take longer however
can fix more complex issues.
• USE_FRM: This option re-creates the .frm file. It’ll help when the table definition file is
corrupted.
MySQL REPAIR TABLE statement examples
Let’s take some examples of using the REPAIR TABLE statement.
The following command performs a quick repair on the sample_table table:
REPAIR TABLE sample_table;Code language: SQL (Structured Query Language) (sql)
It’s equivalent to the following statement that uses the QUICK option explicitly:
REPAIR TABLE sample_table QUICK;Code language: SQL (Structured Query Language) (sql)
The following command performs an extended repair on the sample_table table:
REPAIR TABLE sample_table EXTENDED;Code language: SQL (Structured Query Language) (sql)
When you find that the table definition file (.frm) is suspected to be corrupted, you can use
the USE_FRM option to recreate it:
REPAIR TABLE sample_table USE_FRM;Code language: SQL (Structured Query Language) (sql)
Important notes on using the REPAIR TABLE statement
When using the REPAIR TABLE statement, you should consider the following important notes:
Making a backup before repairing tables
It’s important to make a backup of a table before you repair it. In some cases, the REPAIR
TABLE statement may cause data loss.
Table lock
The REPAIR TABLE statement requires a table lock during the repair process. If you issue queries to
the table, they will blocked until the repair is complete.
Storage Engines
The REPAIR TABLE statement only works with MyISAM, CSV, and ARCHIVE tables. It doesn’t
support tables of other storage engines.
Replication
If they run the REPAIR TABLE for the original tables, the fixes will not propagate to replicas.
Summary
• Use the REPAIR TABLE statement to repair possibly corrupted tables.
Introduction to MySQL OPTIMIZE TABLE statement
The OPTIMIZE TABLE statement allows you to reorganize the physical storage of table data to reclaim
unused storage space and improve performance when accessing the table.
In practice, you’ll find the OPTIMIZE TABLE statement useful in the following cases:
• Frequent deletions/updates: If a table has frequent updates or deletions, its data may be
fragmented. The OPTIMIZE TABLE statement can help rearrange the storage structure and
eliminate wasted space.
• Table with variable-length rows: Tables with variable-length data such as VARCHAR, TEXT,
and BLOB may become fragmented over time. By using the OPTIMIZE TABLE statement,
you can reduce the storage overhead.
• Significant data growth and shrinkage: If your database experiences significant growth &
shrinkage, you can run the OPTIMIZE TABLE periodically to maintain optimal storage
efficiency.
Overall, using the OPTIMIZE TABLE statement helps you optimize the storage space of table data and
improve the query performance.
The OPTIMIZE TABLE statement works with InnoDB, MyISAM, and ARCHIVE tables.
MySQL OPTIMIZE TABLE statement examples
Let’s take some examples of using the MySQL OPTIMIZE TABLE statement.
1) Using the MySQL OPTIMIZE TABLE statement for MyISAM tables
For MyISAM tables, the OPTIMIZE TABLE statement works as follows:
• Repair the table if it has deleted or split rows.
• Sort the index pages if they are not sorted.
• Update the table statistics if they are not up to date.
The following example illustrates the steps for optimizing a MyISAM table:
First, check the table status using the show table status statement:
SHOW TABLE STATUS LIKE '<table_name>'\GCode language: SQL (Structured Query Language)
(sql)
It’ll return the output like this:
*************************** 1. row ***************************
Name: <table_name>
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 5000
Avg_row_length: 44
Data_length: 440000
Max_data_length: 281474976710655
Index_length: 105472
Data_free: 220000
Auto_increment: 10001
Create_time: 2023-11-21 07:34:39
Update_time: 2023-11-21 07:38:43
Check_time: NULL
Collation: utf8mb4_0900_ai_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)Code language: SQL (Structured Query Language) (sql)
There are two important columns in the output regarding optimizing the table:
• The Data_length represents the space used by all rows in the table including any overhead as
row headers.
• The Data_free is the amount of free space (in bytes) in the data file. It indicates how much
space can potentially be reclaimed by the OPTIMIZE TABLE statement.
Second, optimize the table using the OPTIMIZE TABLE statement:
OPTIMIZE TABLE <table_name>;Code language: SQL (Structured Query Language) (sql)
Output:
+----------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------+----------+----------+----------+
| test.text_data | optimize | status | OK |
+----------------+----------+----------+----------+
1 row in set (0.05 sec)Code language: SQL (Structured Query Language) (sql)
The Msg_text is OK which indicates the optimization is successful.
Third, check the status of the table again:
SHOW TABLE STATUS LIKE '<table_name>'\GCode language: SQL (Structured Query Language)
(sql)
If the table space is fragmented, you’ll see Data_free is zero:
*************************** 1. row ***************************
Name: <table_name>
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 5000
Avg_row_length: 44
Data_length: 440000
Max_data_length: 281474976710655
Index_length: 105472
Data_free: 0
Auto_increment: 10001
Create_time: 2023-11-21 08:03:12
Update_time: 2023-11-21 08:03:41
Check_time: NULL
Collation: utf8mb4_0900_ai_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)Code language: SQL (Structured Query Language) (sql)
2) Using the MySQL OPTIMIZE TABLE statement for InnoDB tables
When you run the OPTIMIZE TABLE statement on InnoDB tables, you’ll get the following output:
+--------+----------+----------+-------------------------------------------------------------------+
| Table | Op | Msg_type | Msg_text |
+--------+----------+----------+-------------------------------------------------------------------+
| test.t | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| test.t | optimize | status | OK |
+--------+----------+----------+-------------------------------------------------------------------+
2 rows in set (0.05 sec)Code language: SQL (Structured Query Language) (sql)
The first message:
Table does not support optimize, doing recreate + analyze insteadCode language: SQL (Structured
Query Language) (sql)
It means that the OPTIMIZE TABLE does not optimize the InnoDB tables in the same way it optimizes
the MyISAM tables. Instead, the OPTIMIZE TABLE statement performs the following actions:
• First, create a new empty table.
• Second, copy all rows from the original table into the new table.
• Third, delete the original table and rename the new tables.
• Finally, run the ANALYZE statement to gather table statistics.
One caution is that you should avoid running the OPTIMIZE TABLE statement on a
large InnoDB table when the disk space is low, as it is likely to cause the server to run out of space
while attempting to recreate the large table.
Summary
• Use the OPTIMIZE TABLE statement to reorganize the physical storage of tables to reduce
disk space usage and improve query execution time.