Why build a prefix index?

Analysis & Answer

There is an index optimization strategy related to index selectivity called prefix index, which is to use the prefix of the column instead of the entire column as the index key. When the length of the prefix is ​​appropriate, the selectivity of the prefix index can be close to that of the full column index, and because Shorter index keys reduce index file size and maintenance overhead.

Why use a prefix index?

The prefix index can effectively reduce the size of the index file, so that each index page can save more index values, thereby improving the speed of index query. But the prefix index also has its disadvantages, the prefix index cannot be triggered in order by or group by, nor can they be used for covering indexes.

Under what circumstances is it suitable to use a prefix index?

When the string itself may be relatively long, and the first few characters are different, it is suitable to use a prefix index; otherwise, it is not suitable to use a prefix index, for example, the length of the entire field is 20, and the index selectivity is 0.9, and we have The selectivity of the first 10 characters to build a prefix index is only 0.5, so we need to continue to increase the length of the prefix characters, but at this time the advantages of the prefix index are not obvious, so there is no need to create a prefix index.

Meow Interview Assistant: One-stop solution to interview questions, you can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Brush Questions] -> Interview Assistant  free questions. If you have good interview knowledge or skills, look forward to your sharing!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127391443