Index-related issues

Database Index

Database index is used to improve the speed of data access database tables.

Database indexing features:

a) avoid a full table scan database, in most cases, you only need to scan fewer index pages and data pages instead of querying all data pages. But also for non-clustered index, sometimes you do not need to access the data pages to get the data.

b) to avoid clustered index data inserting operation, focusing on the last page of the data table.

c) In some cases, the index sort operation can be avoided.

Following several aspects expansion index

  • Why use an index can speed up the retrieval speed database ah?

  • Why is the index will reduce insert, delete, modify the speed of maintenance tasks.

  • Hash index and the B + tree index What is the difference? Which mainstream use more? InnoDB storage support it?

  • Clustered index and non-clustered index What is the difference?

  • Leftmost index matching principle refers to what?

 

1.1 Why use an index can speed up the retrieval speed database ah?

The disordered data becomes relatively orderly data (just check the same directory)

1.2 Why is the index will reduce insert, delete, modify the speed of maintenance tasks.

Because MySQL stored in the index when the underlying data structure using a B + tree, B + tree is a balanced tree, if we change the additions and deletions to the old tree, then it certainly will destroy its original structure. To maintain the balance of the tree, you have to do extra work. Because of the extra work expenses, resulting in the index will reduce the rate of additions and deletions

1.3Hash index and the B + tree index What is the difference?

B + tree index:

Most MySQL storage engine is the default index type.

Because they no longer require a full table scan, you only need to search the tree, so much faster search speed.

Because the B + Tree of ordering, so in addition for finding, can also be used for sorting and grouping.

You can specify multiple columns as index columns, multiple columns together constitute the index key.

It applies to the whole key, key and key range prefix lookup, where the key prefix lookup apply only to the most left-prefix lookup. If the order is not to find the index column, you can not use the index.

B + Tree index InnoDB divided main index and a secondary index. Primary index leaf node data field records the complete data record, which is called indexing clustered indexes. Because there is no line data stored in two different places, so a table can have only one clustered index.

 

leaf node data field of secondary indexes primary key value is recorded, and therefore when a secondary index to find, it is necessary to find the primary key, and then to lookup the primary index.

 

It has the characteristics of dynamic equilibrium.
1. support range queries.
2. The other long access paths, the access time changes depending on the amount of data is relatively stable.
3. Find a clear direction.

Hash index:

The hash index is to use a certain hash algorithm, the key terms of the new hash value, do not need to retrieve similar to B + tree look like step by step from the root to leaf nodes with a single hash algorithm can be positioned immediately to the appropriate location, very fast. Essentially the key is to be converted into new hash value, according to locate the hash value.

It has a look-speed characteristics.
1. meet only "=", "IN" and "<=>" query, the query can not be used range. 

2. there is no way hash indexes use the index to complete the sequencing

3. does not support the leftmost matching principle

4. In a large number of duplicate keys, the hash index is very low efficiency ----> hash collision.

 

Mainstream or the use of B + tree index more for hash index, InnoDB adaptive hash index (hash indexes created automatically optimizes the engine created by the InnoDB storage engine, we can not interfere)!

 

 

1.4 aggregation and non-clustered index

Brief summary:

  • To the clustered index is the primary key index creation
  • Non-clustered index is to the non-primary key index creation

the difference:

  • Clustered index the leaf node is stored in the data table
  • Non-clustered index is stored in a leaf node the primary key and the index column
  • When using the non-clustered index query the data, get the primary key on the leaves again found data you want to find . (To get the primary key and then look for a process called back to the table )

Non-clustered indexes also called secondary indexes , do not tangle so many terms, it is equivalent on the line ~

非聚集索引在建立的时候也未必是单列的,可以多个列来创建索引。

  • 此时就涉及到了哪个列会走索引,哪个列不走索引的问题了(最左匹配原则-->后面有说)
  • 创建多个单列(非聚集)索引的时候,会生成多个索引树(所以过多创建索引会占用磁盘空间)

1.5索引最左匹配原则

最左匹配原则

  • 索引可以简单如一个列(a),也可以复杂如多个列(a, b),即联合索引
  • 如果是联合索引,那么key也由多个列组成,同时,索引只能用于查找key是否存在(相等),遇到范围查询(>、<、between、like左匹配)等就不能进一步匹配了,后续退化为线性查找。
  • 因此,列的排列顺序决定了可命中索引的列数

例子:

  • 如有索引(a, b,c),查询条件a = 1 and b > 4 and c > 8,则会在每个节点依次命中a、b,无法命中c。(很简单:索引命中只能是相等的情况,不能是范围匹配)

 

Guess you like

Origin www.cnblogs.com/du001011/p/11300648.html