tag:blogger.com,1999:blog-6986453694837151659.post4344970321209298414..comments2024-03-29T06:20:09.658-07:00Comments on Programming Help: What is an Index in SQL?Khalid Rafiquehttp://www.blogger.com/profile/16082059726366438013noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-6986453694837151659.post-8800342506482490232012-02-16T02:39:20.717-08:002012-02-16T02:39:20.717-08:00More: reference:
http://www.itbully.com/articles/s...More: reference:<br />http://www.itbully.com/articles/sql-indexing-and-performance-part-2-clustered-and-non-clustered<br /><br />http://stackoverflow.com/questions/5070529/difference-between-clustered-and-nonclustered-indexKhalid Rafiquehttps://www.blogger.com/profile/16082059726366438013noreply@blogger.comtag:blogger.com,1999:blog-6986453694837151659.post-60822046209903088282012-02-16T02:37:44.325-08:002012-02-16T02:37:44.325-08:00So, why do you need to index your tables?
Because...So, why do you need to index your tables?<br /><br />Because without an index SQL server has to scan entire tables to return requested data. It is like the index page in a book. You check for the keyword you want to read about in the index and you jump directly to the page were the content belongs, instead of scanning page by page for the material you want to read.<br /><br />Similarly a table index allows you to locate data without the need to scan the entire table. You create indexes on one or more columns in a table to help SQL server find the data quickly in a query. <br /><br />ndex Types<br /><br />There are two main index types; Clustered index and Non-Clustered index.<br /><br />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 table’s rows by that column(s). It is like a dictionary, where all words are sorted in alphabetical order in the entire book. Since it alters the physical storage of the table, only one clustered index can be created per table. In the above example the entire rows are sorted by computer_id since a clustered index on computer_id column has been created.<br /><br />CREATE CLUSTERED INDEX [IX_CLUSTERED_COMPUTER_ID] <br />ON [dbo].[nics] ([computer_id] ASC)<br /><br />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 table’s 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. A non-clustered index on the computer_id in the previous example would look like the table below:<br /><br />CREATE NONCLUSTERED INDEX [IX_NONCLUSTERED_COMPUTER_ID] <br />ON [dbo].[nics] ([computer_id] ASC)Khalid Rafiquehttps://www.blogger.com/profile/16082059726366438013noreply@blogger.com