The concept and use of primary index and secondary index

1. Concept

In databases, both primary and secondary indexes are common index types, with the following differences between them:

1. Different definitions

A primary index, also known as a clustered index or a primary index, is an index created on the primary key column in a table to uniquely identify each row of data. The data of the primary index and the data of the table are stored in the same physical file, sorted according to the value of the primary key column.

A secondary index, also known as a non-clustered index or a secondary index, is an index created outside of the primary index to improve query performance on non-primary key columns. Secondary indexes usually contain index columns and pointers to data rows, and can quickly locate data rows through index columns.

2. Different data storage methods

The first-level index stores the data of the table and the index in the same physical file, sorted according to the value of the primary key column. Therefore, a primary index can improve the performance of queries on primary key columns, but queries on other columns may not be as good as secondary indexes.

Secondary indexes only contain index columns and pointers to data rows, and do not contain data for other columns. The secondary index is usually stored in a separate file, which can be quickly searched through the index column, but needs to access the data row through the pointer.

3. The impact of the update operation is different

Primary and secondary indexes have different effects on update operations. For the primary index, because it contains all the data and index information of the table, the overhead of the update operation is relatively large. If the primary key column is updated, the data in the primary index and the data table need to be reordered, so the overhead of the update operation will be greater.

For secondary indexes, since only index columns and pointers to data rows are included, update operations are less expensive. If the index column is updated, the data in the secondary index and the data table need to be updated, but the index does not need to be reordered.

To sum up, both primary and secondary indexes are common index types, and the difference between them lies in the definition, data storage method, and impact of update operations. When designing the index of the data table, it is necessary to select the appropriate index type according to the actual situation.

Two. Use

In a database, primary and secondary indexes can be created through the following steps:

Create a primary index :

1. Select the table to create an index: When using the command line or graphical user interface of the database management system, you first need to select the table to create an index.

2. Select the primary key column to be indexed: After selecting the table, you need to select the primary key column to be indexed. The primary index is usually associated with the primary key of the table, so the primary key column needs to be selected as the index column.

3. Create an index: After selecting the index column, you can use the CREATE INDEX statement to create the index. For example, to create a primary index named "index_name" on the table named "table_name", you can use the following statement:

CREATE CLUSTERED INDEX index_name ON table_name (primary_key_column);

Among them, primary_key_column is the name of the primary key column to be indexed. Since the primary index is usually associated with the table's primary key, use the CREATE CLUSTERED INDEX statement to create a primary index.

4. Verify the index: After creating the index, you can use the EXPLAIN statement or other tools to verify that the index is being used correctly. If indexes are used correctly, the query's execution plan should include the name of the index.

Create a secondary index:

1. Select the table to create an index: When using the command line or graphical user interface of the database management system, you first need to select the table to create an index.

2. Select the columns to be indexed: After selecting the table, you need to select the columns to be indexed. Normally, choose frequently queried columns as index columns.

3. Create an index: After selecting the index column, you can use the CREATE INDEX statement to create the index. For example, to create a secondary index named "index_name" on a table named "table_name", use the following statement:

CREATE NONCLUSTERED INDEX index_name ON table_name (column_name);

Among them, column_name is the name of the column to be indexed. Since secondary indexes are usually not associated with the table's primary key, use the CREATE NONCLUSTERED INDEX statement to create secondary indexes.

4. Verify the index: After creating the index, you can use the EXPLAIN statement or other tools to verify that the index is being used correctly. If indexes are used correctly, the query's execution plan should include the name of the index.

 

Guess you like

Origin blog.csdn.net/m0_69057918/article/details/131066870