欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql索引

程序员文章站 2022-05-23 18:05:50
索引的命名:用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引)(联合索引) 一:创建索引首先创建一个表:create table t1 (id int primary key,username varchar(20),password varchar(20)); 创建单个索引的语法:cre ......

索引的命名:用户可以在多个列上建立索引,这种索引叫做复合索引(组合索引)(联合索引)

一:创建索引
首先创建一个表:create table t1 (id int primary key,username varchar(20),password varchar(20));

创建单个索引的语法:create index 索引名 on 表名(字段名)

索引名一般是:表名_字段名

给id创建索引:create index t1_id on t1(id);

创建联合索引的语法:create index 索引名 on 表名(字段名1,字段名2)

给username和password创建联合索引:create index t1_username_password on t1(username,password)

二:索引失效
1、对单字段建了索引,where条件多字段。
select * from template t where t.logicdb_id = 4 and t.sync_status = 1

2、建立联合索引,where条件单字段。与上面情况正好相反。
select * from template t where t.sync_status = 4

3、对索引列运算,运算包括(+、-、*、/、!、<>、%、like'%_'(%放在前面)(两边都加%不失效)、or、in、exist等),导致索引失效

4、类型错误,如字段类型为varchar,where条件用number。
错误写法:select * from template t where t.template_id = 1

正确写法:select * from template t where t.template_id = '1'

5、对索引应用内部函数,这种情况下应该建立基于函数的索引。
select * from template t where ROUND(t.logicdb_id) = 1

6、查询表的效率要比应用索引查询快的时候。

7、is null 索引失效;is not null Betree索引生效。导致的原因,个人认为应该是,mysql没有在null写进索引。还要看应用的数据库而定。


三:查询索引
show index from t

四:删除索引
drop index idx_col1_col2 on t1