• 数据库中的索引以及索引的底层实现原理(平衡二叉树)


    索引是用来加快数据的检索速度的,好理解一些可以解释为一本书的目录。合理的使用可以极大的提高访问性能。

    1.)索引的分类

        索引分为单列索引和多列索引,主键约束自带主键索引,唯一约束自带唯一索引。

    2.)索引的创建

        2.1)单列索引的创建

            create index 索引名 on 表名(列名);

            例如:create index emp_index_ename on emp (ename);

        2.2)多列索引(复合索引)

            create index 索引名 on 表名(列名1,列名2,,,,);

            例如:create index emp_index_complexindex on emp (ename,job);

    3.)索引的使用

      向person表中插入5000000条数据测试索引,person表结构:

        SQL> desc person;
      Name Type Nullable Default Comments 
      ------- ------------ -------- ------- -------- 
      PID NUMBER(10) Y 
      PNAME VARCHAR2(30) Y 
      PADRESS VARCHAR2(20) Y

      //向表中插入数据

      create index person_index_ename on person(pname);;//创建单列索引
      begin 
      for i in 1..5000000
        loop 
          insert into person values(person_pid_seq.nextval,'name'||i,'adress'||i);
        end loop
        commit;
      end;

      建立单列索引:create index person_index_ename on person(pname);

      没建立索引之前查询:select * from person where pname='name1000001' ;//耗时0.110秒

      建立索引之后查询:select * from person where pname='name1000001' ;//耗时0.101秒,不知道咋回事,电脑运行太慢了,这个数据应该不准,但是确实是这个,,无语。。

      -----------------------------

      创建多列索引:create index complexindex_person on person(pname,padress);

      没建立索引之前查询:select * from person where pname='name1000001'  and padress='adress1000001';//耗时0.112秒

      建立索引之后查询:select * from person where pname='name1000001'  and padress='adress1000001' ;//耗时0.103秒

    4.)索引的使用场景

        在需要经常查询的列上添加索引有利于提高检索速度。要合理的使用索引。索引里面保存的是rowid,如果修改插入数据会影响效率。

    5.)索引的实现原理

        索引底层的实现原理是平衡二叉树。

    6.)删除索引

        drop index index_name;

  • 相关阅读:
    退役 AFO
    object_id()函数
    DataRow数组根据指定列排序
    窗口函数详解
    窗口函数详细用法
    聚合函数count里面加条件
    C#全局钩子和局部钩子记录
    SqlServer日期时间函数
    SQL中字符串截取、连接、替换等函数的用法
    C# 字符串多行显示、文本换行
  • 原文地址:https://www.cnblogs.com/jasonboren/p/10921753.html
Copyright © 2020-2023  润新知