Chapter10 Examples
Chapter10 Examples
1
USE sample;
CREATE INDEX i_empno ON employee (emp_no);
Example 10.2
USE sample;
CREATE UNIQUE INDEX i_empno_prno
ON works_on (emp_no, project_no)
WITH FILLFACTOR= 80;
Example 10.3
DECLARE @db_id INT;
DECLARE @tab_id INT;
DECLARE @ind_id INT;
SET @db_id = DB_ID('sample');
SET @tab_id = OBJECT_ID('employee');
SELECT avg_fragmentation_in_percent, avg_page_space_used_in_percent
FROM sys.dm_db_index_physical_stats
(@db_id, @tab_id, NULL, NULL, NULL)
Example 10.4
USE AdventureWorks;
GO
SELECT PersonID,
StoreID,
TerritoryID,
AccountNumber,
ModifiedDate
INTO sample.dbo.My_Customers
FROM AdventureWorks.Sales.Customer;
GO
USE sample;
INSERT INTO My_Customers
SELECT
PersonID,
StoreID,
TerritoryID,
AccountNumber,
ModifiedDate
FROM AdventureWorks.Sales.Customer
GO 80
Example 10.5
USE sample;
GO
CREATE INDEX I_MyCustomers
ON My_Customers
(PersonID, StoreID, TerritoryID, AccountNumber,ModifiedDate)
WITH (RESUMABLE = ON, ONLINE = ON);
USE sample;
GO
ALTER INDEX I_MyCustomers ON My_Customers
PAUSE;
GO
Example 10.6
SELECT name, percent_complete, state
FROM sys.index_resumable_operations;
Example 10.7
ALTER INDEX I_MyCustomers ON My_Customers RESUME;
Example 10.8
USE sample;
DROP INDEX i_empno ON employee;
Example 10.9
USE sample;
CREATE INDEX i_works ON works_on(emp_no, enter_date);
SELECT emp_no, project_no, enter_date
FROM works_on
WHERE emp_no = 29346 AND enter_date='1.4.2016';
Example 10.10
USE sample;
SELECT emp_lname, emp_fname
FROM employee, works_on
WHERE employee.emp_no = works_on.emp_no
AND enter_date = '10.15.2017';
Example 10.11
USE AdventureWorks;
GO
DROP INDEX Person.Address.IX_Address_StateProvinceID;
GO
CREATE INDEX i_address_zip
ON Person.Address (PostalCode)
INCLUDE (City, StateProvinceID);
GO
SELECT city, stateprovinceID
FROM Person.Address
WHERE PostalCode = 84407;
Example 10.12
-- 1
USE AdventureWorks;
GO
sp_helpindex [Sales.Store]
GO
--2
SELECT Name, BusinessEntityID, ModifiedDate FROM Sales.Store
WHERE (Name='Sharp Bikes' and BusinessEntityID <> ''
AND ModifiedDate > '2004-10-01');
-- 3
SELECT equality_columns, inequality_columns, statement
FROM sys.dm_db_missing_index_details