A few things worth noting about MySQL indexes

In the database, the ones that have the greatest impact on performance include the database's lock strategy, cache strategy, index strategy, storage strategy, and execution plan optimization strategy.

The indexing strategy determines the efficiency of the database in quickly locating data, and the storage strategy determines the efficiency of data persistence.

1. Indexes do not store null values.

To be more precise, single-column indexes do not store null values, and composite indexes do not store all null values. The index cannot store Null, so when applying the is null condition to this column, because the index is basically

Without Null value, the index cannot be used and only the whole table can be scanned.

Why can't index columns store Null values?

Building index column values ​​inevitably involves many comparison operations. The particularity of Null values ​​is that most of the operations involved are null. In this case, the null value cannot actually be

Participate in the indexing process. In other words, null values ​​will not appear on the leaf nodes of the index tree like other values.

2. Not suitable for columns with fewer key values ​​(columns with more repeated data).

If the index column TYPE has 5 key values ​​and there are 10,000 pieces of data, then WHERE TYPE = 1 will access 2,000 data blocks in the table.

In addition to accessing index blocks, a total of more than 200 data blocks need to be accessed.

If the full table scan is performed, assuming 10 pieces of data per data block, then only 1,000 data blocks need to be accessed. Since the data blocks accessed by the full table scan

Anything less, and the index will definitely not be used.

3. Leading fuzzy queries cannot use indexes (like '%XX' or like '%XX%')

If there is such a column of codes with values ​​of 'AAA', 'AAB', 'BAA', 'BAB', if the where code is like '%AB' condition, because the preceding is

It's fuzzy, so you can't use the order of the index. You have to look for it one by one to see if the conditions are met. This will result in a full index scan or full table scan

trace. If this is the condition where code like 'A % ', you can find the position of the CODE starting with A in the CODE. When you encounter the CODE starting with B

When the data is found, you can stop searching because the subsequent data must not meet the requirements. This way you can take advantage of the index.

4.MySQL mainly provides two types of indexes: B-Tree index and Hash index.

The B-tree index has the capabilities of range search and prefix search. For a B-tree with N nodes, the complexity of retrieving a record is O(LogN). Equivalent to binary search.

Hash indexes can only perform equal searches, but no matter how big the Hash table is, the search complexity is O(1).

Obviously, if the differences in values ​​are large and equal value searches (=, <, >, in) are the main focus, Hash index is a more efficient choice, and it has a search complexity of O(1).

If the difference in values ​​is relatively poor and range search is the main focus, B-tree is a better choice because it supports range search.

The two main storage engines in MySQL, MyISAM and InnoDB, use different indexing and storage strategies. This article will analyze their similarities, differences and performance.

Guess you like

Origin blog.csdn.net/o67f2wpkvdf3bpe8/article/details/131137946