What are the differences between a clustered and a

2019-01-07 01:50发布

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

12条回答
可以哭但决不认输i
2楼-- · 2019-01-07 01:56

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.
查看更多
放荡不羁爱自由
3楼-- · 2019-01-07 01:57

// 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.
查看更多
forever°为你锁心
4楼-- · 2019-01-07 01:58

Clustered Index

  • Only one per table
  • Faster to read than non clustered as data is physically stored in index order

Non Clustered Index

  • Can be used many times per table
  • Quicker for insert and update operations than a clustered index

Both types of index will improve performance when select data with fields that use the index but will slow down update and insert operations.

Because of the slower insert and update clustered indexes should be set on a field that is normally incremental ie Id or Timestamp.

SQL Server will normally only use an index if its selectivity is above 95%.

查看更多
戒情不戒烟
5楼-- · 2019-01-07 02:04

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 *)

查看更多
走好不送
6楼-- · 2019-01-07 02:13

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 table don't have a clustered index) data files are unordered and it uses Heap data structure as the data structure.

查看更多
Luminary・发光体
7楼-- · 2019-01-07 02:14

Clustered basically means that the data is in that phisical order in the table. This is why you can have only one per table.

Unclustered means it's "only" a logical order.

查看更多
登录 后发表回答