Вы находитесь на странице: 1из 4

CREACION DE INDICES

Índice Clustered Índice NonClustered


Índices que define el orden en que los datos Índices que ayudan a mejorar el
son físicamente almacenados en una tabla: rendimiento de una consulta, pero se
1. Primary Key almacena en otra Hoja y se usan cuando
2. Personalizar su creación observas que hay sentencias, por ejemplo:
1. Where o Having
2. Group By o Order By
3. Select sum(), avrg(),etc.
Índice Compuesto (tipo 1 de nonclustered)
Índice que incluye múltiples columnas (Max 16). Usaremos el sgte ejemplo:
hacemos un select de la tabla Person.Person en la BD AdventureWorks
select P.FirstName, P.LastName from Person.Person P
where P.FirstName = 'ken' and P.LastName = 'Sánchez'

Para desaparecer el Index Scan creamos el siguiente índice compuesto:


create nonclustered index compuesto
on Person.Person (LastName, FirstName)
/*usar la columna más única primero , en este caso LastName porque se repite
menos en la tabla y la columna FirstName.
Índice Incluido (tipo 2 de nonclustered)
Índice que no es llave pero se incluye dentro de la creación, veamos el siguiente ejm:
Creamos la tabla dbo.Book con las columnas (PublisherID, Title, ReleaseDate) y
un indice nonclustered IX_Book_Publisher
SELECT PublisherID, Title, ReleaseDate
FROM dbo.Book
WHERE ReleaseDate > DATEADD(year,-1,SYSDATETIME())
ORDER BY PublisherID, ReleaseDate DESC;
GO
CREATE NONCLUSTERED INDEX IX_Book_Publisher
ON dbo.Book (PublisherID, ReleaseDate DESC);
GO

Para desaparecer el Key Lookup incluimos la columna title al índice


IX_Bokk_Publisher
CREATE NONCLUSTERED INDEX IX_Book_Publisher
ON dbo.Book (PublisherID, ReleaseDate DESC)
include (Title)
GO
Índice Filtrado (tipo 3 de nonclustered)
Se crea el índice con Where y se incluye solo ciertas filas. Normalmente son
usados para filtrar los NULL de una tabla al realizar una búsqueda.
set statistics io on
select P.ProductID, P.LocationID, P.Shelf
from Production.ProductInventory as P
where Shelf <> 'N/A'
order by P.Shelf desc
go

Como se puede observar al crear el indice filtrado se mejora el performance de


la consulta.
create nonclustered index filtrado
on Production.ProductInventory (Shelf desc)
where Shelf <> 'N/A'
Índice Único
----Se hace una consulta select de la tabla HumanResource.Employee de la BD
AdventureWork
select LoginID from HumanResources.Employee
where loginID = 'adventure-works\doris0'

----En la consulta select aparece el Index Scan por tal motivo hay
desaparecerlo creando un índice único a la columna LoginID porque sus valores
son únicos y no repetitivos
CREATE UNIQUE NONCLUSTERED
INDEX AK_Employee_LoginID ON
HumanResources.Employee
(LoginID)

Вам также может понравиться