zoukankan      html  css  js  c++  java
  • Mysql学习笔记(八)索引

    PS:把昨天的学习内容补上...发一下昨天学的东西....五月三日。。。继续学习数据库...

    学习内容:

    索引....

    索引的优点:

    1.通过创建唯一索引,可以保证数据库每行数据的唯一性...

    2.使查找的速度明显加快...

    3.当使用分组和排序进行查询时,可以缩短时间...  

    索引的缺点:

    1.维护索引需要耗费数据库的资源...

    2.索引需要占用磁盘空间...

    3.对表进行增删改的时候,由于索引的存在,时间会有所增加...

    索引的分类...

    普通索引和唯一索引...

    普通索引和唯一索引大致相同,都是属于b-tree结构....唯一不同点就是唯一索引的数据列可以为空,并且每一行的索引列的数据唯一...

    单列索引和复合索引...

    单列索引就是定义一个索引的参数是唯一一个,复合索引就是定义索引的时候包含的参数可以是多个...并且复合索引的查询必须要使用第一个字段,否则会产生不走索引的情况...原因是复合索引遵循最左前缀集合...

    全文索引

    全文索引就是定义索引的列上支持值的全文查找,允许在这个列上插入重复值和空值,全文索引可以在字符或者文本上进行定义...存储引擎为Myisam...

    空间索引

    空间索引必须在myisam表进行创建...是对空间的数据类型的字段建立的索引..空间数据类型有四种geometry,point,linestring,polygon...空间索引使用关键字spatial进行扩展...(说实话,我也不完全清楚空间索引到底能用在什么地方)...

    创建索引的语句....

    CREATE TABLE table_name[col_name data type]
    [unique|fulltext|spatial][index|key][index_name](col_name[length])[asc|desc]

    普通索引:

    create table book
    (
        bookid int not null,
        bookname varchar(255) not null,
        author varchar(255) not null,
        info varchar(255) not null,
        comment varchar(255),
        year_publication year not null,
        index(year_publication)
    );
    show create table book;//显示我们建立的表格的相应信息...
    insert into book values(1,'java','James Gosling','good','good',1982);
    
    explain select * from book where year_publication=1982;

    显示数据会有很多的属性..简单的介绍一下...

    id:第几个..select_type:表示查询语句select的复杂程度..table:表格名称...type:访问类型..possible_key:显示索引...key:索引的个数...

    key_len:显示使用索引的字节数...ref:显示哪个列被用于索引查找...rows:找到索引所要读取的行数...extra:表示不适合在列中显示,但是又很重要的额外信息...

    唯一索引:

    唯一索引列的索引值必须唯一,如果是复合索引,复合索引的列组合的值也必须唯一...

    create table t1
    (
       id int not null,
       name varchar(255) not null,
       age int not null,
       info varchar(255),
       unique index multidx(id,name)//复合唯一索引...
    );
    
    Insert into t1 values(1,'we',20,'misaya'),(2,'omg',21,'gogoing');

    使用explain查询语句...

    explain select * from t1 where id=1 and name="we";//使用索引进行查找...
    explain select * from t1 where name="we"//造成了不走索引。。未遵循匹配最左前缀原则...

    全文索引...

    Fulltext索引可以用于全文索引...只有myisam存储引擎支持全文索引..全文索引不支持过滤索引..

    CREATE TABLE t4 (
      id INT NOT NULL,
      NAME CHAR(30) NOT NULL,
      age INT NOT NULL,
      info VARCHAR (255),
      FULLTEXT INDEX FulltxtIdx (info)
    ) ;
    insert into t4 values(1,'we',20,'misaya');//插入数据
    
    select * from t4 where match(info) against('misaya');//使用全文索引来查找数据..

    空间索引:空间索引,用于地理空间类型...自己弄得也不是很明白...所以在这里不进行介绍,避免误人子弟...

    在已有的表中去加入索引....

    两种方式:

    create table t1
    (
       id int not null,
       first_name varchar(255) not null,
       last_name varchar(255) not null,
       dob date not null
    );
    
    //添加索引....
    
    第一种方式....
    
    alter table table_name add[unique][filltext][spatial]index】【key[index_name] (属性值) 【asc】【desc】.....
    
    alter table t1 add index firstidx(first_name);//使first_name成为索引...
    
    show index  from t1;//查看我们建立的索引...
    
    第二种方式....
    
    create [unique][filltext][saptial]indexon table_name(属性值)【asc】【desc//这个不支持key,。插入key会报错...
    create index lastidx on t1(last_name);//使last_name为索引...

    删除索引:

    两种方式
    
    1.alter table table_name drop index index_name;
    
    alter table t1 drop index firstidx;//删除索引...
    
    2.drop index index_name on table_name;
    
    drop index lastidx on t1;//删除索引lastidx...

    注意:anto_increment 约束字段的唯一索引是不允许被删除的....

    无论是添加了索引还是删除了索引,我们都可以使用shou create table table_name来查看数据表建立的情况....

  • 相关阅读:
    转场动画3-手势返回
    UITableViewCell的选中时的颜色设置
    git的使用
    MarkDown语法学习笔记
    移动作业
    移动开发音乐播放器
    python循环删除list中的元素
    Linux权限管理
    Linux用户和用户组
    数据库常用基础操作
  • 原文地址:https://www.cnblogs.com/RGogoing/p/4470876.html
Copyright © 2011-2022 走看看