Introduction to MySQL REINDEX
MySQL REINDEX is also a data structure in a database table which is a significant part to have a good database maintenance method where it is responsible to reorganize and manage the indexes and restores the speedy retrieval. Since MySQL INDEX is also defined as a data structure that helps to optimize the database and progresses the speed of the MYSQL operations in a table. We use REINDEX in MySQL to rebuild indexes using one or multiple columns in a table database to have quick data access and make the performance better. MySQL REINDEX denotes the re-indexing process to rebuild the indexes on a database if the database is corrupted or needs repair to optimize the tables and fetch the rows using indexes properly using phpMyAdmin.
Syntax
Generally, we can apply MySQL REINDEX or INDEXES can be formed easily via phpMyAdmin in local server like WAMP, XAMPP or live server on cPanel. Most of the MySQL indexes procedure such as PRIMARY KEY, UNIQUE, INDEX, FULLTEXT& REINDEX are warehoused in B-trees.
B-tree can be defined as a self-balancing data structure tree that stores data in a sorted manner and allows sequential access, searches, insertions, and deletions on the basis of logarithmic time. It is beneficial for file systems and databases that are engaged to read and write huge blocks of information.
Except some like:
- For spatial data, types indexes are stored in R-trees
- MEMORY tables use hash indexes
- InnoDB supports reversed lists for FULLTEXT types of indexes.
We use the following simple syntax to add MySQL INDEX in a table:
CREATE INDEX [Index_Name] ON [TableName] ([ColumnName of the TableName]);
Now, for any reason you encounter the corruption of an index on the table, then you need to simply re-build that index or all of the indexes present on the table or columns by using the below basic syntax of REINDEX INDEX or even REINDEX TABLE and REINDEX DATABASE to repair particular database indexes.
REINDEX INDEX [Index_Name];
REINDEX TABLE [TableName]; or,
REINDEX TABLE [TableName]([ColumnName of the TableName]);
REINDEX DATABASE [DatabaseName];
The given arguments are explained as below:
- Index_Name is said to be the name of the index.
- TableName is the name of a particular table.
- ColumnName defines the column where the indexing is to be done in the table mentioned above.
- DatabaseName denotes the name of database name where we need to apply REINDEX in MySQL.
Here, REINDEX recreates the specified indexes for a single index, table index, and database index.
How MySQL REINDEX works?
In MySQL, REINDEX does the following works:
- It can help to relocate the indexing information very fast within a database or table.
- Here, re-indexing or simply indexing in MySQL will create an inner catalog which is stored by the MySQL service.
- It uses the catalog of table rows as it can indicate within a decimal of time using the least effort.
- A REINDEX TABLE redefines a database structure which rearranges the values in a specific manner of one or group of columns.
- It works first by sorting the data and then works to allot identification for every row in a table.
- The indexes are put together on the top of the tables and perform executions such as SELECT, DELETE, and UPDATE SQL statements quicker to deploy when the size of data is huge. The REINDEX or INDEX in MySQL can also be termed as a table which comprises of records arrangement technique.
- Indexes help to search for rows corresponding to a WHERE clause with particular column values very quickly so if the index is not working properly, then we must use the REINDEX command to operate and rebuild the indexes of table columns to continue the access of data.
- Eliminates rows from concern if there is the choice between more than one indexes by selective method indexing and thus supports the re-indexing also.
- Indexing and re-indexing also allow fetching rows from other tables when performing JOINS properly.
Examples to Implement MySQL REINDEX
For using MySQL REINDEX let us first create some indexes on tables and explain about them to know in brief the topic:
Initially, we have taken a table named Person in the database with fields Person_ID, Person_Name &Person_Address. It contains few records as follows:
Code:
SELECT * FROM Person
Output:
Examples #1
Now, the following query will find the person whose location is Delhi in the address column using WHERE:
Code #1
SELECT Person_ID, Person_Name FROM Person WHERE Person_Address= ‘Delhi’;
Output:
Code #2
If you want to view how MySQL performed this query internally, use EXPLAIN to the start of the previous query as follows:
EXPLAIN SELECT Person_ID, Person_Name FROM Person WHERE Person_Address = ‘Delhi’;
Output:
Here, the server has to test the whole table containing 7 rows to execute the query.
Example #2
Let us now add an index for address column by the below statement:
Code #1
CREATE INDEX Person_Address ON Person(Person_Address);
Output:
After running the query, again use EXPLAIN to view the result now.
Code #2
EXPLAIN SELECT Person_ID, Person_Name FROM Person WHERE Person_Address = ‘Delhi’;
Output:
The result is clear, now only 3 rows are scanned using the index created and not the whole table which is indicated in the key column.
If you want to see the index, use SHOW query as follows:
Code #3
SHOW INDEXES FROM Person;
Output:
Now, suppose that we have any corrupted index or if you no longer get the valid data due to any practical reasons but may not be possible in theory like software viruses or failures of hardware then, MySQL REINDEX will be responsible for a recovery process to repair the tables or database and rebuild the indexes to maintain the performance and optimization and quick operations within the databases.
Example #3
We can use REORGANISE, REPAIR, OPTIMIZE TABLE statements to rebuild the corrupted table and associated indexes to reduce the memory space and increase the I/O efficiency.
Code #1
REINDEX TABLE ‘Person’;
OR,
OPTIMIZE TABLE ‘Person’;
Output:
After MySQL REINDEX the indexes will be rebuilt and we can view that as output here:
Code #2
SHOW INDEXES FROM Person;
Output:
Conclusion
MYSQL REINDEX statement can drop all indexes on a group or recreates them from scratch, which might cause costly for groups that contain large data or a number of indexes. REINDEX is helpful when we need to recover from the corruption of an index in the database or when the description of an ordering structure is altered. Therefore, REINDEX supports to regain the performance and utility to faster access and smooth going operations in a MySQL database table.
Recommended Articles
This is a guide to MySQL REINDEX. Here we discuss an introduction to MySQL REINDEX with appropriate syntax and programming examples. You can also go through our other related articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses