clustered and nonclustered indexes

SQL Server has two basics kinds of indexes.  They are clustered and nonclustered indexes.  There are some fundamental differences to the two which are key to understanding before you can master index tuning.

First the things that they have in common.

Both clustered and nonclustered indexes can be made up of more than one column.  The columns are put in the index in the order you specify them in the CREATE INDEX statement (or the order they are shown in the UI).  They are also sorted in this order as well.  Indexes are first sorted on the first column in the index, then any duplicates of the first column and sorted by the second column, etc.  You can have up to 16 columns specified as indexed columns.

Neither clustered or nonclustered indexes will guarantee the sort order of the data when it is being returned to you.  If the order of the data matters to you, you should always sort the data with the ORDER BY clause in your select statement.

Both clustered indexes, and nonclustered indexes take up additional disk space.  The amount of space that they require will depend on the columns in the index, and the number of rows in the table.  The clustered index will also grow as you add columns to the table (keep reading, it’ll make sense later on).

Adding indexes (both clustered and nonclusterd) will increase the amount of time that your INSERT, UPDATE and DELETE statement take, as the data has to be updated in the table as well as in each index.  If you have filtered indexes in SQL Server 2008 and the records you are updating are not included in all your indexes, SQL Server should only have to update the values in the indexes which the records are stored within.

Columns of the TEXT, NTEXT and IMAGE data types can not be indexed using normal indexes.  Columns of these data types can only be indexed with Full Text indexes.

If you wish to rebuild your indexes online (without locking the table) and have Enterprise edition do not index TEXT, NTEXT, IMAGE, VARCHAR(MAX), NVARCHAR(MAX), VARBINARY(MAX) data types as including columns with these data types will require that you rebuild the index offline.

The total size of the key columns can not exceed 900 bytes.  Don’t forget that uni-code characters take up two bytes per character which will reduce the number of characters which your index can hold.

Clustered Indexes

SQL Server only supports a single clustered index per each database table.

Clustered indexes do not support included columns.  This is because clustered indexes contain all the columns which aren’t in the index as included columns already.  There is no way to override this.  Columns with the data type of TEXT, NTEXT and IMAGE are not included within the clustered index.  As with the normal table these values are stored out of bounds and only the pointer to the physical location on disk is stored within the index.

Clustered indexes must be placed within the same file group as the physical table which they are created against.

***Update***

Let me point out that SQL Server won’t throw an error if you try and put the clustered index on another file group.  SQL Server will let you do this, however the table will be moved to this file group as well.  The Clustered index and the physical table must be in the same file group.

*************

Nonclustered Indexes

Nonclustered indexes (also called “indexes”) are the normal indexes that are created on your tables.  SQL Server supports 999 nonclustered indexes per table.

Each nonclustered index can contain upto 1023 included columns.  Columns of the TEXT, NTEXT and IMAGE data types can not be included either.

clustered and nonclustered indexes

时间: 2024-10-07 12:40:29

clustered and nonclustered indexes的相关文章

Clustered and Secondary Indexes

Clustered and Secondary Indexes secondary index A type of InnoDB index that represents a subset of table columns. An InnoDB table can have zero, one, or many secondary indexes. (Contrast with the clustered index, which is required for each InnoDB tab

What is the difference between a Clustered and Non Clustered Index?

A clustered index determines the order in which the rows of a table are stored on disk. If a table has a clustered index, then the rows of that table will be stored on disk in the same exact order as the clustered index. An example will help clarify

Index Defragmentation

如果使用sys.dm_db_index_physical_stats 检查Index的 avg_fragmentation_in_percent 是一个非常大的数值,那么就要检查fragmentation是否影响到性能,如果导致性能下降,那么就需要 remove fragmentation. Fragmentation alone is not a sufficient reason to reorganize or rebuild an index. The main effect of fr

Index Reorganize 和 Rebuild 的区别

对Index 进行 Reorganize 和 Rebuild 是有区别的. 1,语义区别 Rebuild 是重新创建,将Index之前占用的空间释放,重新申请空间来创建index.Rebuilding an index means that a whole new set of pages is allocated for it. Reorganize 是重新组织,作用于 index leaf level pages.Reorganizing an index compacts the leaf

Programmer Competency Matrix

Note that the knowledge for each level is cumulative; being atlevel n implies that you also know everything from thelevels lower than n. Computer Science   2n (Level 0) n2 (Level 1) n (Level 2) log(n) (Level 3) Comments data structures        Doesn’t

索引 Reorganize 和 Rebuild 的区别

在SQL Server中,Index是BTree(balance tree)结构,每个Page之间都有双向指针链接在一起.Index是在table结构之外,独立存在的存储结构.Index能使查询性能带来飞跃的主要原因是:Index 结构更小,能够更快加载到内存:Index ey物理顺序和逻辑一致,数据的预读取能够提高数据的加载速度,SQL Server 每次读取操作都会将物理物理相邻的多个Page一起加载到内存. BTree结构决定 Index 的叶子节点,从左到右使依次增大,如图是Index的

Understanding how SQL Server executes a query

https://www.codeproject.com/Articles/630346/Understanding-how-SQL-Server-executes-a-query https://www.codeproject.com/Articles/732812/How-to-analyse-SQL-Server-performance This article will help you write better database code and will help you get st

sql是如何执行一个查询的!

引用自:http://rusanu.com/2013/08/01/understanding-how-sql-server-executes-a-query/ Understanding how SQL Server executes a query August 1st, 2013 If you are a developer writing applications that use SQL Server and you are wondering what exactly happens

sql server中index的REBUILD和REORGANIZE

参考文献: http://technet.microsoft.com/en-us/library/ms188388.aspx 使用alter index来rebuild和reorganize索引来清除碎片,rebuild能够完全清除碎片,但是reorganize却不能. --1.准备实验数据 select * into Employee from AdventureWorks2008R2.HumanResources.Employee; --2.查看使用空间:Employee 290 72 KB