Вы находитесь на странице: 1из 5

http://office.microsoft.com/en-us/templates/CT010152732.

aspx

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Fsqlp%2Frbafysqltrig.htm
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.

32 down vote accepted

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% (ie 95% of the records are unique values).

answered Sep 18 '08 at 11:20

edited Sep 18 '08 at 11:25 link|improve this answer

Martynnw 1,313614 There are also storage considerations. When inserting rows into a table with no clustered index, the rows are stored back to back on the page and updating a row 1 may result in the row being moved to the end of table, leaving empty space and fragmenting the table and indexes. Jeremiah Peschka Sep 18 '08 at 15:44 What does it mean that an index is "faster to read"? How many more x per second can you do? What is x? Stephanie Page Aug 9 '10 at 22:23 you don't have to care what is x. All you need to know is that for an app with millions of users, x will be significant Pacerier Jul 23 at 13:42

feedback

Clustered indexes are stored physically on the table. This means they are the fastest and you can only have one clustered index per table. Non-clustered indexes are stored separately, and you can have as many as you want. The best option is to set your clustered index on the most used unique column, usually the PK. You should always have a well selected clustered index in your tables, unless a very compelling reason--can't think of a single one, but hey, it may be out there--for not doing so comes up.

up vote 7 down vote edited Jul 27 at 10:27 link|improve this answer

91714

answered Sep 18 '08 at 11:19

santiiiii 1,470411 can you elaborate more on "we should always have a clustered index in our tables" ? without elaboration that statement is simply wrong because of the word always Pacerier Jul 23 at 13:43 You're right Pacerier, one shouldn't use absolute statements lightly. Though I don't know of a single case when you shouldn't have a well selected clustered index, such case might exist so I've changed my answer to a more generic version. santiiiii Jul 27 at 10:24 feedback
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.

up All other indexes must be non-clustered. A non-clustered index has a duplicate of the data vote 6 down vote 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

91748

that you need and not use *)

answered Sep 18 '08 at 11:24

link|improve this answer

rslite 5,95221629 feedback


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.

answered Sep 18 '08 at 11:20


91728

up vote link|improve this answer

4 down vote

Biri 3,59811228 feedback


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.

91740

up vote
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.

3 down vote

link|improve this answer answered Sep 18 '08 at 11:22

Giovanni Galbo 6,00752247 feedback


A clustered index actually describes the order in which records are physically stored on the disk, hence the reason you can only have one. A Non-Clustered Index defines a logical order that does not match the physical order on disk.

answered Sep 18 '08 at 11:19


91722

up vote 2

down vote link|improve this answer

Josh 10.8k1342 feedback


You can only have one clustered index per table. But there are plenty of other differences...

answered Sep 18 '08 at 11:17


91707

up vote 0 link|improve this answer

down vote

tjrobinson 1,8821935 Why the downvote? Is this incorrect? tjrobinson Nov 18 '09 at 11:28

5 poor attempt at answering the question? flesh Oct 26 '10 at 14:22 feedback
A clustered index alters the way that the rows are stored. When you create a clustered index on a column (or a number of columns), SQL server sorts the tables rows by that column(s). It is like a dictionary, where all words are sorted in alphabetical order in the entire book. A non-clustered index, on the other hand, does not alter the way the rows are stored in the table. It creates a completely different object within the table that contains the column(s) selected for indexing and a pointer back to the tables rows containing the data. It is like an index in the last pages of a book, where keywords are sorted and contain the page number to the material of the book for faster reference.

up vote 0 down vote

7766076

answered Oct 14 at 10:04

link|improve this answer

Khan 5471112 feedback

Your Answer

Вам также может понравиться