Contact easy to understand the difference between an index, a separate index, a composite index, primary key, unique index, clustered index, non-clustered index, a unique clustered indexes

index

Database only do two things: data storage, data retrieval. The index is outside the data you store, preserve some additional signs (usually a B + tree), to reduce the time to retrieve data. Therefore, the main index is a data structure of the additional derived.

A table can create any number of indexes, each may be a combination of any number of fields. Index might speed up the search (using the index if the query), but it will slow down the write speed, because the need to update the index every write, so the index should only be applied to the column often need to search, do not add in the write once read many fewer columns.

And a separate index composite index

Field contains only a single index is called an index, which comprises two or more fields called composite index (or index combination). When you create composite indexes, the order of the fields is extremely important.

The following SQL statement in the column X, column Y, the establishment of a composite index column Z.

CREATE INDEX 索引名 ON 表名(列名X, 列名Y, 列名Z);

In fact, this is equivalent to the establishment of three indexes, namely:

1, a separate index (column X) 2, composite index (row X, column Y) 3, composite index (row X, column Y, the column Z).

How to understand it?

We can index more than one field combination of old-fashioned paper likened to a phone book, three are:

Surname - Name - Phone Number

In accordance with the contents of the phone book in alphabetical order of last name first, then first name of the same last name alphabetical order, which is equivalent on (last name, first name) the establishment of a composite index. You can quickly find all people with a specific surname phone number by the index, you can quickly find with a specific name - the name of the person the phone number combinations. However, imagine if you want to find the name of a particular person, in fact, this index is of no use, you can traverse the entire phone book from start to finish.

The only index and primary keys

Unique index is an index of a combination of one or more fields in the above table is established, this (or these) value of the field combination in the table can not be repeated. A table can create any number of unique index, but generally create only one.

A primary key is a special kind of unique index difference is that only the index column allows null value, and the primary key column does not allow a null value. A table create up to a primary key, you can not create a primary key.

Clustered index, non-clustered index, primary key

In the "Database Theory" is such a book to explain the difference between clustered index and non-clustered indexes are:

Clustered index leaf node is a data node, rather than clustered index leaf node is still inode, but there is a pointer pointing to the corresponding data block.

How to understand it?

Clustered indexes, that is, the physical order of the data on the hard disk. The primary key is the default clustered index under normal circumstances.

A table allows only one clustered index, the physical order of the real data as there is only one. If you are not a clustered index on the table, the newly created clustered index for it, we need to re-order existing data, so the table is modified drawback is slower clustered indexes for frequently updated column We should not build a clustered index.

Clustered index best performance, as soon as the recording has a first index value is found, a continuous record having index value must also be physically followed. A table can have only one clustered index, so very precious, must be carefully set up, generally select a (or more) fields in accordance with the table of the most common SQL query as a clustered index (or composite clustered index) .

Clustered index default primary key, if the table does not define the primary key, the InnoDB [. 1] selects a unique non-empty index instead of ( "the only non-empty index" refers to a unique index column can not occur null value, with the same primary key properties ). If there is no such index, InnoDB implicitly defined as a primary key clustered index.

Clustered indexes and unique indexes

Strictly speaking, the clustered index is not necessarily a unique index, clustered index index value is not required to be unique, a unique clustered index is! In a clustered index on columns can be inserted two or more of the same value, the same value on the hard disk in the physical order of the sorted clustered index is the same, nothing more.

Guess you like

Origin www.cnblogs.com/heqiyoujing/p/11229260.html