zoukankan      html  css  js  c++  java
  • mysql

    mysql官方定义: 索引是帮助mysql高效获取数据的数据结构

    1.通过底层排序,试查询数据时有规律可循,增加查询速度。

    2.因此索引的本质是通过把关键字排序(大部分是B-Tree结构,还有可能是hash,full-text,R-Tree等)提高查找速度。大幅影响order by 和 where

    3.一般会将最常用的字段设为索引,增加查询速度,union, foreign key和primary key 会默认为索引。

    4.索引本身也很大,所以一般会以索引文件的形式存在磁盘上。

    优劣势:

    优势:增加了查找和排序效率

    劣势:占磁盘空间,降低了表的更新速度(增删改)

    所以:

    mysql会为union, foreign key 和 primary key 会自动建立索引

    频繁用在where, order by, group by 中作为查询,排序,分组的字段适合作为索引

    频繁需要更新的字段不适合作为索引

    数据数量在100万以下不需要索引,一般是300万以上才开始考虑优化问题。

    大量重复数据的字段不适合作为索引

    B树:Balance-tree(平衡多路查找树),可以理解为有多个子节点的平衡树。。。

     分类:

    单值索引:  一个索引仅有一列

    复合索引: 一个索引包含多个列

    索引的创建和删除:

    单值索引的两种方式:   为userinfo 的 username 添加索引。

    CREATE /*unique 如果这列的数据值唯一,可以设为唯一索引*/ INDEX index_userinfo_username ON userinfo(username); #索引名字一般是index_表名_列名
    DROP INDEX index_userinfo_username ON userinfo;
    
    ALTER TABLE userinfo ADD /*unique 如果这列的数据值唯一,可以设为唯一索引*/ INDEX index_userinfo_username(username);
    ALTER TABLE userinfo DROP INDEX index_userinfo_username ;

    #查看索引
    SHOW INDEX FROM userinfo;

     复合索引:

    CREATE INDEX 索引名字 ON 表名(列1, 列2);

     例子:

    CREATE INDEX index_userinfo_username ON userinfo(username, birthday);
  • 相关阅读:
    (easy)LeetCode 223.Rectangle Area
    (easy)LeetCode 205.Reverse Linked List
    (easy)LeetCode 205.Isomorphic Strings (*)
    (easy)LeetCode 204.Count Primes
    (easy)LeetCode 203.Remove Linked List Elements
    (easy)LeetCode 202.Happy Number
    (easy)LeetCode 198.House Robber
    (easy)LeetCode 191.Number of 1 Bits
    试题分析
    使用ADO.NET访问数据库
  • 原文地址:https://www.cnblogs.com/clamp7724/p/11806551.html
Copyright © 2011-2022 走看看