Connect with life
www.connectwithlife.co.in
SQL ServerQuery Tuning
Nauzad KapadiaQuartz Systems
Session Objectives And Key Takeaways
Session Objectives: Dive into the query performance and tuning capabilities in SQL ServerDemonstrate common tuning and troubleshooting techniques using SQL ServerDisclaimer: Query Tuning is a deep & broad topic – won’t cover everything
Key Takeaways:SQL Server 2005+ has wealth of capabilitiesGet a refresher on certain fundamental conceptsCommon pitfalls to avoid and optimization techniques
Factors Affecting Performance?
Application Design3 tier architecture facilitates caching, connection pooling.Partitioning tables and databasesNot mixing logs and database on the same disk
Database DesignNormalization of database structure, choosing indexes.
Microsoft SQL Server Setup.Affinity mask, lightweight pooling, max worker threads and degree of parallelism.
Hardware
IndexesScan and Seek *Clustered and non-clustered indexesSeekable Predicates *
Single column IndexesMulti Column Indexes
Covered ColumnsA Clustered index covers all columnsA non-clustered index covers only key columnsA non-clustered index on a table with a clustered index covers the clustered index key
Example : Covered ColumnsCREATE TABLE T_heap (a int, b int, c int, d int, e int, f int)CREATE INDEX T_heap_a ON T_heap (a)CREATE INDEX T_heap_bc ON T_heap (b, c)CREATE INDEX T_heap_d ON T_heap (d) INCLUDE (e)CREATE UNIQUE INDEX T_heap_f ON T_heap (f)
CREATE TABLE T_clu (a int, b int, c int, d int, e int, f int)CREATE UNIQUE CLUSTERED INDEX T_clu_a ON T_clu (a)CREATE INDEX T_clu_b ON T_clu (b)CREATE INDEX T_clu_ac ON T_clu (a, c)CREATE INDEX T_clu_d ON T_clu (d) INCLUDE (e)CREATE UNIQUE INDEX T_clu_f ON T_clu (f)
Indexing
Bookmark Lookups *Bookmark lookups are expensive
Index Cost *Only one clustered index allowed per tableA Clustered index is always unique even if it is not explicitly specified so.Create a PRIMARY KEY by adding a NONCLUSTERED keyword.Make non-clustered indexes highly selectable.
Indexing – cont’d
Index small fields.Avoid creating indexes on large string columns
ALTER TABLE titles ADD hash_title AS CHECKSUM(title)
CREATE INDEX hash_index ON titles(hash_title)
SELECT * FROM titles WHERE title = 'Cooking with Computers: Surreptitious Balance Sheets'
SELECT * FROM titles WHERE title = 'Cooking with Computers: Surreptitious Balance Sheets'AND hash_title = CHECKSUM('Cooking with Computers: Surreptitious Balance Sheets')
Indexing – cont’d
Pay attention to column orderDrop or create indexes as needed
Plan Caching
Adhoc plan caching *Autoparameterization *Forced Parameterization
ALTER DATABASE <database_name> SET PARAMETERIZATION FORCED;
Use Prepared statements *
Transactions
Keep them shortAvoid external work inside the transaction such as emailTake care to avoid deadlocksUse a appropriate isolation level.
Best Practices
LEARN T-SQLAvoid Cursors as far as possibleDo you have missing indexes – Take the help of DTAAvoid joining tables on different data typesFor frequent computations create a calculated column
It helps in column statistics which SQL Server can use in optimizing the query.
Don’t use unqualified object names
© 2006 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation
as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES,
EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.