Sunday, January 18, 2015

Indexs in SQL SERVER

INDEXES

Before discussing Indexes understand the below scenarios where we used to use daily operations.

Scenario1

create table employee1
(
eid int primary key,
ename varchar(20)
)

we created a table with eid as primary key and ename columns.

insert into employee1 values(100,'srinivas')
--successfully inserted a record

select * from employee1

insert into employee1 values(80,'santosh')
--record inserted, now

select * from employee1 
--will return as shown below

Why the records came in Ascending order ?

see the below two inserts 

insert into employee1 values(105,'Ravi')

insert into employee1 values(76,'Ravi')

Now

select * from employee1 
--will return as shown below


All the records are displaying in ascending order no all the records saving in ascending order in database.

Did you observe this behavior anytime ?

Scenario2:

create table employee2
(
eid int,
ename varchar(20)
)

I created the same table with out primary key on eid

insert into employee2 values(100,'srinivas'),(80,'santosh'),(105,'Ravi'),(76,'Ravi')

and inserted the same records now see the how the data will be stored on the table


is the data displaying in ascending order ?,the data is coming as we stored.

Did any time observed these two behaviours what is the reason?

Now understand the concept of Index first.

Indexes will be used for faster retrieval of data.In SQL Server we have two types of indexes are there as follows

1.Clustered Index (or) Unique Clustered Index:

1.We can have only one clustered  index.
2.When ever you create a primary key, clustered index automatically  you will get on that collumn.

with primary key ---->clustered index
with clustered index ---->we won't get primary key

3.If you have a clustered index/primary key  physical order of records in table will change asc/desc


2. non clustered index (or)  non unique clustered index:


1.we can have 249 non clustred  index.
2.when ever you create a unique key non clustered index automatically u will get  on that column

with unique ---->will get non clustered index
with non clustered index ---->we won't get unique key

3.non clustered index will not change the physical order but display the records in asc r desc order.
--------------------------------------------------------------------------------------------------------------------------
1.Creating an index:

create [unique][clustered/non clustered]
index <index_name> on 
<table_name>(column_name [desc])

2.Droping  an index:

drop index <table_name>.<index_name>

creating a clustered index:

create table employee3
(
eid int,
ename varchar(20),
sal int
)

insert into employee3 values(105,'ramesh',9000),(103,'akhil',8000),(101,'ram',9700),(102,'rahul',7000),(104,'ajay',8500)

select * from employee3
you will get the data as follows



Now I am creating a clustered index on eid as follows

create clustered index myindex1 on employee3(eid)

Now 
select * from employee3 will gets the data as follows


all the data is came in ascending order based on eid because we have a clustered index on eid.It changes the physical structure of a original data.

Now I want to drop the index

drop index employee3.myindex1
and now

select * from employee3


we got the same data even after droppping the index on eid because it changed the physical structure of the records. 

Now we are creating a clustered index on sal

create clustered index myindex1 on employee3(sal)

select * from employee3

you will get the data as follows with sal ascending order.



Now we already having an index on sal and trying to create another index on eid as follows

create clustered index myindex2 on employee3(eid)

you will get the error as follows



as "Msg 1902, Level 16, State 3, Line 2
Cannot create more than one clustered index on table 'employee3'. Drop the existing clustered index 'myindex1' before creating another."

So you can't have more than one clustered index on a table if you want to keep more than one index use Non-clustered index.

Creating a Non-clustered index:
create table employee4
(
eid int,
ename varchar(20),
sal int
)

insert into employee4 values(105,'ramesh',9000),(103,'akhil',8000),(101,'ram',9700),
(102,'rahul',7000),(104,'ajay',8500)


select * from employee4 

you will get the data as follows


On employee4 table I am creating a non clustered index on eid as follows

create nonclustered index myindex2 on employee4(eid) include(ename,sal)

select * from employee4



Now you got the data in ascending order based on eid column because we have non clustered index on eid but it didnt change the physical order of records.

drop index employee4.myindex2

select * from employee4

we got the data in same order of how we insert.

Now I am creating non clustered index on all the three columns as follws

create nonclustered index myindex2 on employee4(eid) include(ename,sal)

create nonclustered index myindex3 on employee4(ename) include(eid,sal)

create nonclustered index myindex4 on employee4(sal) include(eid,ename)

select * from employee4


It displayed the records in ascending order based on sal.

Note:

****If we have number of non clustered indexs on what index basis you will get the data?
based on last index.

Now observe the below

select * from employee4 where sal>8000
we got the records in sal ascending order

on the same table

select * from employee4  where eid>100

we got the records in eid ascending order

If you have number of non clustered indexes and your select query contains where clause, on the column if we have an non clustered index we will get the data based on that index.


will continue with materilised view/indexed view.

Thanks
Srinivas
9059361460