MySQL Innodb covering indexes and primary key index and unique index Analysis

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_39082172/article/details/102722589
  • Primary key index

Primary key index is clustered indexes, each table has one and only one primary key, by one or more fields.
Primary key index of the three conditions: 1. The key must be unique primary key can not contain a null value 2. 3. The primary key is the ensure the self-energizing
an auto-increment primary key can also increment sequentially written in, the storage efficiency can be improved
grammar:

alter table table_name add primary key(column_name);

A plurality of primary key fields

alter table table_name add primary key(column_name1,column_name2);
  • The only index

Is a unique index constraint (constraint is not be null value, can not be repeated, etc.) conditions, values can not be duplicated, but can have a null value, the table can have only one primary key, but can have a plurality of unique index .
grammar:

alter table table_name add unique (column_name);
  • Covering index

A covering index refers to the same too can return query data required by the index file, the data does not have to get back to the table by operations that query column to be covered by the index used. Indexed fields covered field happens to be involved in the query, that is, the index contains all the data query is looking for. Reduce the number of I / O operations, improve query efficiency

eg: Table A have a primary key id, name fields by name to query id

select id from A where name='张三' ; 

Known general index is (the primary key - index fields) such storage, look through the primary key general index field line data reverse think about it, we name changed to ordinary index, so the name field to bring the primary key id, by name retrieval time It can be obtained directly primary key id. This data can be queried directly from the index file. This is covered by the index. Similarly multiple fields queries can also be set to joint index.

Note: Be sure to let the select list columns needed, select * This operation can not occur, whether the index is invalid.

Guess you like

Origin blog.csdn.net/qq_39082172/article/details/102722589