Module 4 Lab: Creating Indexes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Module 4 Lab: Creating Indexes

Exercise 1: Creating Indexes

Lab answer key

Creating a SQL Server Scripts project


You must perform the following steps to create a SQL Scripts project:

1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, and then click
SQL Server Management Studio.
2. In the Connect to Server dialog box, specify the values in the following table, and then click
Connect.
Property Value
Server type Database Engine
Server name MIAMI
Authentication Windows Authentication

3. On the File menu, point to New, and then click Project.


4. In the New Project dialog box, specify the values in the following table, and then click OK.
Property Value
Name AW_Indexes
Location D:\Labfiles\Starter
Create directory for Unselected
solution
Creating the Ix_Product_Supply_Chain index
You must perform the following steps to create the Ix_Product_Supply_Chain index:

1. On the Project menu, click New Query. When prompted, use Microsoft Windows®
authentication to connect to MIAMI.
2. If Solution Explorer is not visible, click Solution Explorer on the View menu.
3. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
CreateIndex.sql.
4. In the query window, type the following Transact-SQL code to create the
Ix_Product_Supply_Chain index.
USE AdventureWorks
CREATE UNIQUE NONCLUSTERED INDEX Ix_Product_Supply_Chain
ON Production.Product (ProductNumber, Color, ReorderPoint,
SafetyStockLevel)
INCLUDE (DaysToManufacture)
WITH (ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = OFF, FILLFACTOR = 90,
PAD_INDEX = ON)

5. Click the Execute button on the toolbar.


6. On the File menu, click Save All.
7. If Object Explorer is not visible, click Object Explorer on the View menu.
8. In Object Explorer, expand Databases, AdventureWorks, Tables, Production.Product, and
Indexes, and then verify that the Ix_Product_Supply_Chain index is listed.
9. Keep SQL Server Management Studio open. You will use it in the next exercise.
Exercise 2: Tuning Indexes

Lab answer key

Adding the workload query to the project


You must perform the following steps to add the workload query to the project:

1. On the Project menu, click Add Existing Item and add the Queries.sql file in the
D:\Labfiles\Starter folder. When prompted, connect to MIAMI.
2. Examine the queries in the file. You will use these queries to analyze the indexes in the
AdventureWorks database.

Analyzing index usage in the AdventureWorks database


You must perform the following steps to analyze the database indexes:

1. Click Start, point to All Programs, point to Microsoft SQL Server 2005, point to
Performance Tools, and then click Database Engine Tuning Advisor.
2. In the Connect to Server dialog box, specify the values in the following table, and then click
Connect.
Property Value
Server type Database Engine
Server name MIAMI
Authentication Windows Authentication

3. In the Session name box, type AW_TuneIndexes.


4. Under Workload, ensure that File is selected, and then click the Browse for a workload file
button.
5. Navigate to the D:\Labfiles\Starter\AW_Indexes folder.
6. Click Queries.sql, and then click Open.
7. Select AdventureWorks in the Database for workload analysis list.
8. In the Select databases and tables to tune section, select the AdventureWorks database.
9. Click the drop-down arrow in the Selected Tables column for the AdventureWorks
database.
10. Clear the check box in the Name column header to clear all tables, and then individually
select the Product and SalesOrderDetail tables.
11. Click the drop-down arrow in the Selected Tables column for the AdventureWorks
database again to hide the list of tables.
12. Click the Tuning Options tab.
13. Clear the Limit tuning time check box.
14. In the Physical Design Structures (PDS) to use in database section, select Indexes.
15. In the Partitioning strategy to employ section, select No partitioning.
16. In the Physical Design Structures (PDS) to keep in database section, select Keep all
existing PDS.
17. On the Actions menu, click Start Analysis, and then wait for the analysis to be completed.
18. Review the recommendations to see whether any new indexes have been recommended.
19. For index recommendations, scroll to the right until you can see the Definition column.
20. Click the index definition for any new index recommendations, and then review the Transact-
SQL.
21. Click the Copy to Clipboard button.
22. Click Close to close the SQL Script Preview window.
23. Switch back to SQL Server Management Studio.
24. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
25. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
IndexRecommendations.sql.
26. Paste the index modification recommendation into IndexRecommendations.sql, and then do
the same for all other index recommendations.
27. On the File menu, click Save All.
28. In the Database Engine Tuning Advisor, click File, and then click Export Session Results.
29. Navigate to the D:\Labfiles\Starter folder.
30. Type the file name ProductionAnalysis.xml, and then click Save. Click OK in the
confirmation message box.
31. Close the Database Engine Tuning Advisor.
32. Keep SQL Server Management Studio open. You will use it in the next exercise.
Exercise 3: Creating XML Indexes

Lab answer key

Creating a new query file


You must perform the following procedure to create a new query file:

1. On the Project menu, click New Query. When prompted, use Windows authentication to
connect to MIAMI.
2. In Solution Explorer, right-click SQLQuery1.sql, and then click Rename. Rename the file
CreateXMLIndexes.sql.

Creating XML indexes


You must perform the following procedure to create XML indexes:

1. In the query window, type the following code to create the primary XML index on the
Diagram column of the Production.Illustration table.

USE AdventureWorks
SET ARITHABORT ON

CREATE PRIMARY XML INDEX PXML_Illustration_Diagram


ON Production.Illustration (Diagram)
WITH (FILLFACTOR = 60, PAD_INDEX = ON)

2. In the query window, type the following code to create the secondary path XML index on the
Diagram column of the Production.Illustration table.

CREATE XML INDEX XMLPATH_Illustration_Diagram


ON Production.Illustration (Diagram)
USING XML INDEX PXML_Illustration_Diagram
FOR PATH
WITH (FILLFACTOR = 70, PAD_INDEX = OFF)

3. Click the Execute button on the toolbar to execute the query.


4. On the File menu, click Save All.
5. In Object Explorer, expand Databases, AdventureWorks, Tables, Production.Illustration,
and Indexes, and then verify that the new XML indexes are listed.
6. Close SQL Server Management Studio.

You might also like