Analysis of MySql index usage strategy

Analysis of MySql index usage strategy

MySql Index

Index advantages

1. You can ensure the uniqueness of the data by creating a unique index or primary key index.
2. Improve the performance of retrieved data
3. The connection conditions in table connection can speed up the direct connection between tables
4. Create indexes. Using indexes in queries can improve performance.

Index Disadvantages

1. Creating and maintaining indexes takes time, which increases with the amount of data.
2. Index files will take up physical space. In addition to the physical space required for the data table, each index will also take up a certain amount of physical space.
3. When inserting, updating, or deleting data in a table, the index must also be maintained dynamically, which will slow down the data maintenance process.

(Creating an index will take up disk space for the index file. Generally this problem is not too serious, but if you create multiple combined indexes on a large table, the index file will expand quickly).

Things to note when using indexes

1. Speed ​​up indexing on columns that are frequently searched
2. The uniqueness of the column can be ensured on the primary key column
3. Adding indexes to the join conditions between tables can speed up the join query.
4. Adding indexes to distinct columns that are often used for sorting (order by), grouping (group by), and so on can speed up sorting query time. (Order by alone cannot use indexes, so consider adding where or limit to the index.)
5. Create an index on the field after some where clauses < <= > >= BETWEEN IN and like in some cases (B-TREE)

6. If you create an index for the nickname field in the like statement, the index will not work when the query statement is nickname lick '%ABC%'. However, the index will work if the query statement is nickname lick 'ABC%'.

7. The index will not include NULL columns. If a column contains NULL values, it will not be included in the index. If a column in a composite index contains NULL values, the composite index will be invalid. Generally, a default value of 0 or ' ' string is required.

8. Use short indexes. If one of your fields is Char(32) or int(32), specify a prefix length when creating the index, such as the first 10 characters (assuming that most values ​​are unique). Then short indexes can increase query speed, save disk space, and reduce I/O operations.

9. Do not perform operations on columns, as this will invalidate the MySQL index and perform a full table scan

10. Choose the smaller the data type, the better, because usually the smaller the data type, the smaller the space occupied in the disk, memory, CPU, cache, and the faster the processing.

When not to create an index

1. Columns that are rarely used in queries should not be indexed. If indexes are created, however, MySQL performance will be reduced and space requirements will increase.
2. Columns with very little data should not be indexed. For example, a gender field with 0 or 1. In the query, the data in the result set accounts for a large proportion of the data rows in the table. MySQL needs to scan a large number of rows. Adding indexes will not improve efficiency.
3. Columns defined as text, image, and bit data types should not have indexes added.
4. When the modification (UPDATE, INSERT, DELETE) operations of the table are much greater than the retrieval (SELECT) operations, you should not create an index. These two operations are mutually exclusive.

The above is the detailed analysis of MySql index usage strategy. For more information about MySQL index, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL learning tutorial clustered index
  • Descending Index in MySQL 8.0
  • Index Skip Scan in MySQL 8.0
  • Summary of several situations in which MySQL indexes fail
  • Detailed explanation of MySQL clustered index and non-clustered index
  • Difference between MySQL btree index and hash index
  • MySQL functional index optimization solution
  • Summary of some common writing methods that cause MySQL index failure
  • Various types of MySQL indexes

<<:  Docker data volume common operation code examples

>>:  JavaScript to achieve a simple magnifying glass effect

Recommend

Tutorial diagram of installing zabbix2.4 under centos6.5

The fixed IP address of the centos-DVD1 version s...

MySQL 5.7.18 download and installation process detailed instructions

MySql Download 1. Open the official website and f...

Version numbers in css and js links in HTML (refresh cache)

background Search the keyword .htaccess cache in ...

How to deploy gitlab using Docker-compose

Docker-compose deploys gitlab 1. Install Docker I...

How to create a stored procedure in MySQL and add records in a loop

This article uses an example to describe how to c...

An article to help you learn CSS3 picture borders

Using the CSS3 border-image property, you can set...

Learn to deploy microservices with docker in ten minutes

Since its release in 2013, Docker has been widely...

Solve the problem of invalid utf8 settings in mysql5.6

After the green version of mysql5.6 is decompress...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...

Quickly solve the problem of slow and stuck opening of input[type=file]

Why is it that when the input tag type is file an...

4 ways to view processes in LINUX (summary)

A process is a program code that runs in the CPU ...

About the location of the H1 tag in XHTML

There has been a lot of discussion about H1 recent...

Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas

Overview When a 500 or 502 error occurs during ac...

Several ways to run Python programs in the Linux background

1. The first method is to use the unhup command d...