Mysql prefix Index

Source: Internet
Author: User

Mysql prefix index Application Scenario: There is an address field in the database, the type is varchar (100), the business determines the need to often based on
Address. Determine the Selectivity of www.2cto.com: SQL code SELECT count (DISTINCT (address)/count (*) AS selecti.pdf FROM info; + ------------- + | selecti.pdf | + ------------- + | 0.8745 | + ------------- + <address> the selection is good, but the length is 100. It is obviously inappropriate to create an index for the entire field. You can consider creating an index.
Prefix index, for example, <left (address, 5)> to check its selectivity: www.2cto.com SQL code SELECT count (DISTINCT (left (address, 5)/count (*) AS selecti.pdf FROM info; + ------------- + | selecti.pdf | + --------------- + | 0.5981 | + ------------- + the Selectivity is good, but it is too low compared with 0.8745, so we can increase the prefix length to 10.
Optional: www.2cto.com SQL code SELECT count (DISTINCT (left (address, 10)/count (*) AS selecti.pdf FROM info; + ------------- + | selecti.pdf | + ------------- + | 0.8239 | + ------------- + and 0.8745 are very close, but the index length is only 10, so you can decide to create a prefix index. Prefix indexes take into account both the index size and query speed, but the disadvantage is that they cannot be used for order by and GROUP
And cannot be used for Covering index (that is, when the index itself contains all the data required for the query
Data File itself ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.