zoukankan      html  css  js  c++  java
  • Mysql学习---索引的学习 180101

    索引:约束 + 快速查找

    索引是数据库中用于帮助用户快速查询数据的一种数据结构。类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获取即可。

    问:为什么索引可以这么快?[类似书的目录]

    答:Mysql默认情况下,扫描一行一行的扫描所有表中的数据,直到遇到我们需要的数据,才结束查询。有了索引之后,Mysql会对我们创建了索引的列单独创建一个文件,同时将表中列中的数据进行一个转换[将name列中的hhh转换为数字],然后按照B+树的顺序排序,也根据B+树查找

    image_thumb[3]

    MySQL中常见的索引:索引都是唯一的

    普通索引 - 加速查找,重复数据重复查找

    唯一索引 - 加速查找,约束列数据不能重复,数据可以为null

    主键索引 - 加速查找,约束列数据不能重复,数据不能为null

    组合索引 - 多列可以创建一个索引文件

    注意:索引适用于不常修改的列,仅用于查询操作

    普通索引:  加速查找

    创建普通索引:

    方案一:创建表的时候创建索引

    方案二:手动创建

    方案一:创建表的时候创建索引

    create table tb1(
        nid int not null auto_increment primary key,
        name  varchar(32) not null,
        email varchar(64) not null,
        extra text,
        index ix_name (name)   # 为neme列创建普通索引,普通索引数据可以重复
    )engine=innodb default charset='utf8'         
    
    

    方案二:手动创建

    create index ix_email on tb1(email) # 为email列创建索引
    
    删除普通索引:
    drop index ix_email;
    
    查看普通索引:
    show index from tb1;

    image_thumb[4]

    唯一索引:加速查询 和 唯一约束(可含null)

    创建唯一索引:

    方案一:创建表的时候创建索引

    方案二:手动创建

    方案一:创建表的时候创建索引

    create table tb2(
        nid int not null auto_increment primary key,
        name  varchar(32) not null,
        email varchar(64) not null,
        extra text,
        unique ix_name (name)   # 为neme列创建唯一索引,唯一索引数据不可以重复
    )engine=innodb default charset='utf8'    

    方案二:手动创建

    方案二:手动创建
    create unique index ix_email on tb2(email) # 为email列创建唯一索引
    
    删除唯一索引:
    drop unique index ix_email;
    
    查看唯一索引:
    show index from tb2;

    主键索引:加快查找、不能重复 和 不能为空

    创建主键索引:

    方案一:创建表的时候创建索引

    方案二:创建表的时候创建索引  --> primary key(nid)

    方案三:手动创建,一个表只能有一个主键

    方案一:创建表的时候创建索引

    create table tb3(
         nid int not null auto_increment primary key,   # 为nid列创建主键索引,主键索引数据不可以重复, 不可以为null
        name  varchar(32) not null,
        email varchar(64) not null,
        extra text,
    )engine=innodb default charset='utf8' 

    方案二:创建表的时候创建索引  --> primary key(nid)

    create table tb3(
        nid int not null auto_increment,
        name  varchar(32) not null,
        email varchar(64) not null,
        extra text,
        primary key (nid)   # 为nid列创建主键索引,主键索引数据不可以重复, 不可以为null
    )engine=innodb default charset='utf8' 

    方案三:手动创建,一个表只能有一个主键

    alter table tb3 add primary key(email) # 重复会报错, Multiple primary key defined
    
    删除主键索引:
    alter table tb3 drop primary key;
    
    查看主键索引:
    show index from tb3;

    组合索引:多列创建一个索引

    - 普通组合索引:   无约束,且遵循最左匹配

    - 联合唯一索引:   有约束,两列数据同时不相同,才能插入,不然报错

    image

    创建组合索引:

    方案一:创建表的时候创建索引

    方案二:手动创建

    方案一:创建表的时候创建索引

    create table tb4(
         nid int not null auto_increment primary key,# 为nid列创建主键索引,主键索引数据不可以重复, 不可以为null
        name  varchar(32) not null,
        email varchar(64) not null,
        extra text,
        index(name, email)       # 创建普通组合索引
      # unique(name, email)      # 创建唯一组合索引
    )engine=innodb default charset='utf8'  

    方案二:手动创建

    方案二:手动创建
    create index ix_name_email on in4(name,email);  # 重复会报错, Multiple primary key defined
    
    删除主键索引:
    drop index ix_name_email;
    
    查看主键索引:
    show index from tb4;

    按照索引查找方式分

    覆盖索引 + 合并索引

    image

    如何有效的使用索引:

    数据库表中添加索引后确实会让查询速度起飞,但前提必须是正确的使用索引来查询,如果以错误的方式使用,则即使建立索引也会不奏效。

    -- 使用做匹配的like
    	select * from student where sname like '%hhh'  # 不推荐,因为左查找,先匹配%后匹配hhh,匹配范围是All
    	select * from student where sname like 'hhh%'   # 推荐,因为左查找,先匹配cnn,匹配范围是range
    -- SQL语句中尽量不要使用函数
    	
    -- 类型不一致,导致索引查询失败
       select * from tb1 where name = 999;
    -- 特别的,Mysql中使用小于,等于的时候默认是使用索引的,但是大于的时候就不走索引了
    

    【更多参见】http://www.cnblogs.com/wupeiqi/articles/5716963.html

  • 相关阅读:
    运算符优先级
    Tips—查询某结构体
    在线词典--(一、流程分析)
    数据库—SQLite3
    回调函数(转载)
    UNIX域套接字
    进程间通信小结
    HDU_oj_2027 统计元音
    HDU_oj_2026 首字母变大写
    HDU_oj_2025 查找最大字母
  • 原文地址:https://www.cnblogs.com/ftl1012/p/9385314.html
Copyright © 2011-2022 走看看