The application of MySQL query optimization index

  • 2020-05-17 06:48:14
  • OfStack

A bad SQL query can have a serious impact on the overall application's performance, not only consuming more database time, but also on other application components.

As in other disciplines, optimizing query performance depends largely on the developer's intuition. Fortunately, databases like MySQL come with a few assistance tools. This article briefly discusses three of the many tools: using indexes, analyzing queries using EXPLAIN, and adjusting the internal configuration of MySQL.

MySQL significantly speeds up queries by allowing database tables to be indexed so that records can be found quickly without having to scan the entire table at the beginning of 1. Each table can have up to 16 indexes, and MySQL also supports multi-column indexing and full-text retrieval.

Adding an index to a table is as simple as calling an CREATE INDEX command and specifying its domain for the index.
Listing A gives an example:


mysql> CREATE INDEX idx_username ON users(username); 

Here, the username field of the users table is indexed to ensure that the SELECT query that references this field in the WHERE or HAVING clauses runs faster than if the index was not added. You can see that the index has been created using the SHOW INDEX command (list B).

It's worth noting that indexes are like a double-edged sword. Indexing every 1 field of a table is usually not necessary and is likely to slow down because MySQL has to re-index each time it inserts or modifies data into a table for this extra work. On the other hand, it is also not a very good idea to avoid indexing every field of a table, because increasing the speed of inserting records results in slower query operations. For example, when designing an indexing system, it is wise to consider the main functions of the table (data repair and editing).


Related articles: