美文网首页
第27课 索引

第27课 索引

作者: 猫哥的技术博客 | 来源:发表于2019-04-13 22:59 被阅读0次

什么是索引

相当于书籍的目录, 加快查询速度

是不是索引越多越好?

索引会加快查询速度, 但是会拖慢写入速度, 因为每写入一条数据, 都需要重建索引

索引什么时候有用

条件越明确, 索引越有用
通俗点说, 经常where哪个字段, 就给哪个字段加索引

image.png

试验一下

我们为了看出速度上的差别, 我们需要3,000,000行数据...
使用存储过程进行插入

drop table if exists test;

create table test(
    id int,
    name varchar(20),
    sex char(1) default '男',
    age int not null
);

drop procedure if exists batch_insert;

create procedure batch_insert() begin 
    declare i int default 0;
    declare sex_str char(1) default '';
    declare age_int int default 0;
    declare name_str varchar(20) default '';


    while i< 3000000 do
        set i = i + 1;
        set name_str = CONCAT('张三_',i);
        if age_int > 110 then 
            set age_int = 1;
        end if;
        set age_int = age_int + 1;
        if i%3 = 0 then 
            set sex_str = '女';
        else 
            set sex_str = '男';
        end if;

        insert into test(id,sex,age,name) values(i,sex_str,age_int,name_str);
        if i % 100000 = 0 then 
            select CONCAT('当前是第',i,'行...');
        end if;
    end while;
end;

call batch_insert();

耗时比较

image.png

mysql支持多种索引

mysql索引.png

创建索引

建表时创建

drop table if exists test2;
create table test2( 
    id int not null,
    name varchar(20) not null,
    sex tinyint(1) not null,
    age tinyint(1) not null,
    index(id),
    index(name),
    index(sex),
    index(age)
);

desc test2;
image.png image.png

建表后创建

create index 索引名称 on 表名(字段名)
image.png

查看索引

show index from 表名;
image.png

删除索引

drop index 索引名称 on 表名;
image.png

相关文章

  • NumPy(索引、array合并与分割)

    一、Numpy索引 一维索引 二维索引 二维索引取值 切片操作注:1:3表示对第2到第4进行切片输出(不包含第4)...

  • MySQL5.7从入门到精通(9-12章)--索引、存储过程、视

    第9章 索引 9.1 索引简介 9.1.1 索引的含义和特点 9.1.2 索引的分类 9.1.3 索引的设计原则 ...

  • pandas 索引

    索引方式 布尔索引 位置索引: df.iloc,Selection by Position,即按位置选择数据,即第...

  • 索引原理-聚簇索引

    索引分为聚簇索引和非聚簇索引。 以一本英文课本为例,要找第8课,直接翻书,若先翻到第5课,则往后翻,再翻到第10课...

  • 索引

    索引 什么是索引 索引是用于快速找出在某个列中有一特定值的行。 不使用索引,MySQL必须从第1条记录开始然后读完...

  • Mysql索引读书笔记

    ################################## 高性能Mysql第5章:索引基础 #####...

  • 搜索引擎

    第 8 章:布尔代数和搜索引擎 搜索引擎: 自动下载尽可能多的网页——下载 建立快速有效的索引——索引 根据相关性...

  • 《数据库索引设计与优化》术语表

    索引设计方法中所涉及的术语 设计最佳索引的算法 三星索引:宽索引。覆盖所有所需列(第3颗星),所需扫描的区域尽可能...

  • 搜索引擎优化(seo)读书笔记|欧朝晖版

    第1章 搜索引擎和搜索引擎优化的同步发展 如何获得来自搜索引擎的流量,就引入了搜索引擎营销这个话题 什么是搜索引擎...

  • 信息检索复习(2)——词项词典及倒排记录表

    构建倒排索引步骤 收集待建索引的文档(Document) 对这些文档中的文本进行词条化(Tokenizer) 对第...

网友评论

      本文标题:第27课 索引

      本文链接:https://www.haomeiwen.com/subject/kvarwqtx.html