mysql query like fuzzy statement

A statement like

% Xxx%: Query xxx record field contains the username.

select * from user where username like '%xxx%'; 

% Xxx: Query username field ending in xxx records.

select * from user where username like '%xxx';

xxx%: Query username field starts with xxx records.

select * from user where username like 'xxx%';

Two statements like whether to use an index?

Use explain keyword analysis like the above query, use the index found that in some cases, in some cases full table queries.

like% xxx%: full table scan, the index is not used, inefficient.

 

 

 like% xxx: full table scan, the index is not used, inefficient.

 

 

 like xxx%: use the index username field.

Three Optimizations like?

There are some recommendations for optimizing online like% xxx%, such as locate, instr, position the way, but after a few pro-test found that the way is also a full table scan. If large amounts of data, it is recommended to use the search engine directly elasticsearch.

 

Guess you like

Origin www.cnblogs.com/mydesky2012/p/11608890.html