Download presentation
Presentation is loading. Please wait.
Published byGloria Bailey Modified over 9 years ago
1
with Michelle Ufford
2
Sr. DBA for GoDaddy.com Working with SQL for 6 years, focused on performance tuning and VLDB’s Member of the PASS Performance SIG President, 380PASS SQL User Group - http://380pass.orghttp://380pass.org Active blogger at http://sqlfool.comhttp://sqlfool.com Twitters at http://twitter.com/sqlfoolhttp://twitter.com/sqlfool
3
The general syntax for creating a table is: CREATE TABLE [schema].[tableName] ( ); i.e. CREATE TABLE dbo.myTable ( id int NOT NULL, lastName varchar(20) NOT NULL, firstName varchar(20) NULL ); In general, narrow tables perform better.
4
Heap Table LastNameFirstName CarsonAmanda SmithJohn AdamsMike ThomsonLaura JohnsonBrian Heap Table LastNameFirstName CarsonAmanda SmithJohn AdamsMike ThomsonLaura JohnsonBrian Ordered Table (Clustered Index) LastNameFirstName AdamsMike CarsonAmanda JohnsonBrian SmithJohn ThomsonLaura Ordered Table (Clustered Index) LastNameFirstName AdamsMike CarsonAmanda JohnsonBrian SmithJohn ThomsonLaura un-ordered data fast writes slow reads not typically recommended ordered data can have fast writes (depends on if clustering key is sequential) fast reads (fastest reads when searching on the clustering key) recommended table structure!
5
General syntax for creating a clustered index: CREATE [UNIQUE] CLUSTERED INDEX ON ( ); i.e. CREATE UNIQUE CLUSTERED INDEX CIX_myTable ON dbo.myTable(id); Best practice is to create a clustered index on a narrow column with static values Try to create a unique clustered index for a little better performance
6
A primary key enforces uniqueness within a table General syntax for creating a primary key: CREATE TABLE [schema].[tableName] ( CONSTRAINT PRIMARY KEY [NONCLUSTERED|CLUSTERED]( )); i.e. CREATE TABLE dbo.myTable ( idintNOT NULL, lastName varchar(20) NOT NULL, firstName varchar(20) NOT NULL CONSTRAINT PK_myTable PRIMARY KEY NONCLUSTERED(id));
7
Pay attention to where you’re placing the primary key! CREATE TABLE dbo.employee ( idintNOT NULL, ssnintNOT NULL PRIMARY KEY, lastName varchar(20) NOT NULL, firstName varchar(20) NOT NULL ); SSNID LastNameFirstName 123-45-67898 CarsonAmanda 212-12-1212 3 SmithJohn 323-23-2323 5 AdamsMike 424-24-2424 1 ThomsonLaura 505-05-0505 4 JohnsonBrian SSNID LastNameFirstName 123-45-67898 CarsonAmanda 212-12-1212 3 SmithJohn 323-23-2323 5 AdamsMike 424-24-2424 1 ThomsonLaura 505-05-0505 4 JohnsonBrian SSNID LastNameFirstName 123-45-67898 CarsonAmanda 200-98-76539 EdwardsFrank 212-12-1212 3 SmithJohn 323-23-2323 5 AdamsMike 424-24-2424 1 ThomsonLaura 505-05-0505 4 JohnsonBrian SSNID LastNameFirstName 123-45-67898 CarsonAmanda 200-98-76539 EdwardsFrank 212-12-1212 3 SmithJohn 323-23-2323 5 AdamsMike 424-24-2424 1 ThomsonLaura 505-05-0505 4 JohnsonBrian New Hire
8
Creating a table with a non-clustered primary key and separate clustered index: CREATE TABLE dbo.employee ( idint Identity(1,1) NOT NULL, ssnint NOT NULL, lastName varchar(20) NOT NULL, firstName varchar(20) NOT NULL CONSTRAINT PK_employee PRIMARY KEY NONCLUSTERED(ssn)); CREATE UNIQUE CLUSTERED INDEX CIX_employee ON dbo.employee(id);
9
Use the most appropriate data type to meet the requirements of the column Use the smallest data type while allowing a little room for growth If the data type is too big, you’re… wasting space wasting IO wasting time
10
Assume you have 1 million records to store: a unique id, a number 1 to 100, and a timestamp CREATE TABLE dbo.myBigTable ( myID INT IDENTITY(1,1), myNumber INT, myDateDATETIME ); Bytes per row: 4 + 4 + 8 + 10 = 26 bytes (myID + myNumber + myDate + overhead) Rows per page: 8,060 / 26 = 310 rows Pages per 1mm rows: 1,000,000 / 310 = 3225 pages
11
We’re still storing 1 million records: CREATE TABLE dbo.mySmallTable ( myID INT IDENTITY(1,1), myNumber TINYINT, myDate SMALLDATETIME ); Bytes per row: 4 + 1 + 4 + 10 = 19 bytes (myID + myNumber + myDate + overhead) Rows per page: 8,060 / 19 = 424 rows Pages per 1mm rows: 1,000,000 / 424 = 2357 pages
12
myBigTable consumed 3,225 pages mySmallTable consumed 2,357 pages mySmallTable used 868 less pages! Space savings of 27% A single IO can now return 114 more rows Performance improvement of 37%
13
Text Book example: Clustered IndexNon-Clustered Index Book Page Appendix Adams, Mike Carson, Amanda Edwards, Frank Johnson, Brian Adams, Mike Carson, Amanda Edwards, Frank Johnson, Brian Roberts, Amanda Smith, John Thomson, Laura Roberts, Amanda Smith, John Thomson, Laura Amanda 2, 5 Brian 4 Frank 3 Amanda 2, 5 Brian 4 Frank 3 John 6 Laura 7 Mike 1 John 6 Laura 7 Mike 1
14
General syntax for creating a non-clustered index: CREATE [UNIQUE] NONCLUSTERED INDEX ON ( ) INCLUDE ( ); i.e. CREATE NONCLUSTERED INDEX IX_employee_ssn ON dbo.employee(ssn) INCLUDE (lastName, firstName); Too may indexes can make writes expensive
15
Included Columns Non-key columns on the leaf level of an index Not used as search criteria in an index SELECT employeeID, lastName, firstName FROM dbo.employee WHERE hireDate < ‘2009-01-01’ CREATE NONCLUSTERED INDEX IX_employee_hireDate ON dbo.employee(hireDate) INCLUDE (lastName, firstName); hireDate = index key employeeID = clustered key pointer lastName, firstName = included column
16
Want free hosting? First person to tweet… "@GoDaddyGuy I want GoDaddy.com hosting!” …will win free hosting for a year!
17
When the logical ordering of pages does not match the physical ordering of pages Both clustered and non-clustered indexes can become fragmented Fragmentation is caused by insert/update/delete operations
18
Page 3 Page 4 Page 5 Page 3 Page 4 Page 5 Page 4 Page 5 Page 6 Page 4 Page 5 Page 6 Page 3 Page 4 Page 742 Page 3 Page 4 Page 742 Page 4 Page 742 Page 5 Page 4 Page 742 Page 5 Page 742 Page 5 Page 6 Page 742 Page 5 Page 6 Insert Page 3 Record 1 Record 2 Record 3 Record 4 Page 5 Page 3 Record 1 Record 2 Record 3 Record 4 Page 5 Page 3 Record 1 Record 2 Page 742 Page 3 Record 1 Record 2 Page 742 Page 4 Record 5 Record 3 Record 4 Page 5 Page 4 Record 5 Record 3 Record 4 Page 5 Insert Page 4 Page 742
19
SQL Server 2000 DBCC ShowContig SQL Server 2005 / 2008 Dynamic Management Views (DMV) sys.dm_db_index_physical_stats Execute after normal business hours or use “Limited” to avoid impacting the environment
20
SQL Server 2000 DBCC IndexDefrag - quicker DBCC dbReindex - more thorough SQL Server 2005 Alter Index {Reorganize | Rebuild} My Index Defrag Script: http://sqlfool.com/2009/03/automated-index-defrag-script/
21
Stored procedures offer: enhanced security better performance easier maintenance less network traffic Best practice for accessing data in SQL Server
22
Select only the columns you actually need Try to use an existing covering index whenever possible, but… Don’t create an index for every query! Place Data Definition (DDL) statements first, then Data Modification (DML) Specify the schema owner (i.e. “dbo”, “sys”) for every referenced object
23
Consider MAXDOP restrictions Consider NOLOCK hints (only if dirty reads are acceptable) Use “SET NOCOUNT ON;” Avoid nested proc calls Keep transactions small
24
Minimize database calls whenever possible When working with static data, retrieve data once and store it in the app (i.e. hash table) When developing high-volume applications: Batches are key! More manageable updates Use table-valued parameters (TVP) or XML to handle bulk operations Staging tables vs single row updates
25
Books Online, http://msdn.microsoft.com/en-us/library/ms130214.aspx http://msdn.microsoft.com/en-us/library/ms130214.aspx SQLServerPedia, http://sqlserverpedia.comhttp://sqlserverpedia.com SQL Server Performance, http://www.sql-server-performance.com http://www.sql-server-performance.com SQL Server Central, http://www.sqlservercentral.com/ http://www.sqlservercentral.com/ My blog! http://sqlfool.com http://sqlfool.com
26
Batch Compiliation, Recompliation, and Plan Caching Issues in SQL Server 2005 http://technet.microsoft.com/en-us/library/cc966425.aspx http://technet.microsoft.com/en-us/library/cc966425.aspx SQL Server 2005 Waits & Queues (Google) Grant Fritchey’s SQL Server Execution Plans Kalen Delaney’s SQL Server 2008 Internals Itzik Ben-Gan’s SQL Server 2008 T-SQL Fundamentals
27
Thank you for attending my presentation! I can be reached at: Michelle Ufford michelle@sqlfool.com http://sqlfool.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.