一,索引

   1>> 索引的定义:索引是一个单独的,存储在磁盘上的数据库结构,它们包含着对数据表里所有记录的引用指针。通过索引能快速找出某个或多个列中有一特定值的行。

   2>>使用索引的优缺点

      优点:1,通过创建唯一索引,可以保证数据库表中每一行的数据的唯一性。

                 2,可以大大加快查询速度。

                 3,在实现数据的参考完整性方面,可以加速表与表之间的连接。

                 4,在使用分组和排序子句进行数据查询时,也可以显著减少查询中分组和排序的时间

      缺点:1,创建索引和维护索引需要时间,并随着数据量的增加,耗时也会增加。

                 2,索引需要占用磁盘空间,除数据表占用空间外,索引也要占用一定的物理空间,如果有大量的索引,索引文件可能比数据文件更快达到最大的文件尺寸。

                 3,当对表中的数据增加和删除及修改的时候,索引也要动态维护,这样就降低了数据维护速度

    3>> 索引设计原则

         1> 索引不是越多越好,大量的索引会影响新增,修改,删除的性能

         2> 避免对经常更新的的表进行过多的索引,并且索引的列尽可能少,对于经常查询的表应该创建索引,但要避免添加不必要的字段

         3>数据量小的表最好不要使用索引,量小使用索引不但不节省时间,反而会增加优化难度

         4> 在条件表达式中经常用到的不同值较多的列上创建索引,较少就不要创建索引。

         5> 当唯一性是某种数据的特征时,指定唯一索引,提高查询速度。

         6> 在频繁进行排序和分组的列上建立索引,如果待排序的列有多个,可以建立组合索引。

  4>>  创建表时创建索引                                                       

         1> 创建普通索引  /单列索引                               

                create table test1(id int nott null

                                             name  varchar(10)  not null

                                            birthday     year  not null

                                             index   age(birthday)); #age  索引名  可省略

             查看索引是否正在使用

                    explain   select * from test1 where  birthday=1992 \G

             查看表结构       show create table

         2>  创建唯一索引

                create table test1(id int nott null

                                             name  varchar(10)  not null

                                            birthday    year  not null

                                             unique index  age(birthday ));  #age  索引名

         3> 创建组合索引               

               create table test1(id int nott null

                                             name  varchar(10)  not null

                                            birthday    year  not null

                                             index  mutiidx(id,name,birthday ));  #mutiidx  索引名

             组合索引遵循最左前缀原则,如id,name,birthday  创建了组合索引,索引可以搜 (id,name,birthday),(id,name),(id),但(name,birthday)则不能使用索引查询

         4 >创建全文索引                 

                create table test4(id int nott null

                                             name  varchar(10)  not null

                                            birthday    year  not null

                                           fulltext   index  full_name(name)           #full_name 索引名

                                          )  engine=MyISAM; 

             全文索引只有MyISAM存储引擎支持且只为 char,varchar和text列创建索引,不支持前缀。  注意:MYSQL5.7 中默认的是InnoDB 引擎,所以建表时需要修改表的存储引擎engine=MyISAM

全文索引非常适合大型数据集,对于小的数据集,用处比较小。

        5> 创建空间索引             

              create table test5(id int nott null

                                             name  varchar(10)  not null

                                            birthday    year  not null

                                           g    geometry  not null

                                           spatial   index  idx(g)           #idx  索引名

                                          )  engine=MyISAM; 

               创建空间索引字段不能为NULL,存储引擎必须是MyISAM,其他引擎不支持空间索引

               详细的空间索引函数参考MySQL5.6空间扩展(原创)-布布扣-bubuko.com

    5>> 在已存在的表上创建索引

           1> 查看索引    show index  from xx(表名)  \G

           2>  添加普通/单列索引    alter table test1 add index  idx(name(10));  idx 索引名 10为长度

                                                   create index idx on  test1(name(10));  

           3> 添加唯一索引   alter table test1 add   unique index  idx(name);  idx  索引名

                                         create unique index idx on test1(name); 

           4> 添加组合索引  alter table test1 add index  idx(name(10),birthday(20));

                                        create index idx on test1(id(5),name(10),birthday(20))  ;

           5> 添加全文索引  alter table test4 add   fulltext  index  idx(name);

                                        create fulltext index  idx on test4(name);

           6> 添加空间索引  alter table test5 add   spatial index  idx(g);

                                         create spatial index idx on test5(g);

      6>> 删除索引

            1> 使用alter table  删除索引

                  alter table test1 drop index idx;              #auto_increment约束的唯一索引不能被删除

             2> 使用drop index 语句删除索引

                 drop index idx on test1;    #  删表时删除列,该列也会从索引中删除,列全部删除,索引删除

     

                                        

            

                

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐