Home > Database > Mysql Tutorial > body text

mysql查询区分大小写高性能

WBOY
Release: 2016-06-07 14:55:23
Original
1155 people have browsed it

mysql查询默认是不区分大小写的 如: 1 2 3 4 5 6 7 a href=/tags.php/select/target=_blankselect/a*fromtable_namewherealike'a%' select*fromtable_namewherealike'A%' select*fromtable_namewherealike'a%' select*fromtable_namewherealike'A%' 效果

mysql查询默认是不区分大小写的 如:

1

2

3

4

5

6

7

select  * from  table_name where  a like  'a%'    

 

select  * from  table_name where  a like  'A%'    

 

select * from table_name where a like 'a%'

 

select * from table_name where a like 'A%'

效果是一样的。

要让mysql查询区分大小写,可以:
 

1

2

3

4

5

6

7

select  * from  table_name where  binary  a like  'a%'    

 

select  * from  table_name where  binary  a like  'A%'    

 

select * from table_name where binary a like 'a%'

 

select * from table_name where binary a like 'A%'

也可以在建表时,加以标识
 

1

2

3

4

5

6

7

8

9

10

11

create  table  table_name(   

 

     a varchar (20) binary     

 

)  

 

create table table_name(

 

     a varchar(20) binary 

 

)

测试30W数据

1

SELECT * FROM `tableName` WHERE ( BINARY weixin = 'value' ) LIMIT 1;

不支持索引,查询效率底下,不建议考虑。上面这些sql语句乍看不会有什么问题,但是当表中的数据多了以后,问题就会凸显出来,用不到索引,就会导致查询效率非常低下。

支持索引,查询效率高(推荐使用)

1

SELECT * FROM `tableName` WHERE weixin = 'value' COLLATE utf8_bin LIMIT 1;

查询分析如下:



通过以上描述可以很清晰的看到两个sql在效率上的根本区别,binary慎用

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!