Influence in MySQL like to use for the index

Sql statement from the self-built data tables to start the test:

-- 创建表
CREATE TABLE test(
id INT(11) NOT NULL AUTO_INCREMENT,
uname VARCHAR(255),
PRIMARY KEY(id) 
);

And then create an index on uname fields:

- Add index 
the ALTER  TABLE the Test the ADD  INDEX uname_index (uname); 

- view the index 
SHOW INDEX  the FROM the Test; 
! [] (HTTPS: // img2018.cnblogs.com / Blog / 905 539 / 201 810 / 905 539 - 20181010143352460 - 197 551 940 .png ) 

- Add a record 
the INSERT  the INTO Test the VALUES (( . 1 , ' Jay ' ), ( 2 , ' JA' ), ( 3 , ' pril ' ), ( 4 , ' aybar ' );

Test results:

filtered: It refers to the results returned row per row (value row rows) needs to read percentage.

EXPLAIN SELECT * FROM test WHERE uname LIKE 'j';

EXPLAIN SELECT * FROM test WHERE uname LIKE 'j%';

EXPLAIN SELECT * FROM test WHERE uname LIKE '%j';

EXPLAIN SELECT * FROM test WHERE uname LIKE '%j%';

So it seems% of the use of the index does not seem impressed, but not coupled with a general index of the field

- adding a non-indexed fields 
the ALTER  TABLE `test`
 the ADD  the COLUMN ` status`   int ( . 11 ) UNSIGNED NULL  the DEFAULT  . 1 an AFTER `uname`    

Again detected:
EXPLAIN the SELECT * Test the WHERE the uname the LIKE the FROM 'J';

EXPLAIN SELECT * FROM test WHERE uname LIKE 'j%';

EXPLAIN SELECT * FROM test WHERE uname LIKE '%j';

EXPLAIN SELECT * FROM test WHERE uname LIKE '%j%';

The result: a direct match with the number of inquiries% without the use of the index after the like; like to begin after matching% value will be used to index.

----doubt? ? ?
| So why add a field after there such a difference? Before field is id, uname id is the primary key, so that a portion of two fields is taken select * index fields are indexed. So like statement will use the index.
Behind the increase of a non-indexed field status, the data list and takes there the status field, the situation% before matching value would be affected.
Of course, if not select *, but select id, or select the uname again or select id, uname will use the index.

Specific also want to try it yourself ha. Hear is better to see; to see themselves not as hands-out.

Guess you like

Origin www.cnblogs.com/shengguorui/p/11318933.html