In the optimization query, database applications (such as MySQL) mean the operation and use of the tool. Using indexes, using explain to analyze and query, and adjusting the internal configuration of MySQL can optimize the query.
Any databaseProgramIn the high-traffic database driver, a bad SQL query statement can seriously affect the running of the entire application, it not only consumes more database time, but also affects other application components.
Like other disciplines, optimizing query performance is largely dependent on developers' intuition. Fortunately, databases like MySQL come with some assistance tools. This article briefly discusses the three tools: Using indexes, analyzing queries using explain, and adjusting the internal configuration of MySQL.
1. Use Indexes
MySQL allows you to index database tables so that you can quickly search for records without scanning the entire table at the beginning, which significantly speeds up query. Each table can have up to 16 indexes. In addition, MySQL also supports multiple column indexes and full-text searches.
It is very easy to add an index to a table. You only need to call the create Index Command and specify its fields for the index. List A provides an example:
Mysql> Create index idx_username on users (username );
Query OK, 1 row affected (0.15 Sec)
Records: 1 duplicates: 0 Warnings: 0
List
Here, the username field of the users table is indexed to ensure that the SELECT query statements that reference this field in the where or having clause run faster than those that do not have an index added. You can run the show Index Command to check whether the index has been created (List B ).
List B
It is worth noting that indexes are like a double-edged sword. Indexing each field of a table is usually unnecessary and may slow down the operation because MySQL has to re-create indexes for these additional tasks every time it inserts or modifies data in the table. On the other hand, it is not a good idea to avoid indexing each field of a table, because the query operation slows down when the record insertion speed is increased. This requires a balance. For example, when designing an index system, it is wise to consider the table's main functions (data repair and editing.