diff --git a/9781430237419.jpg b/9781430237419.jpg new file mode 100644 index 0000000..c14eccf Binary files /dev/null and b/9781430237419.jpg differ diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..b4c59af --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,27 @@ +Freeware License, some rights reserved + +Copyright (c) 2012 Jason Strate and Ted Krueger + +Permission is hereby granted, free of charge, to anyone obtaining a copy +of this software and associated documentation files (the "Software"), +to work with the Software within the limits of freeware distribution and fair use. +This includes the rights to use, copy, and modify the Software for personal use. +Users are also allowed and encouraged to submit corrections and modifications +to the Software for the benefit of other users. + +It is not allowed to reuse, modify, or redistribute the Software for +commercial use in any way, or for a user’s educational materials such as books +or blog articles without prior permission from the copyright holder. + +The above copyright notice and this permission notice need to be included +in all copies or substantial portions of the software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + diff --git a/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-01.sql b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-01.sql new file mode 100644 index 0000000..b90b7f7 --- /dev/null +++ b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-01.sql @@ -0,0 +1,13 @@ +--Listing 1-1. CREATE INDEX Syntax +CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name + ON ( column [ ASC | DESC ] [ ,…n ] ) + [ INCLUDE ( column_name [ ,…n ] ) ] + [ WHERE ] + [ WITH ( [ ,…n ] ) ] + [ ON { partition_scheme_name ( column_name ) + | ?legroup_name + | default + } + ] + [ FILESTREAM_ON { ?lestream_?legroup_name | partition_scheme_name | "NULL" } ] +[ ; ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-02.sql b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-02.sql new file mode 100644 index 0000000..e5bd58d --- /dev/null +++ b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-02.sql @@ -0,0 +1,14 @@ +--Listing 1-2. Index Options +PAD_INDEX = { ON | OFF } +| FILLFACTOR = ?llfactor +| SORT_IN_TEMPDB = { ON | OFF } +| IGNORE_DUP_KEY = { ON | OFF } +| STATISTICS_NORECOMPUTE = { ON | OFF } +| DROP_EXISTING = { ON | OFF } +| ONLINE = { ON | OFF } +| ALLOW_ROW_LOCKS = { ON | OFF } +| ALLOW_PAGE_LOCKS = { ON | OFF } +| MAXDOP = max_degree_of_parallelism +| DATA_COMPRESSION = { NONE | ROW | PAGE} + [ ON PARTITIONS ( { | } + [ , …n ] ) ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-03.sql b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-03.sql new file mode 100644 index 0000000..5cdf8da --- /dev/null +++ b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-03.sql @@ -0,0 +1,19 @@ +--Listing 1-3. ALTER INDEX Syntax +ALTER INDEX { index_name | ALL } + ON + { REBUILD + [ [PARTITION = ALL] + [ WITH ( [ ,…n ] ) ] + | [ PARTITION = partition_number + [ WITH ( + [ ,…n ] ) + ] + ] + ] + | DISABLE + | REORGANIZE + [ PARTITION = partition_number ] + [ WITH ( LOB_COMPACTION = { ON | OFF } ) ] + | SET ( [ ,…n ] ) + } +[ ; ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-04.sql b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-04.sql new file mode 100644 index 0000000..9158b45 --- /dev/null +++ b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-04.sql @@ -0,0 +1,4 @@ +--Listing 1-4. DROP INDEX Syntax +DROP INDEX + index_name ON + [ WITH ( [ ,…n ] ) ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-05.sql b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-05.sql new file mode 100644 index 0000000..4c29ad1 --- /dev/null +++ b/Performance Indexing - Chapter 01/Chapter 01 - Listing 1-05.sql @@ -0,0 +1,10 @@ +--Listing 1-5. DROP INDEX Options +MAXDOP = max_degree_of_parallelism + | ONLINE = { ON | OFF } + | MOVE TO { partition_scheme_name ( column_name ) + | ?legroup_name + | "default" + } + [ FILESTREAM_ON { partition_scheme_name + | ?lestream_?legroup_name + | "default" } ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssms_suo b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssms_suo new file mode 100644 index 0000000..a429a39 Binary files /dev/null and b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssms_suo differ diff --git a/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssln b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssln new file mode 100644 index 0000000..bc9a81e --- /dev/null +++ b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# SQL Server Management Studio Solution File, Format Version 11.00 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Performance Indexing - Chapter 01", "Performance Indexing - Chapter 01.ssmssqlproj", "{648B8869-B6F9-4AC5-A36B-2E2059141C1A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {648B8869-B6F9-4AC5-A36B-2E2059141C1A}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssqlproj b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssqlproj new file mode 100644 index 0000000..a6c826a --- /dev/null +++ b/Performance Indexing - Chapter 01/Performance Indexing - Chapter 01.ssmssqlproj @@ -0,0 +1,58 @@ + + + + + + + 2013-06-13T11:30:26.6896776-04:00 + SQL + FL-WS-CON-JS01 + + Windows Authentication + + 15 + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + + + + + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 01 - Listing 1-01.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 01 - Listing 1-02.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 01 - Listing 1-03.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 01 - Listing 1-04.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 01 - Listing 1-05.sql + + + + + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-01.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-01.sql new file mode 100644 index 0000000..a565aac --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-01.sql @@ -0,0 +1,4 @@ +--Listing 2-1. DBCC EXTENTINFO Syntax +DBCC EXTENTINFO ( {database_name | database_id | 0} + , {table_name | table_object_id}, { index_name | index_id | -1} + , { partition_id | 0} \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-02.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-02.sql new file mode 100644 index 0000000..b2063ac --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-02.sql @@ -0,0 +1,16 @@ +--Listing 2-2. DBCC EXTENTINFO Example One +USE master +GO +CREATE DATABASE Chapter2Internals +GO +USE Chapter2Internals +GO +CREATE TABLE dbo.IndexInternalsOne + ( + RowID INT IDENTITY(1,1) + ,FillerData CHAR(8000) + ) +GO +INSERT INTO dbo.IndexInternalsOne DEFAULT VALUES +GO 4 +DBCC EXTENTINFO(0, IndexInternalsOne, -1) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-03.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-03.sql new file mode 100644 index 0000000..8115fb2 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-03.sql @@ -0,0 +1,7 @@ +--Listing 2-3. DBCC EXTENTINFO Example Two +INSERT INTO dbo.IndexInternalsOne +VALUES ('Demo'),('Demo'); +GO +INSERT INTO dbo.IndexInternalsOne DEFAULT VALUES +GO 4 +DBCC EXTENTINFO(0, IndexInternalsOne, -1) GOO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-04.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-04.sql new file mode 100644 index 0000000..2239663 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-04.sql @@ -0,0 +1,5 @@ +--Listing 2-4. DBCC EXTENTINFO Example Three +ALTER TABLE dbo.IndexInternalsOne REBUILD +GO +DBCC EXTENTINFO(0, IndexInternalsOne, -1) +GOO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-05.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-05.sql new file mode 100644 index 0000000..d034cf1 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-05.sql @@ -0,0 +1,13 @@ +--Listing 2-5. DBCC EXTENTINFO Example Four +CREATE TABLE dbo.IndexInternalsTwo + ( + RowID INT IDENTITY(1,1) + ,FillerData CHAR(8000) + ) +GO +INSERT INTO dbo.IndexInternalsTwo +VALUES ('Demo'),('Demo'),('Demo'),('Demo'),('Demo') + ,('Demo'),('Demo'),('Demo'),('Demo'); +GO +DBCC EXTENTINFO(0, IndexInternalsTwo, -1) +GOO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-06.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-06.sql new file mode 100644 index 0000000..19a82dd --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-06.sql @@ -0,0 +1,3 @@ +--Listing 2-6. DBCC IND Syntax +DBCC IND ( {'dbname' | dbid}, {'table_name' | table_object_id}, + {'index_name' | index_id | -1}) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-07.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-07.sql new file mode 100644 index 0000000..36431eb --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-07.sql @@ -0,0 +1,6 @@ +--Listing 2-7. DBCC IND Example One +USE Chapter2Internals; +GO +DBCC IND (0, 'IndexInternalsOne',-1); +GO +DBCC IND (0, 'IndexInternalsTwo',-1); \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-08.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-08.sql new file mode 100644 index 0000000..e56bc67 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-08.sql @@ -0,0 +1,13 @@ +--Listing 2-8. DBCC IND Example Two +USE Chapter2Internals +GO +CREATE TABLE dbo.IndexInternalsThree + ( + RowID INT IDENTITY(1,1) + ,FillerData CHAR(8000) + ,CONSTRAINT PK_IndexInternalsThree PRIMARY KEY CLUSTERED (RowID) + ) +GO +INSERT INTO dbo.IndexInternalsThree DEFAULT VALUES +GO 4 +DBCC IND (0, 'IndexInternalsThree',-1) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-09.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-09.sql new file mode 100644 index 0000000..1f4727a --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-09.sql @@ -0,0 +1,3 @@ +--Listing 2-9. DBCC PAGE Syntax +DBCC PAGE ( { database_name | database_id | 0}, ?le_number, page_number + [,print_option ={0|1|2|3} ]) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-10.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-10.sql new file mode 100644 index 0000000..47926f3 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-10.sql @@ -0,0 +1,15 @@ +--Listing 2-10. DBCC IND Query for DBCC PAGE Examples +USE [Chapter2Internals]; +GO +CREATE TABLE dbo.IndexInternalsFour + ( + RowID int IDENTITY(1,1) NOT NULL + ,FillerData varchar(2000) NULL + ,CONSTRAINT PK_IndexInternalsFour PRIMARY KEY CLUSTERED ([RowID] ASC) + ); +INSERT INTO dbo.IndexInternalsFour (FillerData) +VALUES (REPLICATE(1,2000)),(REPLICATE(2,2000)),(REPLICATE(3,2000)) + ,(REPLICATE(4,2000)),(REPLICATE(5,25)); +GO +DBCC IND(0, 'dbo.IndexInternalsFour', -1); +GOO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-11.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-11.sql new file mode 100644 index 0000000..5aab6c8 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-11.sql @@ -0,0 +1,3 @@ +--Listing 2-11. DBCC PAGE with Page Header Only Print Option +DBCC TRACEON(3604) +DBCC PAGE(0,1,279,0) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-12.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-12.sql new file mode 100644 index 0000000..34d6b80 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-12.sql @@ -0,0 +1,3 @@ +--Listing 2-12. DBCC PAGE with Hex Rows Print Option +DBCC TRACEON(3604) +DBCC PAGE(0,1,279,1) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-13.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-13.sql new file mode 100644 index 0000000..5c3b570 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-13.sql @@ -0,0 +1,3 @@ +--Listing 2-13. DBCC PAGE with Hex Data Print Option +DBCC TRACEON(3604) +DBCC PAGE(0,1,279,2) \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-14.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-14.sql new file mode 100644 index 0000000..40d53df --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-14.sql @@ -0,0 +1,4 @@ +--Listing 2-14. DBCC PAGE with Row Data Print Option +DBCC TRACEON(3604) +DBCC PAGE(0,1,296,3) -- Data page +DBCC PAGE(0,1,279,3) -- Index page \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-15.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-15.sql new file mode 100644 index 0000000..44e8f86 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-15.sql @@ -0,0 +1,15 @@ +--Listing 2-15. Forwarded Record Scenario +USE AdventureWorks2012 +GO +CREATE TABLE dbo.HeapForwardedRecords +( + RowId INT IDENTITY(1,1) + ,FillerData VARCHAR(2500) +); +INSERT INTO dbo.HeapForwardedRecords (FillerData) +SELECT TOP 24 REPLICATE('X',2000) +FROM sys.objects; +DECLARE @ObjectID INT = OBJECT_ID('dbo.HeapForwardedRecords'); +SELECT object_id, index_type_desc, page_count, record_count, forwarded_record_count +FROM sys.dm_db_index_physical_stats (DB_ID(), @ObjectID, NULL, NULL, 'DETAILED'); +GOO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-16.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-16.sql new file mode 100644 index 0000000..64b147f --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-16.sql @@ -0,0 +1,10 @@ +--Listing 2-16. Script to Cause Forwarded Records +USE AdventureWorks2012 +GO +UPDATE dbo.HeapForwardedRecords +SET FillerData = REPLICATE('X',2500) +WHERE RowId % 2 = 0; +DECLARE @ObjectID INT = OBJECT_ID('dbo.HeapForwardedRecords'); +SELECT object_id, index_type_desc, page_count, record_count, forwarded_record_count +FROM sys.dm_db_index_physical_stats (DB_ID(), @ObjectID, NULL, NULL, 'DETAILED'); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-17.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-17.sql new file mode 100644 index 0000000..fa3fca6 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-17.sql @@ -0,0 +1,16 @@ +--Listing 2-17. Page Split Scenario +USE AdventureWorks2012 +GO +CREATE TABLE dbo.ClusteredPageSplits +( + RowId INT IDENTITY(1,1) + ,FillerData VARCHAR(2500) + ,CONSTRAINT PK_ClusteredPageSplits PRIMARY KEY CLUSTERED (RowId) +); +INSERT INTO dbo.ClusteredPageSplits (FillerData) +SELECT TOP 24 REPLICATE('X',2000) +FROM sys.objects; +DECLARE @ObjectID INT = OBJECT_ID('dbo.ClusteredPageSplits'); +SELECT object_id, index_type_desc, index_level, page_count, record_count +FROM sys.dm_db_index_physical_stats (DB_ID(), @ObjectID, NULL, NULL, 'DETAILED'); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-18.sql b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-18.sql new file mode 100644 index 0000000..17c3b76 --- /dev/null +++ b/Performance Indexing - Chapter 02/Chapter 02 - Listing 2-18.sql @@ -0,0 +1,9 @@ +--Listing 2-18. Script to Cause Page Splits +USE AdventureWorks2012 +GO +UPDATE dbo.ClusteredPageSplits +SET FillerData = REPLICATE('X',2500) +WHERE RowId % 2 = 0; +DECLARE @ObjectID INT = OBJECT_ID('dbo.ClusteredPageSplits'); +SELECT object_id, index_type_desc, index_level, page_count, record_count +FROM sys.dm_db_index_physical_stats (DB_ID(), @ObjectID, NULL, NULL, 'DETAILED'); diff --git a/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssms_suo b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssms_suo new file mode 100644 index 0000000..6a66c80 Binary files /dev/null and b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssms_suo differ diff --git a/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssln b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssln new file mode 100644 index 0000000..201b6c7 --- /dev/null +++ b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# SQL Server Management Studio Solution File, Format Version 11.00 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Performance Indexing - Chapter 02", "Performance Indexing - Chapter 02.ssmssqlproj", "{2C4687EB-DFF8-4471-92C2-2F2CAE4D567D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2C4687EB-DFF8-4471-92C2-2F2CAE4D567D}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssqlproj b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssqlproj new file mode 100644 index 0000000..406a199 --- /dev/null +++ b/Performance Indexing - Chapter 02/Performance Indexing - Chapter 02.ssmssqlproj @@ -0,0 +1,136 @@ + + + + + + + 2013-06-13T11:28:59.6777008-04:00 + SQL + FL-WS-CON-JS01 + + Windows Authentication + + 15 + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + + + + + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-01.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-02.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-03.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-04.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-05.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-06.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-07.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-08.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-09.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-10.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-11.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-12.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-13.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-14.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-15.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-16.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-17.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 02 - Listing 2-18.sql + + + + + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-01.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-01.sql new file mode 100644 index 0000000..f0beff0 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-01.sql @@ -0,0 +1,3 @@ +--Listing 3-1. DBCC SHOW_STATISTICS Syntax +DBCC SHOW_STATISTICS ( table_or_indexed_view_name, target ) +[ WITH [ < options > ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-02.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-02.sql new file mode 100644 index 0000000..ee90314 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-02.sql @@ -0,0 +1,3 @@ +--Listing 3-2. DBCC SHOW_STATISTICS for Index on Sales.SalesOrderDetail Table +DBCC SHOW_STATISTICS ( 'Sales.SalesOrderDetail’ +, PK_SalesOrderDetail_SalesOrderID_SalesOrderDetailID ) \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-03.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-03.sql new file mode 100644 index 0000000..7cdca6f --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-03.sql @@ -0,0 +1,12 @@ +--Listing 3-3. Query to Check AVG_RANGE_ROWS Estimate +USE AdventureWorks2012 +GO +SELECT (COUNT(*)*1.)/COUNT(DISTINCT SalesOrderID) AS AverageRows +FROM Sales.SalesOrderDetail +WHERE SalesOrderID BETWEEN 43672 AND 43677; +SELECT (COUNT(*)*1.)/COUNT(DISTINCT SalesOrderID) AS AverageRows +FROM Sales.SalesOrderDetail +WHERE SalesOrderID BETWEEN 43675 AND 43677; +SELECT (COUNT(*)*1.)/COUNT(DISTINCT SalesOrderID) AS AverageRows +FROM Sales.SalesOrderDetail +WHERE SalesOrderID BETWEEN 43675 AND 43680; \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-04.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-04.sql new file mode 100644 index 0000000..13000cf --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-04.sql @@ -0,0 +1,2 @@ +--Listing 3-4. STATS_DATE Syntax +STATS_DATE ( object_id, stats_id ) \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-05.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-05.sql new file mode 100644 index 0000000..0957e51 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-05.sql @@ -0,0 +1,15 @@ +--Listing 3-5. Query for Header Columns in sys.dm_db_index_usage_stats +USE AdventureWorks2012 +GO +SELECT TOP 10 OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.database_id + ,ius.object_id + ,ius.index_id +FROM sys.indexes i + LEFT JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.index_id IS NULL +AND OBJECTPROPERTY(i.object_id, 'IsUserTable') = 1 \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-06.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-06.sql new file mode 100644 index 0000000..f941736 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-06.sql @@ -0,0 +1,9 @@ +--Listing 3-6. Index Seek Queries +USE AdventureWorks2012 +GO +SELECT * FROM Sales.SalesOrderDetail +WHERE SalesOrderID = 43659 +GO +SELECT * FROM Sales.SalesOrderDetail +WHERE SalesOrderID BETWEEN 43659 AND 44659 +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-07.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-07.sql new file mode 100644 index 0000000..f66405f --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-07.sql @@ -0,0 +1,15 @@ +--Listing 3-7. Query for index_seeks from sys.dm_db_index_usage_stats +USE AdventureWorks2012 +GO +SELECT TOP 10 + OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.user_seeks + ,ius.last_user_seek +FROM sys.indexes i + INNER JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.object_id = OBJECT_ID('Sales.SalesOrderDetail') + GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-08.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-08.sql new file mode 100644 index 0000000..3495452 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-08.sql @@ -0,0 +1,8 @@ +--Listing 3-8. Index Scan Queries +USE AdventureWorks2012 +GO +SELECT * FROM Sales.SalesOrderDetail +GO +SELECT * FROM Sales.SalesOrderDetail +WHERE CarrierTrackingNumber = '4911-403C-98' +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-09.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-09.sql new file mode 100644 index 0000000..b76fa69 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-09.sql @@ -0,0 +1,15 @@ +--Listing 3-9. Query for index_scans From sys.dm_db_index_usage_stats +USE AdventureWorks2012 +GO +SELECT TOP 10 + OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.user_scans + ,ius.last_user_scan +FROM sys.indexes i + INNER JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.object_id = OBJECT_ID('Sales.SalesOrderDetail') + GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-10.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-10.sql new file mode 100644 index 0000000..5f034a4 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-10.sql @@ -0,0 +1,7 @@ +--Listing 3-10. Index Lookup Query +USE AdventureWorks2012 +GO +SELECT ProductID, CarrierTrackingNumber +FROM Sales.SalesOrderDetail +WHERE ProductID = 778 +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-11.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-11.sql new file mode 100644 index 0000000..0c29e32 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-11.sql @@ -0,0 +1,13 @@ +--Listing 3-11. Query for index_lookups from sys.dm_db_index_usage_stats +SELECT TOP 10 + OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.user_seeks + ,ius.user_lookups + ,ius.last_user_lookup +FROM sys.indexes i + INNER JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.object_id = OBJECT_ID('Sales.SalesOrderDetail') \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-12.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-12.sql new file mode 100644 index 0000000..0291164 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-12.sql @@ -0,0 +1,18 @@ +--Listing 3-12. Index Lookup Query +USE AdventureWorks2012 +GO +INSERT INTO Sales.SalesOrderDetail +(SalesOrderID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, +UnitPriceDiscount, Modi?edDate) +SELECT SalesOrderID, CarrierTrackingNumber, OrderQty, ProductID, SpecialOfferID, UnitPrice, +UnitPriceDiscount, GETDATE() AS Modi?edDate +FROM Sales.SalesOrderDetail +WHERE SalesOrderDetailID = 1; +GO +UPDATE Sales.SalesOrderDetail +SET CarrierTrackingNumber = '999-99-9999' +WHERE Modi?edDate > DATEADD(d, -1, GETDATE()); +GO +DELETE FROM Sales.SalesOrderDetail +WHERE Modi?edDate > DATEADD(d, -1, GETDATE()); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-13.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-13.sql new file mode 100644 index 0000000..c51422d --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-13.sql @@ -0,0 +1,12 @@ +--Listing 3-13. Query for index_lookups from sys.dm_db_index_usage_stats +SELECT TOP 10 + OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.user_updates + ,ius.last_user_update +FROM sys.indexes i + INNER JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.object_id = OBJECT_ID('Sales.SalesOrderDetail') \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-14.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-14.sql new file mode 100644 index 0000000..51167a7 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-14.sql @@ -0,0 +1,6 @@ +--Listing 3-14. Update for Sales.SalesOrderDetail +USE AdventureWorks2012 +GO +UPDATE Sales.SalesOrderDetail +SET UnitPriceDiscount = 0.01 +WHERE UnitPriceDiscount = 0.00 \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-15.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-15.sql new file mode 100644 index 0000000..c998937 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-15.sql @@ -0,0 +1,17 @@ +--Listing 3-15. Query for System Columns in sys.dm_db_index_usage_stats +SELECT OBJECT_NAME(i.object_id) AS table_name + ,i.name AS index_name + ,ius.system_seeks + ,ius.system_scans + ,ius.system_lookups + ,ius.system_updates + ,ius.last_system_seek + ,ius.last_system_scan + ,ius.last_system_lookup + ,ius.last_system_update +FROM sys.indexes i + INNER JOIN sys.dm_db_index_usage_stats ius + ON i.object_id = ius.object_id + AND i.index_id = ius.index_id + AND ius.database_id = DB_ID() +WHERE ius.object_id = OBJECT_ID('Sales.SalesOrderDetail') \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-16.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-16.sql new file mode 100644 index 0000000..d9bea8b --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-16.sql @@ -0,0 +1,7 @@ +--Listing 3-16. Index Operational Stats Syntax +sys.dm_db_index_operational_stats ( + { database_id | NULL | 0 | DEFAULT } + , { object_id | NULL | 0 | DEFAULT } + , { index_id | 0 | NULL | -1 | DEFAULT } + , { partition_number | NULL | 0 | DEFAULT } +) \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-17.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-17.sql new file mode 100644 index 0000000..a569624 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-17.sql @@ -0,0 +1,37 @@ +--Listing 3-17. DML Activity Script +USE AdventureWorks +GO +IF OBJECT_ID('dbo.KungFu') IS NOT NULL + DROP TABLE dbo.KungFu +GO +CREATE TABLE dbo.KungFu +( + KungFuID INT +,Hustle BIT +,CONSTRAINT PK_KungFu_KungFuID PRIMARY KEY CLUSTERED (KungFuID) +) +GO +INSERT INTO dbo.KungFu + SELECT ROW_NUMBER() OVER (ORDER BY t.object_id) + ,t.object_id % 2 + FROM sys.tables t +GO +DELETE FROM dbo.KungFu +WHERE Hustle = 0 +GO +UPDATE dbo.KungFu +SET Hustle = 0 +WHERE Hustle = 1 +GO +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.leaf_insert_count + ,ios.leaf_update_count + ,ios.leaf_delete_count + ,ios.leaf_ghost_count +FROM sys.dm_db_index_operational_stats(DB_ID(),NULL,NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +WHERE ios.object_id = OBJECT_ID('dbo.KungFu') +ORDER BY ios.range_scan_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-18.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-18.sql new file mode 100644 index 0000000..71df125 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-18.sql @@ -0,0 +1,9 @@ +--Listing 3-18. Query for range_scan_count from sys.dm_db_index_operational_stats +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.range_scan_count +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('Sales.SalesOrderDetail'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios.range_scan_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-19.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-19.sql new file mode 100644 index 0000000..6a6731b --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-19.sql @@ -0,0 +1,9 @@ +--Listing 3-19. Query for singleton_lookup_count from sys.dm_db_index_operational_stats +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.singleton_lookup_count +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('Sales.SalesOrderDetail'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios. singleton_lookup_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-20.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-20.sql new file mode 100644 index 0000000..222d44c --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-20.sql @@ -0,0 +1,12 @@ +--Listing 3-20. T-SQL Script for Forward Records +CREATE TABLE dbo.ForwardedRecords + ( + ID INT IDENTITY(1,1) + ,VALUE VARCHAR(8000) + ); +INSERT INTO dbo.ForwardedRecords (VALUE) +SELECT REPLICATE(type, 500) +FROM sys.objects; +UPDATE dbo.ForwardedRecords +SET VALUE = REPLICATE(VALUE, 16) +WHERE ID%3 = 1; \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-21.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-21.sql new file mode 100644 index 0000000..3339977 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-21.sql @@ -0,0 +1,9 @@ +--Listing 3-21. Query for forwarded_fetch_count from sys.dm_db_index_operational_stats +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.forwarded_fetch_count +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('dbo.ForwardedRecords'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios.forwarded_fetch_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-22.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-22.sql new file mode 100644 index 0000000..eff4656 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-22.sql @@ -0,0 +1,9 @@ +--Listing 3-22. T-SQL Script to Generate Row Locks +USE AdventureWorks2012 +GO +SELECT SalesOrderID + ,SalesOrderDetailID + ,CarrierTrackingNumber + ,OrderQty +FROM Sales.SalesOrderDetail +WHERE ProductID = 710 \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-23.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-23.sql new file mode 100644 index 0000000..06c0737 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-23.sql @@ -0,0 +1,13 @@ +--Listing 3-23. Query for Row Locks in sys.dm_db_index_operational_stats +USE AdventureWorks2012 +GO +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.row_lock_count + ,ios.row_lock_wait_count + ,ios.row_lock_wait_in_ms +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('Sales.SalesOrderDetail'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios.range_scan_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-24.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-24.sql new file mode 100644 index 0000000..c7fd283 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-24.sql @@ -0,0 +1,13 @@ +--Listing 3-24. Query for Page Locks in sys.dm_db_index_operational_stats +USE AdventureWorks2012 +GO +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.page_lock_count + ,ios.page_lock_wait_count + ,ios.page_lock_wait_in_ms +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('Sales.SalesOrderDetail'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios.range_scan_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-25.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-25.sql new file mode 100644 index 0000000..e56345f --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-25.sql @@ -0,0 +1,6 @@ +--Listing 3-25. T-SQL Script to Generate Lock Promotion +USE AdventureWorks2012 +GO +UPDATE Sales.SalesOrderDetail +SET ProductID = ProductID +WHERE ProductID <= 712 \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-26.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-26.sql new file mode 100644 index 0000000..b2d234e --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-26.sql @@ -0,0 +1,12 @@ +--Listing 3-26. Query for Lock Escalation in sys.dm_db_index_operational_stats +USE AdventureWorks2012 +GO +SELECT OBJECT_NAME(ios.object_id) AS table_name + ,i.name AS index_name + ,ios.index_lock_promotion_attempt_count + ,ios.index_lock_promotion_count +FROM sys.dm_db_index_operational_stats(DB_ID(),OBJECT_ID('Sales.SalesOrderDetail'),NULL,NULL) ios + INNER JOIN sys.indexes i + ON i.object_id = ios.object_id + AND i.index_id = ios.index_id +ORDER BY ios.range_scan_count DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-27.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-27.sql new file mode 100644 index 0000000..a8cf5c6 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-27.sql @@ -0,0 +1,10 @@ +--Listing 3-27. T-SQL Script to Generate Page I/O Latch +USE AdventureWorks2012 +GO +DBCC DROPCLEANBUFFERS +GO +SELECT * +FROM Sales.SalesOrderDetail sod +INNER JOIN Sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID +INNER JOIN Production.Product p ON sod.ProductID = p.ProductID +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-28.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-28.sql new file mode 100644 index 0000000..85e2c3c --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-28.sql @@ -0,0 +1,13 @@ +--Listing 3-28. Query for Page I/O Latch Statistics in sys.dm_db_index_operational_stats +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name +,i.name as index_name +,page_io_latch_wait_count +,page_io_latch_wait_in_ms +,CAST(1. * page_io_latch_wait_in_ms + / NULLIF(page_io_latch_wait_count ,0) AS decimal(12,2)) AS page_io_avg_lock_wait_ms +FROM sys.dm_db_index_operational_stats (DB_ID(), NULL, NULL, NULL) ios +INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id +WHERE i.object_id = OBJECT_ID('Sales.SalesOrderHeader') +OR i.object_id = OBJECT_ID('Sales.SalesOrderDetail') +OR i.object_id = OBJECT_ID('Production.Product') +ORDER BY 5 DESC \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-29.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-29.sql new file mode 100644 index 0000000..903c2ea --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-29.sql @@ -0,0 +1,13 @@ +--Listing 3-29. T-SQL Script to Generate Page Latch Scenario +USE AdventureWorks2012 +GO +IF OBJECT_ID('dbo.PageLatchDemo') IS NOT NULL + DROP TABLE dbo.PageLatchDemo +GO +CREATE TABLE dbo.PageLatchDemo +( +PageLatchDemoID INT IDENTITY (1,1) +,FillerData bit +,CONSTRAINT PK_PageLatchDemo_PageLatchDemoID PRIMARY KEY CLUSTERED (PageLatchDemoID) +) +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-30.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-30.sql new file mode 100644 index 0000000..8ff9528 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-30.sql @@ -0,0 +1,5 @@ +--Listing 3-30. T-SQL Script to Generate Page Latch Load +INSERT INTO dbo.PageLatchDemo + (FillerData) +SELECT t.object_id % 2 +FROM sys.tables t \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-31.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-31.sql new file mode 100644 index 0000000..1f7d431 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-31.sql @@ -0,0 +1,11 @@ +--Listing 3-31. Query for Page Latch Statistics in sys.dm_db_index_operational_stats +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name +,i.name as index_name +,page_latch_wait_count +,page_latch_wait_in_ms +,CAST(100. * page_latch_wait_in_ms + / NULLIF(page_latch_wait_count ,0) AS decimal(12,2)) AS page_avg_lock_wait_ms +FROM sys.dm_db_index_operational_stats (DB_ID(), NULL, NULL, NULL) ios +INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id +WHERE i.object_id = OBJECT_ID('dbo.PageLatchDemo') +OR i.object_id = OBJECT_ID('dbo.PageLatchDemo') \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-32.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-32.sql new file mode 100644 index 0000000..6b8f24d --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-32.sql @@ -0,0 +1,25 @@ +--Listing 3-32. T-SQL Script to Generate Page Allocations +USE AdventureWorks2012 ; +GO +IF OBJECT_ID('dbo.AllocationCycle') IS NOT NULL + DROP TABLE dbo.AllocationCycle ; +GO +CREATE TABLE dbo.AllocationCycle +( +ID INT +,FillerData VARCHAR(1000) +,CreateDate DATETIME +,CONSTRAINT PK_AllocationCycle PRIMARY KEY CLUSTERED (ID) +) ; +WITH l0 AS ( + SELECT 0 AS C UNION ALL SELECT 0), + l1 AS (SELECT 0 AS C FROM l0 AS A CROSS JOIN l0 AS B), + l2 AS (SELECT 0 AS C FROM l1 AS A CROSS JOIN l1 AS B), + l3 AS (SELECT 0 AS C FROM l2 AS A CROSS JOIN l2 AS B), + l4 AS (SELECT 0 AS C FROM l3 AS A CROSS JOIN l3 AS B), + l5 AS (SELECT 0 AS C FROM l4 AS A CROSS JOIN l4 AS B), + nums AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n FROM l5) +INSERT INTO dbo.AllocationCycle +SELECT TOP (100000) n, NEWID(), GETDATE() +FROM nums +ORDER BY n ; \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-33.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-33.sql new file mode 100644 index 0000000..b1fcd87 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-33.sql @@ -0,0 +1,9 @@ +--Listing 3-33. Query for Page Latch Statistics in sys.dm_db_index_operational_stats +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name + ,i.name as index_name + ,ios.leaf_allocation_count + ,ios.nonleaf_allocation_count + ,ios.leaf_page_merge_count + ,ios.nonleaf_page_merge_count +FROM sys.dm_db_index_operational_stats(DB_ID(), OBJECT_ID('dbo.AllocationCycle'), NULL,NULL) ios + INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-34.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-34.sql new file mode 100644 index 0000000..be40ea2 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-34.sql @@ -0,0 +1,6 @@ +--Listing 3-34. T-SQL Script to Increase Page Allocations +USE AdventureWorks2012 ; +GO +UPDATE dbo.AllocationCycle +SET FillerData = REPLICATE('x',1000) +WHERE ID % 3 = 1 ; \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-35.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-35.sql new file mode 100644 index 0000000..1f271ba --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-35.sql @@ -0,0 +1,17 @@ +--Listing 3-35. T-SQL Script to Generate Page Latch Scenario +USE AdventureWorks2012 +GO +IF OBJECT_ID('dbo.PageCompression') IS NOT NULL + DROP TABLE dbo.PageCompression +GO +CREATE TABLE dbo.PageCompression( + ProductPhotoID int NOT NULL, + ThumbNailPhoto varbinary(max) NULL, + LargePhoto varbinary(max) NULL, + CONSTRAINT PK_PageCompression PRIMARY KEY CLUSTERED (ProductPhotoID)) + WITH (DATA_COMPRESSION = PAGE); +INSERT INTO dbo.PageCompression +SELECT ProductPhotoID + ,ThumbNailPhoto + ,LargePhoto +FROM Production.ProductPhoto \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-36.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-36.sql new file mode 100644 index 0000000..10c1f37 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-36.sql @@ -0,0 +1,7 @@ +--Listing 3-36. Query for Page Compression in sys.dm_db_index_operational_stats +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name +,i.name as index_name +,page_compression_attempt_count +,page_compression_success_count +FROM sys.dm_db_index_operational_stats (DB_ID(), OBJECT_ID('dbo.PageCompression'), NULL, NULL) ios + INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-37.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-37.sql new file mode 100644 index 0000000..a7a73fc --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-37.sql @@ -0,0 +1,17 @@ +--Listing 3-37. T-SQL Script to Generate LOB Scenario +IF OBJECT_ID('dbo.LOBAccess') IS NOT NULL + DROP TABLE dbo.LOBAccess +GO +CREATE TABLE dbo.LOBAccess + ( + ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED + ,LOBValue VARCHAR(MAX) + ,FillerData CHAR(2000) DEFAULT(REPLICATE('X',2000)) + ,FillerDate DATETIME DEFAULT(GETDATE()) + ) +INSERT INTO dbo.LOBAccess (LOBValue) +SELECT TOP 10 'Short Value' +FROM Production.ProductPhoto +UPDATE dbo.LOBAccess +SET LOBValue = REPLICATE('Long Value',8000) +SELECT * FROM dbo.LOBAccess \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-38.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-38.sql new file mode 100644 index 0000000..61267b6 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-38.sql @@ -0,0 +1,13 @@ +--Listing 3-38. Query for LOB Statistics in sys.dm_db_index_operational_stats +SELECT OBJECT_SCHEMA_NAME(ios.object_id) + '.' + OBJECT_NAME(ios.object_id) as table_name + ,i.name as index_name + ,lob_fetch_in_pages + ,lob_fetch_in_bytes + ,lob_orphan_create_count + ,lob_orphan_insert_count + ,row_over?ow_fetch_in_pages + ,row_over?ow_fetch_in_bytes + ,column_value_push_off_row_count + ,column_value_pull_in_row_count +FROM sys.dm_db_index_operational_stats (DB_ID(), OBJECT_ID('dbo.LOBAccess'), NULL, NULL) ios +INNER JOIN sys.indexes i ON i.object_id = ios.object_id AND i.index_id = ios.index_id \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-39.sql b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-39.sql new file mode 100644 index 0000000..8389b97 --- /dev/null +++ b/Performance Indexing - Chapter 03/Chapter 03 - Listing 3-39.sql @@ -0,0 +1,8 @@ +--Listing 3-39. Parameters for sys.dm_db_index_physical_stats +sys.dm_db_index_physical_stats ( + { database_id | NULL | 0 | DEFAULT } + , { object_id | NULL | 0 | DEFAULT } + , { index_id | NULL | 0 | -1 | DEFAULT } + , { partition_number | NULL | 0 | DEFAULT } + , { mode | NULL | DEFAULT } +) \ No newline at end of file diff --git a/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssms_suo b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssms_suo new file mode 100644 index 0000000..26ae765 Binary files /dev/null and b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssms_suo differ diff --git a/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssln b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssln new file mode 100644 index 0000000..6a923c1 --- /dev/null +++ b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# SQL Server Management Studio Solution File, Format Version 11.00 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Performance Indexing - Chapter 03", "Performance Indexing - Chapter 03.ssmssqlproj", "{015B50B9-6FC4-468B-9666-EEB9A5BCCD1B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {015B50B9-6FC4-468B-9666-EEB9A5BCCD1B}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssqlproj b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssqlproj new file mode 100644 index 0000000..18db780 --- /dev/null +++ b/Performance Indexing - Chapter 03/Performance Indexing - Chapter 03.ssmssqlproj @@ -0,0 +1,262 @@ + + + + + + + 2013-06-13T11:10:37.6686695-04:00 + SQL + FL-WS-CON-JS01 + + Windows Authentication + + 15 + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + + + + + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-01.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-02.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-03.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-04.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-05.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-06.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-07.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-08.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-09.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-10.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-11.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-12.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-13.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-14.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-15.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-16.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-17.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-18.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-19.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-20.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-21.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-22.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-23.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-24.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-25.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-26.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-27.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-28.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-29.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-30.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-31.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-32.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-33.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-34.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-35.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-36.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-37.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-38.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 03 - Listing 3-39.sql + + + + + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-01.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-01.sql new file mode 100644 index 0000000..5b1bbc1 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-01.sql @@ -0,0 +1,17 @@ +--Listing 4-1. An Example of Well-formed XML + + + + + 1.32 + .97 + + + + + 2.99 + .40 + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-02.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-02.sql new file mode 100644 index 0000000..8e3d36b --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-02.sql @@ -0,0 +1,34 @@ +--Listing 4-2. CREATE SCHEMA COLLECTION Used to Create Typed XML +CREATE XML SCHEMA COLLECTION CheckoutCouponSchema AS ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +' +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-03.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-03.sql new file mode 100644 index 0000000..06de001 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-03.sql @@ -0,0 +1,8 @@ +--Listing 4-3. Creating a Table and Specifying a Schema Collection on XML Columns +CREATE TABLE [dbo].[XMLTable]( + [XMLValue] [xml](CONTENT [dbo].[CheckoutCouponSchema]) NULL, + [TransID] [bigint] IDENTITY(1,1) NOT NULL, +PRIMARY KEY CLUSTERED +( + [TransID] ASC +)) \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-04.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-04.sql new file mode 100644 index 0000000..bf53ae0 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-04.sql @@ -0,0 +1,19 @@ +--Listing 4-4. Inserting XML into a SQL Server Table and XML Column +DECLARE @XML XML +SET @XML = ' + + + + 1.32 + .97 + + + + + 2.99 + .40 + + + +' +INSERT INTO XMLTable VALUES (@XML) \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-05.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-05.sql new file mode 100644 index 0000000..a7fdd51 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-05.sql @@ -0,0 +1,9 @@ +--Listing 4-5. Querying XML Data with the exist() Method +SELECT +XMLValue.value('(/CheckoutItem/TotalSales/Product/ItemSale/NetPrice)[1]','varchar(max)') + AS [Net Price], +XMLValue.value('(/CheckoutItem/TotalSales/Product/ItemSale/CouponPrice)[1]','varchar(max)') + AS [Coupon Savings] +FROM XMLTable +WHERE + XMLValue.exist('//TotalSales/Product/@ProdID[.="468"]') = 1 \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-06.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-06.sql new file mode 100644 index 0000000..d324676 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-06.sql @@ -0,0 +1,17 @@ +--Listing 4-6. Creating a Database to Hold Geometry-related Data +CREATE TABLE [dbo].[AREAZIP]( + [ident] [int] IDENTITY(1,1) NOT NULL, + [AREA] [real] NULL, + [PERIMETER] [real] NULL, + [ZT55_D00_] [bigint] NULL, + [ZT55_D00_I] [bigint] NULL, + [ZCTA] [nvarchar](255) NULL, + [NAME] [nvarchar](255) NULL, + [LSAD] [nvarchar](255) NULL, + [LSAD_TRANS] [nvarchar](255) NULL, + [geom] [geometry] NULL, + CONSTRAINT [PK_AREAZIP] PRIMARY KEY CLUSTERED +( + [ident] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-07.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-07.sql new file mode 100644 index 0000000..2dd74b9 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-07.sql @@ -0,0 +1,3 @@ +--Listing 4-7. Using MakeValid() to Correct any Invalid Geometry Instances +UPDATE AREAZIP +SET GEOM = GEOM.MakeValid() \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-08.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-08.sql new file mode 100644 index 0000000..01a6b3b --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-08.sql @@ -0,0 +1,5 @@ +--Listing 4-8. Query for the Top Ten Closest ZIP Codes to a Given Point +Declare @point geometry = geometry::STGeomFromText('POINT(-87.819473 42.55022)', 0) +SELECT TOP 10 geom FROM zt55_d00 +WHERE geom.MakeValid().STDistance(@point) IS NOT NULL AND geom.MakeValid().STDistance(@point) < 1 +ORDER BY geom.MakeValid().STDistance(@point); \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-09.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-09.sql new file mode 100644 index 0000000..01d2038 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-09.sql @@ -0,0 +1,9 @@ +--Listing 4-9. Create Statement for a Spatial Index +CREATE SPATIAL INDEX IDX_WIZIP_GEOM ON [dbo].[AREAZIP] +( + [geom] +)USING GEOMETRY_GRID +WITH ( +BOUNDING_BOX =(-91.513079, -87.496494, 36.970298, 36.970298), GRIDS =(LEVEL_1 = LOW,LEVEL_2 = MEDIUM,LEVEL_3 = MEDIUM,LEVEL_4 = HIGH), +CELLS_PER_OBJECT = 16, PAD_INDEX = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-10.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-10.sql new file mode 100644 index 0000000..c905bb5 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-10.sql @@ -0,0 +1,11 @@ +--Listing 4-10. Table and INSERT Statements Used with Full-Text Search +CREATE TABLE SQLServerDocuments (ID INT IDENTITY(1,1) PRIMARY KEY, DocType VARCHAR(6), DOC +VARBINARY(MAX)) +GO +DECLARE @worddoc VARBINARY(MAX) +SELECT @worddoc = CAST(bulkcolumn AS VARBINARY(MAX)) +FROM OPENROWSET( BULK 'C:\High Availability with SQL Server 2008.doc', SINGLE_BLOB ) AS x + +INSERT INTO SQLServerDocuments (DocType,DOC) +VALUES ('doc',@worddoc) +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-11.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-11.sql new file mode 100644 index 0000000..fb370f6 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-11.sql @@ -0,0 +1,6 @@ +--Listing 4-11. The Create Full-Text Catalog Syntax +CREATE FULLTEXT CATALOG +WITH +AS DEFAULT +AUTHORIZATION +ACCENT_SENSITIVITY = \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-12.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-12.sql new file mode 100644 index 0000000..1d8071c --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-12.sql @@ -0,0 +1,8 @@ +--Listing 4-12. Create Full-Text Index Syntax +CREATE FULLTEXT INDEX ON +() +KEY INDEX +ON +WITH +CHANGE_TRACKING = +STOPLIST = \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-13.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-13.sql new file mode 100644 index 0000000..4a8a245 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-13.sql @@ -0,0 +1,10 @@ +--Listing 4-13. Using sys.fulltext_stoplists to Review StopList Words +SELECT + lists.stoplist_id, + [name], + stopword +FROM +sys.fulltext_stoplists lists +JOIN sys.fulltext_stopwords words on lists.stoplist_id = words.stoplist_id +WHERE words.[language] = 'English' +ORDER BY lists.[name] \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-14.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-14.sql new file mode 100644 index 0000000..c4604c2 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-14.sql @@ -0,0 +1,11 @@ +--Listing 4-14. CREATE FULL TEXT INDEX Statement +CREATE FULLTEXT INDEX ON dbo. +SQLServerDocuments + ( + DOC + TYPE COLUMN DocType + ) + KEY INDEX [PK__SQLServe__3214EC2769F9A9EA] + ON WhitePaperCatalog + WITH STOPLIST = WhitePaperStopList; +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-15.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-15.sql new file mode 100644 index 0000000..5ace0cb --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-15.sql @@ -0,0 +1,5 @@ +--Listing 4-15. Using CONTAINS to Query for a Speci?c Word +SELECT + DOC, DocType +FROM SQLServerDocuments +WHERE CONTAINS(DOC, 'replication') \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-16.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-16.sql new file mode 100644 index 0000000..4d1e0cd --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-16.sql @@ -0,0 +1,2 @@ +--Listing 4-16. FULLTEXTCATALOGPROPERTY to Return Population Status of a Catalog +SELECT FULLTEXTCATALOGPROPERTY('WhitePaperCatalog','PopulateStatus') \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-17.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-17.sql new file mode 100644 index 0000000..b51bc03 --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-17.sql @@ -0,0 +1,6 @@ +--Listing 4-17. Utilizing sys.fulltext_index_catalog_usages +SELECT + OBJECT_NAME(object_id) [Object Name], + index_id, + fulltext_catalog_id +FROM sys.fulltext_index_catalog_usages \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-18.sql b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-18.sql new file mode 100644 index 0000000..0fb643c --- /dev/null +++ b/Performance Indexing - Chapter 04/Chapter 04 - Listing 4-18.sql @@ -0,0 +1,15 @@ +--Listing 4-18. Using all the Catalog Views for Full-Text Index Information +SELECT + idx.is_enabled, + idx.change_tracking_state, + idx.crawl_type_desc, + idx.crawl_end_date [Last Crawl], + cat.[name], + CASE WHEN cat.is_accent_sensitivity_on = 0 THEN 'Accent InSensitive' + WHEN cat.is_accent_sensitivity_on = 1 THEN 'Accent Sensitive' + END [Accent Sensitivity], + lists.[name], + lists.modify_date [Last Modi?ed Date of StopList] +FROM sys.fulltext_indexes idx +JOIN sys.fulltext_catalogs cat on idx.fulltext_catalog_id = cat.fulltext_catalog_id +JOIN sys.fulltext_stoplists lists on idx.stoplist_id = lists.stoplist_id \ No newline at end of file diff --git a/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssms_suo b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssms_suo new file mode 100644 index 0000000..40cb2c1 Binary files /dev/null and b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssms_suo differ diff --git a/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssln b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssln new file mode 100644 index 0000000..ccd5a6d --- /dev/null +++ b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# SQL Server Management Studio Solution File, Format Version 11.00 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Performance Indexing - Chapter 04", "Performance Indexing - Chapter 04.ssmssqlproj", "{9B38C4FF-F490-4D07-8047-898B911F20C0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9B38C4FF-F490-4D07-8047-898B911F20C0}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssqlproj b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssqlproj new file mode 100644 index 0000000..2c9cb7a --- /dev/null +++ b/Performance Indexing - Chapter 04/Performance Indexing - Chapter 04.ssmssqlproj @@ -0,0 +1,136 @@ + + + + + + + 2013-06-13T11:34:23.2912104-04:00 + SQL + FL-WS-CON-JS01 + + Windows Authentication + + 15 + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + + + + + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-01.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-02.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-03.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-04.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-05.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-06.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-07.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-08.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-09.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-10.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-11.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-12.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-13.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-14.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-15.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-16.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-17.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 04 - Listing 4-18.sql + + + + + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-01.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-01.sql new file mode 100644 index 0000000..7892042 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-01.sql @@ -0,0 +1,14 @@ +--Listing 5-1. Table with No Index +SELECT * INTO MythOne +FROM Sales.SalesOrderDetail +GO +SET STATISTICS IO ON +SET NOCOUNT ON +GO +SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, +SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal +FROM MythOne +WHERE CarrierTrackingNumber = '4911-403C-98' +GO +SET STATISTICS IO OFF +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-02.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-02.sql new file mode 100644 index 0000000..3b182a4 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-02.sql @@ -0,0 +1,3 @@ +--Listing 5-2. I/O Statistics for Table with No Index +Table 'MythOne'. Scan count 1, logical reads 1494, physical reads 0, read-ahead reads 0, lob +logical reads 0, lob physical reads 0, lob read-ahead reads 0. \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-03.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-03.sql new file mode 100644 index 0000000..0ababf7 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-03.sql @@ -0,0 +1,13 @@ +--Listing 5-3. Adding an Index to MythOne +CREATE INDEX IX_CarrierTrackingNumber ON MythOne (CarrierTrackingNumber) +GO +SET STATISTICS IO ON +SET NOCOUNT ON +GO +SELECT SalesOrderID, SalesOrderDetailID, CarrierTrackingNumber, OrderQty, ProductID, +SpecialOfferID, UnitPrice, UnitPriceDiscount, LineTotal +FROM MythOne +WHERE CarrierTrackingNumber = '4911-403C-98' +GO +SET STATISTICS IO OFF +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-04.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-04.sql new file mode 100644 index 0000000..3d0c592 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-04.sql @@ -0,0 +1,3 @@ +--Listing 5-4. I/O Statistics for Table with an Index +Table 'MythOne'. Scan count 1, logical reads 15, physical reads 0, read-ahead reads 0, +lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-05.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-05.sql new file mode 100644 index 0000000..3ad3667 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-05.sql @@ -0,0 +1,29 @@ +--Listing 5-5. Two Ways to Create PRIMARY KEYS +CREATE TABLE dbo.MythTwo1 + ( + RowID int NOT NULL + ,Column1 nvarchar(128) + ,Column2 nvarchar(128) + ) +ALTER TABLE dbo.MythTwo1 +ADD CONSTRAINT PK_MythTwo1 PRIMARY KEY (RowID) +GO +CREATE TABLE dbo.MythTwo2 + ( + RowID int NOT NULL + ,Column1 nvarchar(128) + ,Column2 nvarchar(128) + ) +CREATE CLUSTERED INDEX CL_MythTwo2 ON dbo.MythTwo2 (RowID) +ALTER TABLE dbo.MythTwo2 +ADD CONSTRAINT PK_MythTwo2 PRIMARY KEY (RowID) +GO +SELECT OBJECT_NAME(object_id) AS table_name + ,name + ,index_id + ,type + ,type_desc + ,is_unique + ,is_primary_key +FROM sys.indexes +WHERE object_id IN (OBJECT_ID('dbo.MythTwo1'),OBJECT_ID('dbo.MythTwo2')) \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-06.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-06.sql new file mode 100644 index 0000000..10cda13 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-06.sql @@ -0,0 +1,20 @@ +--Listing 5-6. Online Index to Build Myth Script +CREATE TABLE dbo.MythThree + ( + RowID int NOT NULL + ,Column1 uniqueidenti?er + ); +WITH L1(z) AS (SELECT 0 UNION ALL SELECT 0) +, L2(z) AS (SELECT 0 FROM L1 a CROSS JOIN L1 b) +, L3(z) AS (SELECT 0 FROM L2 a CROSS JOIN L2 b) +, L4(z) AS (SELECT 0 FROM L3 a CROSS JOIN L3 b) +, L5(z) AS (SELECT 0 FROM L4 a CROSS JOIN L4 b) +, L6(z) AS (SELECT TOP 10000000 0 FROM L5 a CROSS JOIN L5 b) +INSERT INTO dbo.MythThree +SELECT ROW_NUMBER() OVER (ORDER BY z) AS RowID, NEWID() +FROM L6; +GO +CREATE INDEX IX_MythThree_ONLINE ON MythThree (Column1) WITH (ONLINE = ON); +GO +CREATE INDEX IX_MythThree ON MythThree (Column1); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-07.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-07.sql new file mode 100644 index 0000000..eedf9b0 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-07.sql @@ -0,0 +1,8 @@ +--Listing 5-7. Extended Event Session for Lock Acquired and Released +CREATE EVENT SESSION [MythThree] ON SERVER +ADD EVENT sqlserver.lock_acquired(SET collect_database_name=(1) + WHERE ([sqlserver].[session_id]=(66))), +ADD EVENT sqlserver.lock_released( + WHERE ([sqlserver].[session_id]=(66))) +ADD TARGET package0.ring_buffer +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-08.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-08.sql new file mode 100644 index 0000000..d6a0834 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-08.sql @@ -0,0 +1,7 @@ +--Listing 5-8. Multicolumn Index Myth +SELECT SalesOrderID, OrderDate, DueDate, ShipDate +INTO dbo.MythFour +FROM Sales.SalesOrderHeader; +ALTER TABLE dbo.MythFour +ADD CONSTRAINT PK_MythFour PRIMARY KEY CLUSTERED (SalesOrderID); +CREATE NONCLUSTERED INDEX IX_MythFour ON dbo.MythFour (OrderDate, DueDate, ShipDate); \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-09.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-09.sql new file mode 100644 index 0000000..7c026c0 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-09.sql @@ -0,0 +1,3 @@ +--Listing 5-9. Query Using Left-most Column in Index +SELECT OrderDate FROM dbo.MythFour +WHERE OrderDate = '2001-07-17 00:00:00.000' \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-10.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-10.sql new file mode 100644 index 0000000..0bd2c01 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-10.sql @@ -0,0 +1,3 @@ +--Listing 5-10. Query Using Right-most Column in Index +SELECT ShipDate FROM dbo.MythFour +WHERE ShipDate = '2001-07-17 00:00:00.000' \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-11.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-11.sql new file mode 100644 index 0000000..ece5cef --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-11.sql @@ -0,0 +1,3 @@ +--Listing 5-11. Query Using Middle Column in Index +SELECT DueDate FROM dbo.MythFour +WHERE DueDate = '2001-07-17 00:00:00.000' \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-12.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-12.sql new file mode 100644 index 0000000..8b0f53f --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-12.sql @@ -0,0 +1,12 @@ +--Listing 5-12. Create and Populate Myth Five Table +CREATE TABLE dbo.MythFive +( +RowID int PRIMARY KEY CLUSTERED +,TestValue varchar(20) NOT NULL +); +INSERT INTO dbo.MythFive (RowID, TestValue) VALUES (1, 'FirstRecordAdded'); +INSERT INTO dbo.MythFive (RowID, TestValue) VALUES (3, 'SecondRecordAdded'); +INSERT INTO dbo.MythFive (RowID, TestValue) VALUES (2, 'ThirdRecordAdded'); +GO +DBCC IND ('AdventureWorks2008R2', 'dbo.MythFive', 1); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-13.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-13.sql new file mode 100644 index 0000000..ec502df --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-13.sql @@ -0,0 +1,5 @@ +--Listing 5-13. Create and Populate Myth Five Table +DBCC TRACEON (3604); +GO +DBCC PAGE (AdventureWorks, 1, 140107, 2); +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-14.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-14.sql new file mode 100644 index 0000000..8afd198 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-14.sql @@ -0,0 +1,21 @@ +--Listing 5-14. Create and Populate Myth Six Table +CREATE TABLE dbo.MythSix + ( + RowID int NOT NULL + ,Column1 varchar(500) + ); +ALTER TABLE dbo.MythSix ADD CONSTRAINT + PK_MythSix PRIMARY KEY CLUSTERED (RowID) WITH(FILLFACTOR = 50) +WITH L1(z) AS (SELECT 0 UNION ALL SELECT 0) +, L2(z) AS (SELECT 0 FROM L1 a CROSS JOIN L1 b) +, L3(z) AS (SELECT 0 FROM L2 a CROSS JOIN L2 b) +, L4(z) AS (SELECT 0 FROM L3 a CROSS JOIN L3 b) +, L5(z) AS (SELECT 0 FROM L4 a CROSS JOIN L4 b) +, L6(z) AS (SELECT TOP 1000 0 FROM L5 a CROSS JOIN L5 b) +INSERT INTO dbo.MythSix + +SELECT ROW_NUMBER() OVER (ORDER BY z) AS RowID, REPLICATE('X', 500) +FROM L6 +SELECT object_id, index_id, avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.MythSix'),NULL,NULL,'DETAILED') +WHERE index_level = 0 \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-15.sql b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-15.sql new file mode 100644 index 0000000..ab20059 --- /dev/null +++ b/Performance Indexing - Chapter 05/Chapter 05 - Listing 5-15.sql @@ -0,0 +1,5 @@ +--Listing 5-15. Rebuild Clustered Index on Myth Six Table +ALTER INDEX PK_MythSix ON dbo.MythSix REBUILD +SELECT object_id, index_id, avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.MythSix'),NULL,NULL,'DETAILED') +WHERE index_level = 0 \ No newline at end of file diff --git a/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssms_suo b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssms_suo new file mode 100644 index 0000000..c959c7e Binary files /dev/null and b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssms_suo differ diff --git a/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssln b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssln new file mode 100644 index 0000000..331f137 --- /dev/null +++ b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# SQL Server Management Studio Solution File, Format Version 11.00 +Project("{4F2E2C19-372F-40D8-9FA7-9D2138C6997A}") = "Performance Indexing - Chapter 05", "Performance Indexing - Chapter 05.ssmssqlproj", "{03821AE7-1A4F-4F3E-B8AC-DD3F6D595B4C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Default|Default = Default|Default + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {03821AE7-1A4F-4F3E-B8AC-DD3F6D595B4C}.Default|Default.ActiveCfg = Default + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssqlproj b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssqlproj new file mode 100644 index 0000000..3f4b039 --- /dev/null +++ b/Performance Indexing - Chapter 05/Performance Indexing - Chapter 05.ssmssqlproj @@ -0,0 +1,118 @@ + + + + + + + 2013-06-13T17:57:19.2117333-04:00 + SQL + FL-WS-CON-JS01 + + Windows Authentication + + 15 + 0 + NotSpecified + Microsoft SQL Server Management Studio - Query + + + + + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-01.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-02.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-03.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-04.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-05.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-06.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-07.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-08.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-09.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-10.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-11.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-12.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-13.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-14.sql + + + 8c91a03d-f9b4-46c0-a305-b5dcc79ff907:FL-WS-CON-JS01:True + FL-WS-CON-JS01 + + Chapter 05 - Listing 5-15.sql + + + + + + + + \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-01.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-01.sql new file mode 100644 index 0000000..e2cfbc3 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-01.sql @@ -0,0 +1,13 @@ +--Listing 6-1. Populate Uniqueidenti?er Table +IF OBJECT_ID('dbo.UsingUniqueidenti?er') IS NOT NULL + DROP TABLE dbo.UsingUniqueidenti?er; +CREATE TABLE dbo.UsingUniqueidenti?er +( +RowID uniqueidenti?er CONSTRAINT DF_GUIDValue DEFAULT NEWID() +,Name sysname +,JunkValue varchar(2000) +); +INSERT INTO dbo.UsingUniqueidenti?er (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +CREATE CLUSTERED INDEX CLUS_UsingUniqueidenti?er ON dbo.UsingUniqueidenti?er(RowID); \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-02.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-02.sql new file mode 100644 index 0000000..58afbbe --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-02.sql @@ -0,0 +1,11 @@ +--Listing 6-2. View INSERT Index Fragmentation +SELECT index_type_desc + ,index_depth + ,index_level + ,page_count + ,record_count + ,CAST(avg_fragmentation_in_percent as DECIMAL(6,2)) as avg_fragmentation_in_percent + ,fragment_count + ,avg_fragment_size_in_pages + ,CAST(avg_page_space_used_in_percent as DECIMAL(6,2)) as avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.UsingUniqueidenti?er'),NULL,NULL,'DETAILED') \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-03.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-03.sql new file mode 100644 index 0000000..8e5ddfb --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-03.sql @@ -0,0 +1,4 @@ +--Listing 6-3. INSERT into Uniqueidenti?er Table +INSERT INTO dbo.UsingUniqueidenti?er (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.objects \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-04.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-04.sql new file mode 100644 index 0000000..459e8b0 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-04.sql @@ -0,0 +1,2 @@ +--Listing 6-4. Create Non-Clustered Index +CREATE NONCLUSTERED INDEX IX_Name ON dbo.UsingUniqueidenti?er(Name) INCLUDE (JunkValue); \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-05.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-05.sql new file mode 100644 index 0000000..f406055 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-05.sql @@ -0,0 +1,11 @@ +--Listing 6-5. View UPDATE Index Fragmentation +SELECT index_type_desc + ,index_depth + ,index_level + ,page_count + ,record_count + ,CAST(avg_fragmentation_in_percent as DECIMAL(6,2)) as avg_fragmentation_in_percent + ,fragment_count + ,avg_fragment_size_in_pages + ,CAST(avg_page_space_used_in_percent as DECIMAL(6,2)) as avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.UpdateOperations'),NULL,NULL,'DETAILED') \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-06.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-06.sql new file mode 100644 index 0000000..54c327d --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-06.sql @@ -0,0 +1,13 @@ +--Listing 6-6. CreateTable for UPDATE Operations +IF OBJECT_ID('dbo.UpdateOperations') IS NOT NULL + DROP TABLE dbo.UpdateOperations; +CREATE TABLE dbo.UpdateOperations +( +RowID int IDENTITY(1,1) +,Name sysname +,JunkValue varchar(2000) +); +INSERT INTO dbo.UpdateOperations (Name, JunkValue) +SELECT name, REPLICATE('X', 1000) +FROM sys.columns +CREATE CLUSTERED INDEX CLUS_UsingUniqueidenti?er ON dbo.UpdateOperations(RowID); \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-07.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-07.sql new file mode 100644 index 0000000..2ef751d --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-07.sql @@ -0,0 +1,4 @@ +--Listing 6-7. Create Table for UPDATE Operations +UPDATE dbo.UpdateOperations +SET JunkValue = REPLICATE('X', 2000) +WHERE RowID % 5 = 1 \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-08.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-08.sql new file mode 100644 index 0000000..b7729ba --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-08.sql @@ -0,0 +1,2 @@ +--Listing 6-8. Create Non-clustered Index for UPDATE operations +CREATE NONCLUSTERED INDEX IX_Name ON dbo.UpdateOperations(Name) INCLUDE (JunkValue); \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-09.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-09.sql new file mode 100644 index 0000000..433f7d3 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-09.sql @@ -0,0 +1,4 @@ +--Listing 6-9. UPDATE Operation to Change Index Key Value +UPDATE dbo.UpdateOperations +SET Name = REVERSE(Name) +WHERE RowID % 9 = 1 \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-10.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-10.sql new file mode 100644 index 0000000..be5aac6 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-10.sql @@ -0,0 +1,12 @@ +--Listing 6-10. View DELETE Index Fragmentation +SELECT index_type_desc + ,index_depth + ,index_level + ,page_count + ,record_count + ,CAST(avg_fragmentation_in_percent as DECIMAL(6,2)) as avg_fragmentation_in_percent + ,fragment_count + ,avg_fragment_size_in_pages + ,CAST(avg_page_space_used_in_percent as DECIMAL(6,2)) as avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.DeleteOperations'), +NULL,NULL,'DETAILED') \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-11.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-11.sql new file mode 100644 index 0000000..e7390f7 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-11.sql @@ -0,0 +1,13 @@ +--Listing 6-11. Creating a Table for DELETE Operation +IF OBJECT_ID('dbo.DeleteOperations') IS NOT NULL + DROP TABLE dbo.DeleteOperations; +CREATE TABLE dbo.DeleteOperations +( +RowID int IDENTITY(1,1) +,Name sysname +,JunkValue varchar(2000) +); +INSERT INTO dbo.DeleteOperations (Name, JunkValue) +SELECT name, REPLICATE('X', 1000) +FROM sys.columns +CREATE CLUSTERED INDEX CLUS_UsingUniqueidenti?er ON dbo.DeleteOperations(RowID); \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-12.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-12.sql new file mode 100644 index 0000000..eccb9a5 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-12.sql @@ -0,0 +1,3 @@ +--Listing 6-12. Performing DELETE Operation +DELETE dbo.DeleteOperations +WHERE RowID % 100 BETWEEN 1 AND 50 \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-13.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-13.sql new file mode 100644 index 0000000..a953067 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-13.sql @@ -0,0 +1,11 @@ +--Listing 6-13. View Index Fragmentation from Shrink +SELECT index_type_desc + ,index_depth + ,index_level + ,page_count + ,record_count + ,CAST(avg_fragmentation_in_percent as DECIMAL(6,2)) as avg_fragmentation_in_percent + ,fragment_count + ,avg_fragment_size_in_pages + ,CAST(avg_page_space_used_in_percent as DECIMAL(6,2)) as avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.FirstTable'),NULL,NULL,'DETAILED') \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-14.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-14.sql new file mode 100644 index 0000000..f69d517 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-14.sql @@ -0,0 +1,49 @@ +--Listing 6-14. Shrink Operation Database Preparation +USE master +GO +DROP DATABASE Fragmentation +GO +CREATE DATABASE Fragmentation +GO +Use Fragmentation +GO +IF OBJECT_ID('dbo.FirstTable') IS NOT NULL + DROP TABLE dbo.FirstTable; +CREATE TABLE dbo.FirstTable +( +RowID int IDENTITY(1,1) +,Name sysname +,JunkValue varchar(2000) +,CONSTRAINT PK_FirstTable PRIMARY KEY CLUSTERED (RowID) +); +INSERT INTO dbo.FirstTable (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +IF OBJECT_ID('dbo.SecondTable') IS NOT NULL + DROP TABLE dbo.SecondTable; +CREATE TABLE dbo.SecondTable +( +RowID int IDENTITY(1,1) +,Name sysname +,JunkValue varchar(2000) +,CONSTRAINT PK_SecondTable PRIMARY KEY CLUSTERED (RowID) +); +INSERT INTO dbo.SecondTable (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +GO +INSERT INTO dbo.FirstTable (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +GO +INSERT INTO dbo.SecondTable (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +GO +INSERT INTO dbo.FirstTable (Name, JunkValue) +SELECT name, REPLICATE('X', 2000) +FROM sys.columns +GO +IF OBJECT_ID('dbo.SecondTable') IS NOT NULL + DROP TABLE dbo.SecondTable; +GO \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-15.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-15.sql new file mode 100644 index 0000000..3ed1465 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-15.sql @@ -0,0 +1,2 @@ +--Listing 6-15. Shrink Operation +DBCC SHRINKDATABASE (Fragmentation) \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-16.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-16.sql new file mode 100644 index 0000000..96e3a7d --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-16.sql @@ -0,0 +1,11 @@ +--Listing 6-16. View Index Fragmentation for I/O Example +SELECT index_type_desc + ,index_depth + ,index_level + ,page_count + ,record_count + ,CAST(avg_fragmentation_in_percent as DECIMAL(6,2)) as avg_fragmentation_in_percent + ,fragment_count + ,avg_fragment_size_in_pages + ,CAST(avg_page_space_used_in_percent as DECIMAL(6,2)) as avg_page_space_used_in_percent +FROM sys.dm_db_index_physical_stats(DB_ID(),OBJECT_ID('dbo.IndexIO),NULL,NULL,'DETAILED') \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-17.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-17.sql new file mode 100644 index 0000000..69a8c0d --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-17.sql @@ -0,0 +1,25 @@ +--Listing 6-17. Script to Build Index I/O Example +IF OBJECT_ID('dbo.IndexIO') IS NOT NULL + DROP TABLE dbo.IndexIO; +CREATE TABLE dbo.IndexIO +( +RowID int IDENTITY(1,1) +,Name sysname +,JunkValue varchar(2000) +); +INSERT INTO dbo.IndexIO (Name, JunkValue) +SELECT name, REPLICATE('X', 1000) +FROM sys.columns +CREATE CLUSTERED INDEX CLUS_IndexIO ON dbo.IndexIO(RowID); +UPDATE dbo.IndexIO +SET JunkValue = REPLICATE('X', 2000) +WHERE RowID % 5 = 1 +SELECT i.name, ps.in_row_reserved_page_count +FROM sys.indexes i +INNER JOIN sys.dm_db_partition_stats ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id +WHERE i.name = 'CLUS_IndexIO' +ALTER INDEX CLUS_IndexIO ON dbo.IndexIO REBUILD +SELECT i.name, ps.in_row_reserved_page_count +FROM sys.indexes i +INNER JOIN sys.dm_db_partition_stats ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id +WHERE i.name = 'CLUS_IndexIO' \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-18.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-18.sql new file mode 100644 index 0000000..28dc8e9 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-18.sql @@ -0,0 +1,7 @@ +--Listing 6-18. Script to Rebuild Index to Remove Fragmentation +ALTER INDEX CLUS_IndexIO ON dbo.IndexIO REBUILD + +SELECT i.name, ps.in_row_reserved_page_count +FROM sys.indexes i +INNER JOIN sys.dm_db_partition_stats ps ON i.object_id = ps.object_id AND i.index_id = ps.index_id +WHERE i.name = 'CLUS_IndexIO' \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-19.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-19.sql new file mode 100644 index 0000000..c884f66 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-19.sql @@ -0,0 +1,15 @@ +--Listing 6-19. Index Rebuild with CREATE INDEX +CREATE [ UNIQUE ] [ CLUSTERED | NONCLUSTERED ] INDEX index_name + ON ( column [ ASC | DESC ] [ ,…n ] ) + [ INCLUDE ( column_name [ ,…n ] ) ] + [ WHERE ] + [ WITH ( [ ,…n ] ) ] + [ ON { partition_scheme_name ( column_name ) + | ?legroup_name + | default + } + ] + [ FILESTREAM_ON { ?lestream_?legroup_name | partition_scheme_name | “NULL” } ] +[ ; ] + ::= + DROP_EXISTING = { ON | OFF } \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-20.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-20.sql new file mode 100644 index 0000000..0d4c86e --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-20.sql @@ -0,0 +1,12 @@ +--Listing 6-20. Index Rebuild with ALTER INDEX +ALTER INDEX { index_name | ALL } + ON + { REBUILD + [ [PARTITION = ALL] + [ WITH ( [ ,…n ] ) ] + | [ PARTITION = partition_number + [ WITH ( + [ ,…n ] ) + ] + ] + ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-21.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-21.sql new file mode 100644 index 0000000..34f4594 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-21.sql @@ -0,0 +1,6 @@ +--Listing 6-21. Index Reorganization with ALTER INDEX +ALTER INDEX { index_name | ALL } + ON + | REORGANIZE + [ PARTITION = partition_number ] + [ WITH ( LOB_COMPACTION = { ON | OFF } ) ] \ No newline at end of file diff --git a/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-22.sql b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-22.sql new file mode 100644 index 0000000..54077c2 --- /dev/null +++ b/Performance Indexing - Chapter 06/Chapter 06 - Listing 6-22.sql @@ -0,0 +1,35 @@ +--Listing 6-22. Index Defragmantion Script Template +DECLARE @MaxFragmentation TINYINT=30 +,@MinimumPages SMALLINT=1000 +,@SQL nvarchar(max) +,@ObjectName NVARCHAR(300) +,@IndexName NVARCHAR(300) +,@CurrentFragmentation DECIMAL(9, 6) +DECLARE @FragmentationState TABLE +( +SchemaName SYSNAME +,TableName SYSNAME +,object_id INT +,IndexName SYSNAME +,index_id INT +,page_count BIGINT +,avg_fragmentation_in_percent FLOAT +,avg_page_space_used_in_percent FLOAT +,type_desc VARCHAR(255) +) +INSERT INTO @FragmentationState +