What are the differences between a clustered and a non-clustered index?

后端 未结 12 1337
攒了一身酷
攒了一身酷 2020-11-29 14:24

What are the differences between a clustered and a non-clustered index?

相关标签:
12条回答
  • 2020-11-29 14:51

    Clustered Index

    • Only one clustered index can be there in a table
    • Sort the records and store them physically according to the order
    • Data retrieval is faster than non-clustered indexes
    • Do not need extra space to store logical structure

    Non Clustered Index

    • There can be any number of non-clustered indexes in a table
    • Do not affect the physical order. Create a logical order for data rows and use pointers to physical data files
    • Data insertion/update is faster than clustered index
    • Use extra space to store logical structure

    Apart from these differences you have to know that when table is non-clustered (when the table doesn't have a clustered index) data files are unordered and it uses Heap data structure as the data structure.

    0 讨论(0)
  • 2020-11-29 14:51

    Pros:

    Clustered indexes work great for ranges (e.g. select * from my_table where my_key between @min and @max)

    In some conditions, the DBMS will not have to do work to sort if you use an orderby statement.

    Cons:

    Clustered indexes are can slow down inserts because the physical layouts of the records have to be modified as records are put in if the new keys are not in sequential order.

    0 讨论(0)
  • 2020-11-29 14:52

    Clustered Index

    1. There can be only one clustered index for a table.
    2. Usually made on the primary key.
    3. The leaf nodes of a clustered index contain the data pages.

    Non-Clustered Index

    1. There can be only 249 non-clustered indexes for a table(till sql version 2005 later versions support upto 999 non-clustered indexes).
    2. Usually made on the any key.
    3. The leaf node of a nonclustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.
    0 讨论(0)
  • 2020-11-29 14:53

    Clustered indexes physically order the data on the disk. This means no extra data is needed for the index, but there can be only one clustered index (obviously). Accessing data using a clustered index is fastest.

    All other indexes must be non-clustered. A non-clustered index has a duplicate of the data from the indexed columns kept ordered together with pointers to the actual data rows (pointers to the clustered index if there is one). This means that accessing data through a non-clustered index has to go through an extra layer of indirection. However if you select only the data that's available in the indexed columns you can get the data back directly from the duplicated index data (that's why it's a good idea to SELECT only the columns that you need and not use *)

    0 讨论(0)
  • 2020-11-29 14:53

    You might have gone through theory part from the above posts:

    -The clustered Index as we can see points directly to record i.e. its direct so it takes less time for a search. Additionally it will not take any extra memory/space to store the index

    -While, in non-clustered Index, it indirectly points to the clustered Index then it will access the actual record, due to its indirect nature it will take some what more time to access.Also it needs its own memory/space to store the index

    0 讨论(0)
  • 2020-11-29 14:55

    // Copied from MSDN, the second point of non-clustered index is not clearly mentioned in the other answers.

    Clustered

    • Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be stored in only one order.
    • The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.

    Nonclustered

    • Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and
      each key value entry has a pointer to the data row that contains the key value.
    • The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.
    0 讨论(0)
提交回复
热议问题