Database indexing operations

index

What is the index

可以理解为: 搜索引导,索引是一个特殊的数据结构,其存储的是数据的关键信息与详细信息的位置对应关系  

​例如:书本的目录

Why Index

Speed ​​up queries, when the amount of data is very large, one query data is very slow

The impact of index

​1.不是说有了索引就能加速,得看你的查询语句有没有正确使用索引  

​2.索引也需要占用额外的数据空间  

​3.添加索引后 将导致,增减删除修改变慢  (写入)

​什么样的数据应该添加索引:

​查询操作较多写入较少并且数据量很大时

​查询与写入操作的占比,10:1 或者查询更多  

​本质上索引原理是尽可能的减小搜索范围  

Disk IO

The average search takes at least a data 9ms this is the CPU will switch to other programs,

We want to speed up queries, the number must be less io operations

Index data structure

b + tree

In the b + tree leaf node is stored in the real data, the more the number of leaves, the higher the level of the tree, resulting in increased number of IO

To avoid this problem, store more data as possible in a leaf node, it should be a small amount of data as the index field

Leftmost matching principle

When the data item is a compound b + tree data structure, such as (name, age, sex) when (multi-column joint index), b + tree search tree will be established in accordance with the order from the left, such as when (Zhang, 20, F) so that the time to retrieve the data, b + tree name priority comparison determines the next search direction, if the same name and age Sex comparison in turn, finally obtained data retrieved; but (20, F) such data is not to name the time, b + tree node does not know what the next step to the investigation, because the time to establish the search tree name is the first comparative factor, you must first name according to the search query in order to know where to go next . For example, when (Zhang, F) ​​to retrieve such data, b + tree name can be used to specify the search direction, but the lack of age next field, so only the name is equal to the seating of the data is found, then the matching sex F of the data, this is a very important property, namely the left-most matching characteristics of the index.

Clustered index

​聚集索引中包含了所有字段的值,如果拟制定了主键,主键就是聚集索引,如果没有则找一个非空且唯一的字段作为聚集索引,如果也找不着,自动生成一个字段作为聚集索引

​聚集索引中存储了所有的数据

Secondary indexes

​除了聚集索引以外的都叫做辅助索引

​辅助索引中只包含当前的索引字段和主键的值

Cover the query

​指得是在当前索引结构中就能找到所有需要的数据  ,如果使用的是聚集索引来查询那么一定覆盖查询,速度是最快的

Back to the table query

​指得是在当前索引结构中找不到所需的数据,需要通过id 去聚集索引中查询 ,速度慢与聚集索引

Conclusion Index

​1.使用占用空间最小的字段来作为索引

​2.不要再一行中存储太多的数据,例如小说,视频,如果字段太多可以分表  

​3.尽量使用覆盖查询  

​4.如果字段区分度低(重复度高),建立索引是没有意义,反过来说应该将区分度高的字段作为索引

​5.模糊匹配中,百分号尽量不要写在前面

​6.不要再等号的左边做运算

​例如:select count(*) from usr where id * 3 = 6; 也会遍历所有记录

​7.and语句中会自动找一个具备缩印的字段优先执行,所以我们应该在and语句中至少包含一个具备索引的字段

​8.or语句要避免使用,如果要用则保证所有字段都有索引才能加速

​9.联合索引中,顺序应该将区分度最高的放到左边,最低的放右边,

​查询语句中必须保证最左边的索引出现在语句中

​另外需要注意:如果要查询的数据量非常大 索引无法加速

​总结: 不是添加了索引就能提速,需要考虑索引添加的是否合理,sql语句是否使用到了索引

The syntax to create an index

创建索引的语法:

​create index 索引的名字  on 表名称(字段名)

联合索引

​create index  索引的名字  on 表名称(字段名,字段名)

​create index union_index on usr(id,email,name,gender);

删除索引:

​drop index 索引名称 on 表名称;

Guess you like

Origin www.cnblogs.com/bladecheng/p/11209499.html