Connect with life Nauzad Kapadia Quartz Systems [email protected].

12
Connect with life www.connectwithlife.co.in SQL Server Query Tuning Nauzad Kapadia Quartz Systems www.quartzsystems.com [email protected]

Transcript of Connect with life Nauzad Kapadia Quartz Systems [email protected].

Page 1: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

Connect with life

www.connectwithlife.co.in

SQL ServerQuery Tuning

Nauzad KapadiaQuartz Systems

[email protected]

Page 2: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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

Page 3: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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

Page 4: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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

Page 5: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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)

Page 6: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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.

Page 7: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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')

Page 8: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

Indexing – cont’d

Pay attention to column orderDrop or create indexes as needed

Page 9: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

Plan Caching

Adhoc plan caching *Autoparameterization *Forced Parameterization

ALTER DATABASE <database_name> SET PARAMETERIZATION FORCED;

Use Prepared statements *

Page 10: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

Transactions

Keep them shortAvoid external work inside the transaction such as emailTake care to avoid deadlocksUse a appropriate isolation level.

Page 11: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

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

Page 12: Connect with life  Nauzad Kapadia Quartz Systems  nauzadk@quartzsystems.com.

© 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.