[MySQL] Introduction to Indexes

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wrs120/article/details/80711800

1. What is the Index

  The official definition: Index (Index) to help MySQL efficient access to data the data results, can be seen, the index is a data structure. We can be understood as the index is a row of data to achieve a good result in order to quickly find the
  shows, in addition to the data, the database system also maintains a data structure to meet the particular search algorithm , the data structure reference (point) data in some way so that you can achieve the advanced query algorithms on the data structure, this data structure is indexed. The figure below shows a possible indexing:
Write pictures described here
  on the left is the actual data stored, the far left is the physical address of the data record; the right is the index, in order to speed up the look Col2, maintainable find a binary number shown on the right, each each node contains a pointer to build the index value and a pointer to the physical address corresponding to the recorded data, so that you can use a binary search to the corresponding data acquired in a certain complexity to quickly retrieve the matching records


2. Why use index

  Improve query efficiency, the dictionary analogy, if you want to query "mysql" word, we first locate m, then down to find y, find sql, if there is no index, we have a - z traverse all over and over again, perhaps the first to find m, perhaps the last great find m, we build an index, you can directly locate


3. The index storage

  General index itself is that it is impossible in memory, so the index is often in the form of index files are stored on disk storage all


4. The operation of the index

Creating an index

CREATE [UNIQUE] INDEX indexName ON mytable(列名1,列名2)
ALTER mytable ADD [UNIQUE] INDEX indexName ON (列名1,列名2)

Write pictures described here
Delete Index

DROP INDEX indexName ON mytable;

View Index

SHOW INDEX FROM mytable

  The impact index is where and order by conditions


5. Advantage

  • Improve retrieval efficiency, reduce IO successful database (because each query will once IO, and the index reduces the number of queries)
  • 通过索引列对数据进行排序,降低数据排序的成本,降低了CPU的消耗(因为有了索引该列就自动排序了,再执行排序时就不用排序了)

6.劣势

  • 实际上索引也是一张表,该表保存了主键和索引字段,并执行实体表的记录,所以索引列也要占用空间
  • 降低了更新表的速度(insert,update,delete操作),因为更新表时,mysql不仅要保存数据,还要更新建值变化后的索引信息。总之:更新时,数据在变,索引也在变
  • 若数据量大,要考虑建立最优的索引

7.mysql索引分类

  • 单值索引:一个索引列只包含单个列,一个表可有多个单列索引
  • 唯一索引:索引类的值必须唯一,但允许为空
  • 复合索引:一个索引包含多列

8.mysql索引结构

索引结构分为:

  • BTree索引
  • Hash索引
  • full-text全文索引
  • R-Tree索引

Write pictures described here
【初始化介绍】
  一 颗 b + 树 , 浅 蓝 色 的 块 我 们 称 之 为 一 个 磁 盘 块 , 可 以 看 到 每 个 磁 盘 块 包 含 几 个 数 据 项 ( 深 蓝 色 所 示 ) 和 指 针 ( 黄 色 所 示 ) ,如 磁 盘 块 1 包 含 數 据 项 17 和 35 , 包 含 指 针 PI 、 P2 、 P3 , PI 表 示 小 于 17 的 磁 盘 块 , P2 表 示 在 17 和 35 之 同 的 磁 盘 块 , P3 表 示 大 于 35 的 磁 盘 块 ,真实的数据存在于叶子节点, 即 3 、 5 、 9 、 10 、 13 、 15 、 28 、 29 、 36 、 60 、 75 、 79 、 90 、 99 。非 叶 子 节 点 不 存 储 真 实 的 数 据 , 只 存 储 指 引 搜索方 向 的 数 据 项 , 如 17 、 35 并 不 真 实 存 在 于 数 据 表 中 ·
【查找过程】
  To find Han data items 29, the first magnet block 1 will be loaded from disk to memory, IO occurs a case, in the memory 29 is determined by a binary search in the same 17 and 35, the locking disk block P2 of the pointer 1, memory time is very short (compared to a disk IO) is negligible, the disk block 3 by loading a disk block pointer P2 from disk to disk memory address, the second IO occurred, with 29 of 26 and 30, the locking disk block pointer P2 3 through 8 pointer is loaded into memory disk blocks, the third IO occurred while the memory do binary search to find 29, the end of the inquiry, a total of three times IO. The truth is, the layer 3 b + tree can represent millions of data, if millions of data to find only three IO performance improvement would be great, if there is no index, each data item occurs once every IO then a total of millions of IO, obviously very, very high cost


9. What you need to create an index case

  • Primary key automatically create a unique index
  • Frequently as a query field
  • Sort field query, if the sort field to access the index will be greatly enhanced by sorting speed
  • Statistical query or group of fields
  • Query table associated with the other fields, i.e., foreign keys

10. What case do not need to create an index

  • Too little table records
  • Frequently updated fields
  • where conditions are less than the field
  • A data class contains many duplicate content (such as gender)

Selective index : the index table refers to the ratio of the number of different values in the class record number. If there are 2000 records in a table, the index table 1980 class has different values, the index item selection is 1980/2000 = 0.99, a selective index closer to 1, the higher the efficiency of the index

Guess you like

Origin blog.csdn.net/wrs120/article/details/80711800