Advanced Mysql index entry

1. Index Operations

MySQL index rookie

2. Index type

  • PRIMARY
    unique and can not be empty; a table can have only one primary key index
  • INDEX
    general index
  • UNIQUE
    unique index
  • FULLTEXT
    full-text index: searching for a very long article, the best results. Used in a relatively short text, if it is a line or two, and can also be ordinary INDEX

3. VS non-clustered index clustered index

3.1 difference

* 聚集索引:主键索引,索引中键值的逻辑顺序决定了表中相应行的物理顺序
* 非聚集索引(非主键索引,也称二级索引):除主键索引(普通索引、唯一索引、全文索引),索引的逻辑顺序与磁盘上行的物理存储顺序不同

Query process:

  • Queries clustered index can directly get all the data,
  • Charles non-clustered indexes need to get a clustered index address, back to the table and then get the data.

3.1 clustered index rules

  1. If a primary key is defined, then the primary key is a clustered index
  2. If the primary key is not defined, then the first non-empty unique index of the table is indexed as aggregates
  3. If no primary key nor suitable unique index, innodb internally generates a hidden primary key as an aggregate index, the hidden primary key is a six byte column value reclassified will vary with inserted data increment.

4 index structure

By default B+Tree, Hash(Key-value of insertion and query time complexity of the hash table is O (1), if not ordered through the data, the best performance of the hash table.)

B + tree of binary evolution m TREES

Why is B + tree (with read and write characteristics of the disk, reducing the number of disk accesses a single query.)

4.1 B + tree features

---- US Geeks time of data structures and algorithms

  • The number of child nodes of each node can not exceed m, not less than m / 2;
  • The number of child nodes of the root can be no more than m / 2, which is an exception;
  • m-tree index only memory, the data is not really stored, somewhat similar to the jump table;
  • By list to string together a leaf node, so you can easily find the press section;
  • Typically, the root node will be stored in memory, the other nodes stored on disk.

the complexity

  • All operations (search, insert, delete) time complexity O (logm (N)),
  • Worst space complexity O (n)

4.2 m order to calculate how come?

Page read by the operating system (or default 4k 8K), in order to enhance I / O efficiency, and an operating system reads the index page space remains the same.

m = 数据页大小/索引项大小

Therefore, the index entry fields duty smaller space (int 4byte, less than half bigint 8byte), the more a stored index data, when the length of the index should also consider the optimization field.

Child node is a doubly linked list structure, convenient range queries and sorting.

Consider:
10 million data, how high the tree?

5. Cover Index

select 主键 from table where 普通索引字段 = ** ;

Covering indexes 概念: results directly into an index, the table does not need to return to operation.

Example: ID number and name

If you want to query information based on the ID number, as long as the index on the ID card, you need to build [ID, name] composite index it?

If a high-frequency query query name ID number, then build composite index of the top, you can achieve coverage index, you do not need back to the table found in the entire row of data, reducing execution time.

6. The most left-prefix principles

Two concepts:

  • The most left-prefix could be the leftmost composite index N fields
  • It may be the most left-string index of M characters .

Establishment of combination index (a, b, c) corresponding to established (a, b, c) (a, b) (a, c) (a) four index

As long as it matches the leftmost N fields, you can use the index. Such as [a, b, c] [a, c] [a, b] [a] can trigger index, the variable internal sequence, mysql automatically adjusted.

String index
leftmost M characters: as like x% ok,% x, % x% can not.

7. index pushdown

MySQL 5.6 index pushdown optimization (index condition pushdown) introduced

May index traversal, the fields included in the index do first judgment, to filter out direct recording condition is not satisfied, to reduce the number back to the table.

8. The index is in effect, optimization

You can use EXPLAIN to analyze whether the index onset, slow to do some sql index optimization Explain optimize queries detection

  • When the field is of type int index, condition can be '' may wrap a direct numerical comparison
  • Index field is of type varchar, the conditions to use the 'wrap

  • Can trigger range range index>, <, not in, in,! =, BETWEEN AND (5.5 version)

9. Common Index naming

唯一 uk_[字段名]_[字段名]...
普通 idx_[字段名]_[字段名]...

github
blog

Guess you like

Origin www.cnblogs.com/loveincode/p/mysql_index.html
Recommended