The problem and example of string indexing in MySQL

Source: Internet
Author: User

The cause of the event is the online log found on the MySQL slow query. 1 million data volume standards, the joint query all Go index case, it is more than 600 milliseconds. Puzzled, but the data was raised 30 times times after the index column was changed from varchar (50) to bigint type. The reason for this is that there are a lot of comparison operations to search on the index tree, and string comparisons are more time-consuming than integers.

Therefore, it is recommended that you do not index the string column in general, and if you do not want to use a string index, you can use the following two methods:

1. Only the leftmost n characters of a string are indexed and recommended n<=10; such as index left (address,8), but it is necessary to know that the prefix index cannot be used in an order by or on an index overlay.

2. Use the hash method on the string to convert the string to an integer, Address_key=hashtoint (address), Address_key indexed, query can be used as follows query where Address_key = Hashtoint (' Beijing,china ') and address = ' Beijing,china ';

Example

I found an issue in the following statement:

SELECT * from region where city like "Renqiu%" limit 10;
SELECT * from region where city like "Shangqiu%" limit 10;

The results of these two statements are the same,

A similar situation is as follows:

SELECT * from region where city like "Linan%" limit 10;
SELECT * from region where city like "Liu'an%" limit 10;

Linhai Longhai
Zaoyang Yiyang City
Wenzhou Wuzhou

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.