SQL has three types of indexes. Unique indexes cannot have duplicates, but clustered indexes and non-clustered indexes can have duplicates. Is the primary key a clustered index? Primary key, unique index clustered index relationship

important:

(1) If SQL is created, if the type is not specified, the default is a non-clustered index

(2) Both the clustered index and the non-clustered index can have duplicate records, and the unique index cannot have duplicate records.

(3) The primary key is a clustered index with a unique constraint by default, but it can also be specified as a non-clustered index with a unique constraint when the primary key is created. Therefore, the primary key is only a clustered index with a unique constraint by default. It cannot be said that the primary key is added. Unique Constraint Clustered Index

A bit of a mouthful, you can refer to my blog: Is the primary key a clustered index?

Creating an index for a column is actually sorting the column to facilitate query. Building an index for a column is equivalent to establishing a sorting for a column.

    The primary key is unique, so when a primary key is created, a unique index is created for this field.

    A unique index actually requires that all the data in the specified column must be different.

    The difference between a primary key and a unique index:

         1 A table can only have one primary key, and multiple unique indexes can be built.
         2 The primary key can be used as a foreign key to other tables.
         3 The primary key cannot be null, and the unique index can be null.

Clustered index: A directory that arranges data in a table according to certain rules. Because of this, there is only one focused index in a table. In this regard, we should note that "the primary key is the focused index", which is extremely wrong and a waste of the focused index. (Although the default primary key of SQL Server is the focused index) The biggest advantage of using the focused index is to quickly narrow the query range according to the query requirements and avoid full table scans. Secondly, making each field with a different number as a focused index also does not meet the "principle that a clustered index should not be established in the case of a large number of different cases".

First, the role of the index

1. Help to retrieve data;

2. Improve connection efficiency;

3. Save the time of ORDER BY and GROUP BY;

4. Ensure data uniqueness (only for unique indexes).

 

Second, the design of the index

When deciding to build an index, we first need to determine whether it is clustered or non-clustered, single-column or multi-column, unique or non-unique, column is ascending or descending, and its storage is like: partition, fill factor, etc. Let's look at it one by one:

1. Clustered index

(1) First point out a misunderstanding, the primary key is not necessarily a clustered index, but in SQL SERVER, if it is not clearly pointed out, the primary key is defined as clustered by default, while in Oracle it is non-clustered by default, because the ROWID in SQL SERVER Not open for use.

(2) The clustered index is suitable for columns that need to be searched in a range, because the leaf nodes of the clustered index store ordered data rows, and the query engine can directly locate the leaf nodes at both ends according to the range given in WHERE. The data of this part of the node page can be fetched according to the order of the linked list;

(3) The clustered index should be established on the column whose value will not change as much as possible, otherwise it will bring the maintenance of the non-clustered index;

(4) Try to establish a clustered index before establishing a non-clustered index, otherwise it will cause the reconstruction of all non-clustered indexes on the table;

(5) Clustered indexes should be avoided to be built on columns with monotonous values, otherwise it may cause IO competition and imbalance of B-trees, which will cause the database system to frequently maintain the balance of B-trees. The column values ​​of a clustered index are best distributed evenly in the table.

3. Unique index

(1) Another misunderstanding, a clustered index is not necessarily a unique index , because SQL SERVER defines the primary key as a clustered index by default, in fact, whether the index is unique and whether it is clustered is irrelevant, the clustered index can be a unique index, or Can be a non-unique index;

(2) Setting the index to unique is very beneficial for equal value search. When the first record that meets the conditions is found, the search can be stopped and the data will be returned. If the index is not unique, the search must continue. Similarly, due to the need for To ensure uniqueness, the insertion of each row of data will check the repetition;

 

Below is a simple comparison table

 

 

  primary key clustered index
use Enforce Entity Integrity for Tables Sort data rows for easy query
How many in a table A table has at most one primary key A table has at most one clustered index
Whether to allow multiple fields to define A primary key can be defined by multiple fields An index can be defined by multiple fields
     
Whether to allow null data rows to appear If there is null in the data column to be created, the primary key cannot be established.
PRIMARY KEY constraint columns specified when the table is created are implicitly converted to NOT NULL.
There is no limit to establish a clustered index column must be not null.
That is, the data that can be listed is null,
see the last comparison
Whether the data must be unique Require data to be unique Data can be unique or not. Depends on the UNIQUE setting you define for this index.
(This point needs to look at a comparison later, although your data column may not be unique, but the system will generate a unique column for you that you can't see)
     
created logic When the database creates the primary key, it will automatically create a unique index.
If the table does not have a clustered index before, and the non-clustered index is not forced to be specified when the primary key is established, a unique clustered index will be established at the same time when the primary key is established.
If the clustered index is not created with the UNIQUE attribute, the database engine automatically adds a four-byte uniqueifier column to the table.
When necessary, the database engine will automatically add a uniqueifier value to the row, making each key unique. This column and column values ​​are for internal use and cannot be viewed or accessed by users.
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325204041&siteId=291194637