使用通配符(%%)提高MySQL搜索性能?
不,只要您使用通配符,MySQL都不会提高搜索性能,因为MySQL将无法使用索引。如果您将其更改为“anyLetter%”,则可以使用索引
以下语法更适合与尾随通配符一起使用。语法如下-
SELECT *FROM yourTableName WHERE yoorColumnName LIKE ‘anyLetter%’;
创建表的查询如下-
mysql> create table TrailingWildCardDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name Varchar(20), -> PRIMARY KEY(Id) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into TrailingWildCardDemo(Name) values('John'); mysql> insert into TrailingWildCardDemo(Name) values('Johnson'); mysql> insert into TrailingWildCardDemo(Name) values('Mike'); mysql> insert into TrailingWildCardDemo(Name) values('Kevin'); mysql> insert into TrailingWildCardDemo(Name) values('Adam'); mysql> insert into TrailingWildCardDemo(Name) values('Ethan'); mysql> insert into TrailingWildCardDemo(Name) values('James');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from TrailingWildCardDemo;
以下是输出-
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Johnson | | 3 | Mike | | 4 | Kevin | | 5 | Adam | | 6 | Ethan | | 7 | James | +----+---------+ 7 rows in set (0.00 sec)
这是尾随通配符的查询-
mysql> select *from TrailingWildCardDemo where Name LIKE 'j%';
以下是输出-
+----+---------+ | Id | Name | +----+---------+ | 1 | John | | 2 | Johnson | | 7 | James | +----+---------+ 3 rows in set (0.00 sec)