码迷,mamicode.com
首页 > 数据库 > 详细

[SQL Basics] Indexes

时间:2014-08-25 22:30:34      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:os   io   for   ar   sp   new   on   ad   ef   

An index is used to speed up searching in the database.

By default, when you create this table, your data will be stored on disk and sorted by the "Id" primary key column. This default sort is called the "Clustered Index". 

  • Affects the physical order of data so there can only one clustered index.

With a clustered index the rows are stored physically on the disk in the same order as the index. There can therefore be only one clustered index.

But if you search by other non-primary key columns most of the time on this table, then you might want to consider changing the clustered index to this column instead.

There a few things to keep in mind when changing the default clustered index in a table:

  1. Lookups from non-clustered indexes must look up the query pointer in the clustered index to get the pointer to the actual data records instead of going directly to the data on disk (usually this performance hit is negligble).
  2. Inserts will be slower because the insert must be added in the exact right place in the clustered index. (NOTE: This does not re-order the data pages. It just inserts the record in the correct order in the page that it corresponds to. Data pages are stored as doubly-linked lists so each page is pointed to by the previous and next. Therefore, it is not important to reorder the pages, just their pointers and that is only in the case where the newly inserted row causes a new data page to be created.)

Non-clustered indexes are not copies of the table but a sorting of the columns you specify that "point" back to the data pages in the clustered index. With a non clustered index there is a second list that has pointers to the physical rows. You can have many non clustered indexes, although each new index will increase the time it takes to write new records.

It is generally faster to read from a clustered index if you want to get back all the columns. You do not have to go first to the index and then to the table.

Writing to a table with a clustered index can be slower, if there is a need to rearrange the data.

小总:Clustered index意思是在选取某个column A,以它排序来存储table里的所有records,所以当你以A为选择条件来做query的时候,因为physically records locate in the same order as the index,通过clustered index可以很快找到符合条件的records。

Non-clustered index意思是比如还是column A,index会存储A的值以及a pointer to the in the table where that value is actually stored.而clusterd index会在leaf node里存储整条record。所以clustered index会更快。

[SQL Basics] Indexes

标签:os   io   for   ar   sp   new   on   ad   ef   

原文地址:http://www.cnblogs.com/chayu3/p/3935883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!