forked from cihanozhan/advanced-tsql-programming
/
06_WorkingWithIndexes.sql
140 lines (63 loc) · 2.12 KB
/
06_WorkingWithIndexes.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
-- Ýndeksler Hakkýnda Bilgi Edinmek
EXEC sp_helpindex 'Production.Product';
SELECT * FROM sys.indexes;
-- Ýndex Oluþturmak
CREATE CLUSTERED INDEX CL_PersonelID
ON Personeller(PersonelID);
CREATE INDEX NC_PersonelID
ON Personeller(PersonelID);
CREATE INDEX NC_PersonelID
ON Personeller(PersonelID ASC);
-- Unique Ýndeks Oluþturmak
CREATE UNIQUE NONCLUSTERED INDEX UI_Email
ON dbo.Personeller(Email)
ON [PRIMARY];
-- Kapsam(Covering) Ýndeks Oluþturmak
SELECT Name, ProductNumber, ListPrice FROM Production.Product;
CREATE INDEX CV_Product
ON Production.Product(Name, ProductNumber, ListPrice);
-- Eklenti Sütunlu Ýndeks Oluþturmak
CREATE INDEX CV_SalesDetail
ON Sales.SalesOrderDetail(SalesOrderID)
INCLUDE(OrderQty, ProductID, UnitPrice)
-- Filtreli Ýndeks Oluþturmak
SELECT ProductID, Name, Color FROM Production.Product
WHERE Color IS NOT NULL
CREATE INDEX FI_Product
ON Production.Product(ProductID, Name)
WHERE Color IS NOT NULL;
-- Ýndeks Yönetimi
-- REBUILD : Ýndeksleri Yeniden Derlemek
ALTER INDEX ALL ON Production.Product
REBUILD WITH(FILLFACTOR = 90)
REBUILD WITH(FILLFACTOR = 90, SORT_IN_TEMPDB = ON)
-- REORGANIZE : Ýndeksleri Yeniden Düzenlemek
ALTER INDEX UI_Email
ON dbo.Personeller
REORGANIZE WITH (LOB_COMPACTION = ON);
-- Ýndeksleri Kapatmak
ALTER INDEX CL_PersonelID
ON Personeller
DISABLE
SELECT * FROM Personeller;
ALTER INDEX FI_Product
ON Production.Product
DISABLE
SELECT * FROM Production.Product;
ALTER INDEX CL_PersonelID
ON dbo.Personeller
REBUILD
ALTER INDEX FI_Product
ON Production.Product
REBUILD
-- Ýndeks Seçeneklerini Deðiþtirmek
SELECT Object_ID, Name, Index_ID, Type, type_desc, Allow_Row_Locks FROM sys.indexes WHERE Name = 'FI_Product';
ALTER INDEX FI_Product
ON Production.Product
SET(ALLOW_ROW_LOCKS = ON);
-- Ýstatistikler
-- Ýstatistik Oluþturmak
CREATE STATISTICS Statistic_ProductID
ON Production.Product(ProductID);
-- Ýstatistikleri Silmek
DROP STATISTICS Production.Product.Statistic_ProductID;