首页 > 数据库技术 > 详细

数据库中的索引

时间:2019-05-26 10:21:17      阅读:131      评论:0      收藏:0      [点我收藏+]

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

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;

数据库中的索引

原文:https://www.cnblogs.com/jasonboren/p/10921753.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!