MySQL index used: When the field is varchar type, the condition is to be wrapped up with ' '

Source: Internet
Author: User
Tags mysql index


Conclusion:

When MySQL is in an int type, the search condition where num= ' 111 ' and where num=111 can use the index of the field.
When MySQL field is a varchar type, the search condition where num= ' 111 ' can use the index, where num=111 can not use the index

Verification process:

To build a table statement:

CREATE TABLE ' Gyl ' (  ' id ' int (one) not null auto_increment,  ' str ' varchar (255) is not NULL,  ' num ' int (one) ' NOT NULL Default ' 0 ',  ' obj ' varchar (255) Default NULL,  PRIMARY key (' id '),  key ' str_x ' (' str '),  key ' num_x ' (' num ' ') Engine=innodb  DEFAULT Charset=utf8;

Inserting data into a table using a self-replicating statement

Insert INTO Gyl (' str ', ' num ') VALUES (123123, ' 12313 ');

Insert INTO Gyl (' str ', ' num ') select ' str ', ' num ' from Gyl;

Change data Update Gyl set Num=id,str=id

Results:

Mysql> Explain select * from Gyl where str=123123 limit 1;+----+-------------+-------+------+---------------+------+ ---------+------+--------+-------------+| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows |  Extra |+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+| 1 | Simple | Gyl | All | str_x | NULL | NULL | NULL | 262756 | Using where |+----+-------------+-------+------+---------------+------+---------+------+--------+-------------+1 Row in setmysql> explain select * from Gyl where str= ' 123123 ' limit 1;+----+-------------+-------+------+------------- --+-------+---------+-------+--------+-------------+| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows |  Extra |+----+-------------+-------+------+---------------+-------+---------+-------+--------+-------------+| 1 | Simple | Gyl | Ref | str_x | str_x | 257 | ConsT | 131378 | Using where |+----+-------------+-------+------+---------------+-------+---------+-------+--------+------------- +1 row in setmysql> explain select * from Gyl where num= ' 12313 ' limit 1; +----+-------------+-------+------+---------------+-------+---------+-------+--------+-------+| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows |  Extra |+----+-------------+-------+------+---------------+-------+---------+-------+--------+-------+| 1 | Simple | Gyl | Ref | num_x | num_x | 4 | Const |       131378 | |+----+-------------+-------+------+---------------+-------+---------+-------+--------+-------+1 row in set1065- Query was emptymysql> explain select * from Gyl where num=12313 limit 1;+----+-------------+-------+------+----------- ----+-------+---------+-------+--------+-------+| ID | Select_type | Table | Type | Possible_keys | Key | Key_len | Ref | Rows | Extra |+----+-------------+-------+------+---------------+-------+---------+-------+--------+-------+| 1 | Simple | Gyl | Ref | num_x | num_x | 4 | Const |       131378 | |+----+-------------+-------+------+---------------+-------+---------+-------+--------+-------+1 row in Set

  

MySQL index used: When the field is varchar type, the condition is to be wrapped up with ' '

Related Article

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.