[Notes] Clustered index, secondary index and joint index

  1. Clustered Index:

    • Features: Clustered index is the physical sorting method of data rows in the table. Each table can only have one clustered index. Normally, a clustered index is associated with a primary key, but if no primary key is explicitly defined, the InnoDB storage engine selects a unique non-null column as the clustered index.
    • Contact: The clustered index is actually the primary sort order for the table because it determines the physical arrangement of the data rows on disk. The leaf nodes of a clustered index contain the actual data rows, not just the index key values. Therefore, when you use a clustered index in a query, you can retrieve data faster.
  2. Secondary Index:

    • Features: A secondary index is any other index on the table except the clustered index. A table can have multiple secondary indexes, which do not change the physical ordering of data rows, but provide additional index paths to speed up queries.
    • Contact: The secondary index refers to the data row through the index key value, rather than directly containing the data row. When you query, the database first uses the secondary index to find the matching row's primary key, and then uses the primary key to find the actual row of data. Therefore, secondary indexes can speed up specific queries, but require an additional lookup step.
  3. Composite Index:

    • Features: A joint index is an index that contains multiple columns, which together form the index key value. Union indexes can be used to speed up queries on multiple columns, not just a single column.
    • Contact: The joint index can be a clustered index or a type of secondary index. If the union index is the primary key of the table, it is a clustered index. If the union index is not the primary key, it is a secondary index. The columns in the joint index form key values ​​in the order defined by the index, which can be used to speed up multi-column query conditions.

To summarize:

  • A clustered index is the primary physical ordering of a table, usually associated with a primary key.
  • Secondary indexes are additional indexes on a table that speed up queries but do not change the physical ordering of data rows.
  • A joint index is an index that contains multiple columns. It can be a clustered index or a secondary index and is used to speed up queries on multiple columns.

These index types play an important role in database query optimization and performance. It is very important to choose the appropriate index type based on different query requirements and data models.

Guess you like

Origin blog.csdn.net/weixin_44624036/article/details/134223452