Choosing clustered primary key index and

First, what is a clustered index?

First, a clustered index is not a separate index type, in fact, how data is stored. Clustered indexes store data and index put together, found the index will find the data.

In the MySql InnoDB engine, the file table data is a B + tree index structure according to the organization. According to the clustered index is the primary key of each table constructed B + tree leaf node is the entire row of data table, the clustered index leaf node is also called a data page.

Second, what is non-clustered index?

1. InnoDB engine

Primary key index is a kind of a clustered index, while others created out of the prefix index, joint index, the only index belong to the non-clustered index, also called a secondary index, secondary index.

Secondary index leaf node containing index key value, and their clustered index key (primary key).

Access to all the secondary indexes need to go through to find two indexes: the first find by primary key value of non-clustered index, and then find the line corresponding to the data through a clustered index based on the primary key value
Here Insert Picture Description
Here Insert Picture Description

2. MyISAM engine

In MyISAM, the index files and data files are separate index file only save the address data line. Primary key index and other indexes, are non-clustered index, the leaf rows of data storage address only.

Here Insert Picture Description

Third, the relationship between the pros and cons of the main keys to select the clustered index

First, because of InnoDB data structure, in fact, for each InnoDB tables are forced to build a clustered index:

  1. When the primary key definition, automatically build the primary key of a clustered index
  2. When no master key, can be automatically selected as the primary key column (non-null unique index) uniquely identifies a data record
  3. When two or more are not automatically generate a length of 6 bytes long integer hidden field as the primary key, a clustered index

Clustered index advantages and disadvantages :

Advantages :

  1. Faster data access than non-clustered index as clustered index data stored in a B + tree with less step than the non-clustered index addressing
  2. Clustered index applies to the sort of occasion
  3. Clustered index data takes a time range more efficient
  4. When accessing different rows of data in the same page, since the page has been loaded into the Buffer in time, again accessed, access will be completed in memory, without having to access the disk. If the primary key Id according to organize data, access data faster

Disadvantages:

  1. The speed of insertion depends heavily on the order of insertion, inserted according to the master key order high efficiency, and vice versa will result page splitting, seriously affect the performance
  2. Since the size of the data stored in the secondary index, secondary index will increase as the primary key increases
  3. Access to secondary indexes need to look twice

For the above shortcomings, we can see through the primary key value specification can be circumvented impact of these shortcomings:

  1. We will define a general increment primary key record insert according to ensure that primary key order. Physical clustered index data stored sequence is consistent with the order index, so long as the indices are adjacent, then the corresponding data must also be stored on the disk adjacent to the
  2. Our primary keys are not related to the business at any time should put an end to modify the primary key, primary key if you modify the scene appears, indicating that you should use a new field to store your original master key
Published 19 original articles · won praise 2 · Views 2400

Guess you like

Origin blog.csdn.net/Azhuzhu_chaste/article/details/105153063