tags:

views:

70

answers:

4

Could somebody please explain how indexing works in SQL Server and what is a clustered and non clustered index?

When we create an index on some column how does it increase the performance and how are the values of column stored to increase the performance?

+2  A: 

You can write a book about it.

You should get familiar with this

Table and Index structures

Understanding and design indexes

And then if You still don't catch something ask here for some details.

Vash
+1. This is a grodss case of "not willing to even try looking at the documentation". Which, interesting enough, is a base approach for any non-joke developer.
TomTom
I can write 3 books, it is more easy than to read 1
vgv8
+1  A: 

Check out:

for some good intro material

marc_s
i am confused that if we create index on some columns how a query search directly in that column instead of searching through the table.such as if i want to search a rice between two values and if i made that price column clustered index then how my search query will directly hit to that column to fatch fast result.
NoviceToDotNet
You should avoid using non unique column for clustered index. I thing that normal index will do in this case. He will do a range scan and then retrieve the entities. Use the execution plan to see, SQl Server Management Studio provide some fancy graphical one.
Vash
+1  A: 

In short:

An index is faster because it is a subset of the table that is cached in memory.

Also, if all data requsted is in the index the server does not even have to query disk.

Clustered vs. unclustered:

A table can only have one clustered index as it means that the data on disk is ordered in the same order as the index. If you insert new data the table will be adjusted to keep this order.

Clustered index is faster often to search, especially if you need more data from each row but gets a performance hit on inserts.

Usually you have clustered index on the ID column.

Unclustered indexes you can have many of and they are for speeding up queries.

By combining the fields that most often are queried together and order the fields in the index so that the most varied field is first you can get big performance boosts, but read the links the other supplied, you can always öearn more about indexing, even if you build you own database =)

David Mårtensson
+1  A: 

Let me try to explain the concept of an index. This explanation is not SQL Server specific, and I'm perhaps over simplifying things a bit. However, it may help you get an idea why an index is useful.

Assume you store simple records consisting of an ID and a name. Data are stored in rows where the first column (#) is a record number or an address enabling us to point to a particular row.

#  Id  Name
------------
0  1   Lorem
1  2   Ipsum
2  3   Dolor
3  5   Sit
4  6   Amet

Assume that you want to find a the row having a specific name, say "Sit". The only way you can find it is by scanning the entire table looking for that particular name. Doing that on a big table is time consuming.

To solve that problem you can create an index on the Name column. An index is like a table:

Name   #
--------
Amet   4
Dolor  2
Ipsum  1
Lorem  0
Sit    3

Note that the names are sorted which makes it easier to search for a particular name. Finding a name no longer requires a complete table scan but can instead be speeded up by searching the index. When the name is found the corresponding record number can be looked up and the associated record can be retrieved. Often the index will use a balanced tree which makes it quite efficient to search.

To sum up, search is faster if you have an index but you will have to maintain an additional data structure that consumes spaces and requires updating when the underlying table is modified.

A clustered index is a special index that defines how the table is stored. Having an clustered index on the Id column will ensure that the rows in the table are ordered by ID. You don't need an additional index data structure to maintain this index. The index is already defined by how the table is stored.

Martin Liversage
ok for every index a seperate data table is created ?
NoviceToDotNet
This hash # record number is unclear to me, is it automatically created and what if i delete a record. And also when i create index on some column it separately create a table with the # column reference, and the column where i create cluster will automatically be sorted?
NoviceToDotNet
The record number (#) or address is a mechanism used by the database to refer to a specific record. Exactly how it is done is not something I try to (or can) explain. I'm simply trying to give you a simplified high level idea of what an index is. If you need to get into more specific details you should definitely look at some of the other answers provided.
Martin Liversage