/
ch16.sql
249 lines (201 loc) · 6.86 KB
/
ch16.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* 16-1. Creating a Table Index */
USE AdventureWorks2012;
GO
If Not Exists (Select 1 from sys.objects where name = 'TerminationReason' and SCHEMA_NAME(schema_id) = 'HumanResources')
BEGIN
CREATE TABLE HumanResources.TerminationReason(
TerminationReasonID smallint IDENTITY(1,1) NOT NULL,
TerminationReason varchar(50) NOT NULL,
DepartmentID smallint NOT NULL,
CONSTRAINT FK_TerminationReason_DepartmentID FOREIGN KEY (DepartmentID)
REFERENCES HumanResources.Department(DepartmentID)
);
END
USE AdventureWorks2012;
GO
ALTER TABLE HumanResources.TerminationReason
ADD CONSTRAINT PK_TerminationReason PRIMARY KEY CLUSTERED (TerminationReasonID);
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NCI_TerminationReason_DepartmentID ON HumanResources.TerminationReason (DepartmentID);
/* 16-2. Enforcing Uniqueness on Non-key Columns */
--create Unique Index
USE AdventureWorks2012;
GO
CREATE UNIQUE NONCLUSTERED INDEX UNI_TerminationReason ON HumanResources.TerminationReason (TerminationReason);
--Insert records with success
USE AdventureWorks2012;
GO
INSERT INTO HumanResources.TerminationReason (DepartmentID, TerminationReason)
VALUES (1, 'Bad Engineering Skills')
,(2, 'Breaks Expensive Tools');
--Insert records - fail
USE AdventureWorks2012;
GO
INSERT INTO HumanResources.TerminationReason (DepartmentID, TerminationReason)
VALUES (2, 'Bad Engineering Skills');
--Select to confirm
USE AdventureWorks2012;
GO
SELECT TerminationReasonID, TerminationReason, DepartmentID
FROM HumanResources.TerminationReason;
/* 16-3. Creating an Index on Multiple Columns */
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason(TerminationReason, DepartmentID);
/* 16-4. Defining Index Column Sort Direction */
USE AdventureWorks2012;
GO
ALTER TABLE HumanResources.TerminationReason
ADD ViolationSeverityLevel smallint;
GO
CREATE NONCLUSTERED INDEX NI_TerminationReason_ViolationSeverityLevel
ON HumanResources.TerminationReason (ViolationSeverityLevel DESC);
/* 16-5. Viewing Index Metadata */
USE AdventureWorks2012;
GO
EXEC sp_helpindex 'HumanResources.Employee';
USE AdventureWorks2012;
GO
SELECT index_name = SUBSTRING(name, 1,30) ,
allow_row_locks,
allow_page_locks,
is_disabled,
fill_factor,
has_filter
FROM sys.indexes
WHERE object_id = OBJECT_ID('HumanResources.Employee');
/* 16-6. Disabling an Index */
USE AdventureWorks2012;
GO
ALTER INDEX UNI_TerminationReason
ON HumanResources.TerminationReason DISABLE
/* 16-7. Dropping Indexes */
USE AdventureWorks2012;
GO
DROP INDEX HumanResources.TerminationReason.UNI_TerminationReason;
/* 16-8. Changing an Existing Index */
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason(TerminationReason, DepartmentID)
WITH (DROP_EXISTING = ON);
GO
--add column to existing nonclustered index
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason(TerminationReason, ViolationSeverityLevel, DepartmentID DESC)
WITH (DROP_EXISTING = ON);
GO
/* 16-9. Sorting in Tempdb */
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_Address_PostalCode
ON Person.Address (PostalCode)
WITH (SORT_IN_TEMPDB = ON);
/* 16-10. Controlling Index Creation Parallelism */
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_Address_AddressLine1
ON Person.Address (AddressLine1)
WITH (MAXDOP = 4);
/* 16-11. User Table Access During Index Creation */
USE AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NCI_ProductVendor_MinOrderQty
ON Purchasing.ProductVendor(MinOrderQty)
WITH (ONLINE = ON); -- Online option is an Enterprise Edition feature
/* 16-12. Using an Index INCLUDE */
USE AdventureWorks2012;
GO
ALTER TABLE HumanResources.TerminationReason
ADD LegalDescription varchar(max);
Go
DROP INDEX HumanResources.TerminationReason.NI_TerminationReason_TerminationReason_DepartmentID;
Go
CREATE NONCLUSTERED INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason (TerminationReason, DepartmentID)
INCLUDE (LegalDescription);
/* 16-13. Using PADINDEX and FILLFACTOR */
USE AdventureWorks2012;
GO
DROP INDEX HumanResources.TerminationReason.NI_TerminationReason_TerminationReason_DepartmentID;
GO
CREATE NONCLUSTERED INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason (TerminationReason ASC, DepartmentID ASC)
WITH (PAD_INDEX=ON, FILLFACTOR=50);
GO
/* 16-14. Disabling Page and/or Row Index Locking */
USE AdventureWorks2012;
GO
-- Disable page locks. Table and row locks can still be used.
CREATE INDEX NI_EmployeePayHistory_Rate
ON HumanResources.EmployeePayHistory (Rate)
WITH (ALLOW_PAGE_LOCKS=OFF);
-- Disable page and row locks. Only table locks can be used.
ALTER INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason
SET (ALLOW_PAGE_LOCKS=OFF,ALLOW_ROW_LOCKS=OFF );
-- Allow page and row locks.
ALTER INDEX NI_TerminationReason_TerminationReason_DepartmentID
ON HumanResources.TerminationReason
SET (ALLOW_PAGE_LOCKS=ON,ALLOW_ROW_LOCKS=ON );
/* 16-15. Creating an Index on a Filegroup */
Use master;
GO
ALTER DATABASE AdventureWorks2012
ADD FILEGROUP FG2;
Use AdventureWorks2012;
GO
ALTER DATABASE AdventureWorks2012
ADD FILE
--Please ensure the Apress directory exists or change the path in the FILENAME statement
( NAME = AW2,FILENAME = 'c:\Apress\aw2.ndf',SIZE = 1MB )
TO FILEGROUP FG2;
Use AdventureWorks2012;
GO
CREATE INDEX NI_ProductPhoto_ThumnailPhotoFileName
ON Production.ProductPhoto (ThumbnailPhotoFileName)
ON [FG2];
/* 16-16. Implementing Index Partitioning */
Use AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NI_WebSiteHits_WebSitePage
ON Sales.WebSiteHits (WebSitePage)
ON [HitDateRangeScheme] (HitDate);
/* 16-17. Indexing a Subset of Rows */
Use AdventureWorks2012;
GO
SELECT SalesOrderID
FROM Sales.SalesOrderDetail
WHERE UnitPrice BETWEEN 150.00 AND 175.00;
Use AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NCI_UnitPrice_SalesOrderDetail
ON Sales.SalesOrderDetail(UnitPrice)
WHERE UnitPrice >= 150.00 AND UnitPrice <= 175.00;
Use AdventureWorks2012;
GO
SELECT SalesOrderDetailID
FROM Sales.SalesOrderDetail
WHERE ProductID IN (776, 777)
AND OrderQty > 10;
Use AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NCI_ProductID_SalesOrderDetail
ON Sales.SalesOrderDetail(ProductID,OrderQty)
WHERE ProductID IN (776, 777);
/* 16-18. Reducing Index Size */
Use AdventureWorks2012;
GO
CREATE NONCLUSTERED INDEX NCI_SalesOrderDetail_CarrierTrackingNumber
ON Sales.SalesOrderDetail (CarrierTrackingNumber)
WITH (DATA_COMPRESSION = PAGE);
Use AdventureWorks2012;
GO
ALTER INDEX NCI_SalesOrderDetail_CarrierTrackingNumber
ON Sales.SalesOrderDetail
REBUILD
WITH (DATA_COMPRESSION = ROW);