zoukankan      html  css  js  c++  java
  • 十.数据库(索引index)

    一 .索引(index)

    1.索引的介绍

    数据库中专门用于帮助用户快速查找数据的一种数据结构。类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置吗,然后直接获取。
    约束和加速查找

     2. 常见的几种索引:

    - 普通索引
    - 唯一索引
    - 主键索引
    - 联合索引(多列)
    
       - 联合主键索引 
    
      - 联合唯一索引   
       - 联合普通索引
    无索引: 从前往后一条一条查询
    有索引:创建索引的本质,就是创建额外的文件(某种格式存储,查询的时候,先去格外的文件找,定好位置,然后再去原始表中直接查询。但是创建索引越多,会对硬盘也是有损耗。
    
    建立索引的目的:
    a.额外的文件保存特殊的数据结构
    b.查询快,但是插入更新删除依然慢
    c.创建索引之后,必须命中索引才能有效
    
    无索引和有索引的区别以及建立索引的目的
    hash索引和BTree索引
    (1)hash类型的索引:查询单条快,范围查询慢
    (2)btree类型的索引:b+树,层数越多,数据量指数级增长(我们就用它,因为innodb默认支持它)

     3. 普通索引

    
    
    create index 索引的名字 on 表名(字段)               创建 索引字段
    drop index 索引的名字 on 表名                           删除索引字段
    show index from 表名                                        显示索引字段

    create table userinfo( nid int
    not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, index ix_name(name) );

     4. 唯一索引

    
    
    create unique index 索引名 on 表名(列名)      创建唯一索引字段
    drop index 索引名 on 表名;                    删除索引表名

    create table userinfo( id int
    not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, unique index ix_name(name) );

     5. 主建索引

    主键索引有两个功能: 加速查找和唯一约束(不含null)
    alter table 表名 add primary key(列名);

    alter table 表名 drop primary key;
    alter table 表名 modify 列名 int, drop primary key;

    create table userinfo( id int
    not null auto_increment primary key, name varchar(32) not null, email varchar(64) not null, unique index ix_name(name) ) or create table userinfo( id int not null auto_increment, name varchar(32) not null, email varchar(64) not null, primary key(nid), unique index ix_name(name) )

    6. 组合索引

     组合索引是将n个列组合成一个索引
    
     其应用场景为:频繁的同时使用n列来进行查询,如:where name = 'alex' and email = 'alex@qq.com'。
    
    
    create index 索引名 on 表名(列名1,列名2);

     7. 索引名词

    #覆盖索引:在索引文件中直接获取数据
            例如:
            select name from userinfo where name = 'alex50000';
    
    
    #索引合并:把多个单列索引合并成使用
            例如:
            select * from  userinfo where name = 'alex13131' and id = 13131;
  • 相关阅读:
    es 基于match_phrase/fuzzy的模糊匹配原理及使用
    感谢帮助我的人们
    ps6—如何安装笔刷
    如何下载安装Photoshop cs 6(供新手)
    axure rp 使用心得
    信安协会作业2
    CentOS7下安装Docker
    20181330 王茜《网络对抗技术》 Exp8 Web综合
    20181330 王茜《网络对抗技术》Exp7 网络欺诈防范
    20181330 王茜《网络对抗技术》Exp6 MSF基础应用
  • 原文地址:https://www.cnblogs.com/Sup-to/p/11245028.html
Copyright © 2011-2022 走看看