• mysql索引入门-黑马


    11 索引B树

    多路平衡查找树

    动态构建B树

    裂变

    12 索引B+树

    所有元素都会出现在叶子节点

    非叶子节点起到索引作用

    叶子形成了一个单向链表

    mysql在原B+树基础上,增加了一个指向相邻节点的链表指针。

    一个页默认16k

    15 索引分类

    主键索引

    只能有一个

    唯一索引
    常规索引
    全文索引

    索引的存储形式

    聚集索引

    必须有,只能有一个

    叶子节点挂的数据就是一行的数据

    二级索引

    叶子节点挂的是id值。

    聚集索引选取规则

    如果有主键,使用主键;

    如果没有主键,用第一个唯一索引;

    没有主键,生成要给rowid;

    回表索引

    根据二级索引找到主键值,再根据聚集索引拿到这一行的数据。

    16 思考题

    select * from user where id=10;
    
    select * from user where name='Ann';
    //先扫描name字段索引,再扫描聚集索引
    
    • 1
    • 2
    • 3
    • 4
    Innodb主键索引的B+树高度?

    innodb指针占用6个字节,假设主键使用Bigint,每个用8字节。

    n*8+(n+1)6=161024

    高度为2,n等于1170;

    高度为3,可以存储两千多万数据。

    17 语法

    创建索引

    单列索引 /联合索引

    查看索引
    show index from table_name;
    
    • 1
    删除索引
    drop index index_name on table_name;
    
    • 1

    实操

    select * from tb_user;
    
    1 name,该字段可能重复,常见索引
    2 phone 非空,唯一,创建唯一索引
    3 profession/age/status创建联合索引
    4 email创建合适索引提高查询效率
    
    show index from tb_user;
    
    show index from tb_user\G;
    
    create index idx_user_name on tb_user(name);
    
    show index from tb_user;
    
    create unique index idx_user_phone on tb_user(phone);
    
    create index idx_user_pro_ages_sta on tb_user(profession,age,status);
    //创建联合索引顺序有讲究。
    
    create index idx_user_email on tb_user(email);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    (电力开发)376.1 主站通信协议基本结构解析
    Js使用ffmpeg进行视频剪辑和画面截取
    手机技巧:推荐一款手机省电、提升流畅度APP
    数据结构 栈与队列详解!!
    mysql中binlog日志
    WebRTC系列-网络之RTCP请求关键帧(requestKeyFrame)
    丹青映画携梦枕貘巨著《暗狩之师》参加玩协四展
    Perl爬虫程序
    关于springboot访问tomcat,线程http-nio-8080-exec的来源问题
    Vue的三种网络请求方式
  • 原文地址:https://blog.csdn.net/qq_42938698/article/details/126587911