What do Clustered and Non clustered index actually mean?

前端 未结 11 1895
粉色の甜心
粉色の甜心 2020-11-22 06:40

I have a limited exposure to DB and have only used DB as an application programmer. I want to know about Clustered and Non clustered indexes. I goo

11条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 07:15

    Clustered Index - A clustered index defines the order in which data is physically stored in a table. Table data can be sorted in only way, therefore, there can be only one clustered index per table. In SQL Server, the primary key constraint automatically creates a clustered index on that particular column.

    Non-Clustered Index - A non-clustered index doesn’t sort the physical data inside the table. In fact, a non-clustered index is stored at one place and table data is stored in another place. This is similar to a textbook where the book content is located in one place and the index is located in another. This allows for more than one non-clustered index per table.It is important to mention here that inside the table the data will be sorted by a clustered index. However, inside the non-clustered index data is stored in the specified order. The index contains column values on which the index is created and the address of the record that the column value belongs to.When a query is issued against a column on which the index is created, the database will first go to the index and look for the address of the corresponding row in the table. It will then go to that row address and fetch other column values. It is due to this additional step that non-clustered indexes are slower than clustered indexes

    Differences between clustered and Non-clustered index

    1. There can be only one clustered index per table. However, you can create multiple non-clustered indexes on a single table.
    2. Clustered indexes only sort tables. Therefore, they do not consume extra storage. Non-clustered indexes are stored in a separate place from the actual table claiming more storage space.
    3. Clustered indexes are faster than non-clustered indexes since they don’t involve any extra lookup step.

    For more information refer to this article.

提交回复
热议问题