Contents

About the Author

About the Technical Reviewer

Acknowledgments

Introduction

image Chapter 1: SQL Query Performance Tuning

The Performance Tuning Process

The Core Process

Iterating the Process

Performance vs. Price

Performance Targets

“Good Enough” Tuning

Performance Baseline

Where to Focus Efforts

SQL Server Performance Killers

Poor Indexing

Inaccurate Statistics

Poor Query Design

Poor Execution Plans

Excessive Blocking and Deadlocks

Non-Set-Based Operations

Poor Database Design

Excessive Fragmentation

Nonreusable Execution Plans

Frequent Recompilation of Queries

Improper Use of Cursors

Improper Configuration of the Database Log

Excessive Use or Improper Configuration of tempdb

Summary

image Chapter 2: System Performance Analysis

Performance Monitor Tool

Dynamic Management Objects

Hardware Resource Bottlenecks

Identifying Bottlenecks

Bottleneck Resolution

Memory Bottleneck Analysis

SQL Server Memory Management

Available Bytes

Pages/sec and Page Faults/sec

Paging File %Usage and Page File %Usage

Buffer Cache Hit Ratio

Page Life Expectancy

Checkpoint Pages/sec

Lazy writes/sec

Memory Grants Pending

Target Server Memory (KB) and Total Server Memory (KB)

Additional Memory Monitoring Tools

DBCC Memorystatus

Dynamic Management Objects

Memory Bottleneck Resolutions

Optimizing Application Workload

Allocating More Memory to SQL Server

Increasing System Memory

Changing from a 32-bit to a 64-bit Processor

Data Compression

Enabling 3GB of Process Address Space

Disk Bottleneck Analysis

Disk Counters

% Disk Time

Current Disk Queue Length

Disk Transfers/sec

Disk Bytes/sec

Avg. Disk Sec/Read and Avg. Disk Sec/Write

Additional I/O Monitoring Tools

Sys.dm_io_virtual_file_stats

Sys.dm_os_wait_stats

Disk Bottleneck Resolutions

Optimizing Application Workload

Using a Faster I/O Path

Using a RAID Array

Raid 0

Raid 1

Raid 5

Raid 6

RAID 1+0 (RAID 10)

Using a SAN System

Using SSD Disks

Aligning Disks Properly

Adding System Memory

Creating Multiple Files and Filegroups

Placing the Table and Index on Separate Disks

Moving the Log Files to a Separate Physical Disk

Partitioning Tables

Processor Bottleneck Analysis

% Processor Time

% Privileged Time

Processor Queue Length

Context Switches/sec

Batch Requests/sec

SQL Compilations/sec

SQL Recompilations/sec

Other Tools for Measuring CPU Performance

Sys.dm_os_wait_stats

Sys.dm_os_workers and Sys.dm_os_schedulers

Processor Bottleneck Resolutions

Optimizing Application Workload

Eliminating Excessive Compiles/Recompiles

Using More or Faster Processors

Using a Large L2/L3 Cache

Running More Efficient Controllers/Drivers

Not Running Unnecessary Software

Network Bottleneck Analysis

Bytes Total/sec

% Net Utilization

Network Bottleneck Resolutions

Optimizing Application Workload

SQL Server Overall Performance

Missing Indexes

Database Blocking

Nonreusable Execution Plans

General Behavior

User Connections

Batch requests/sec

Considerations for Monitoring Virtual Machines

Creating a Baseline

Creating a Reusable List of Performance Counters

Creating a Counter Log Using the List of Performance Counters

Performance Monitor Considerations

System Behavior Analysis Against Baseline

Summary

image Chapter 3: SQL Query Performance Analysis

Extended Events Wizard

Extended Events Sessions

Events

Global Fields

Event Filters

Data Storage

Finishing the Wizard and Starting the Session

Extended Events Automation

Creating a Session Script Using the GUI

Defining a Session Using Stored Procedures

Extended Events Recommendations

Set Max File Size Appropriately

Avoid Debug Events

Partition Memory in the Sessions

Avoid Use of No_Event_Loss

Other Methods for Query Performance Metrics

Costly Queries

Identifying Costly Queries

Costly Queries with a Single Execution

Costly Queries with Multiple Executions

Identifying Slow-Running Queries

Execution Plans

Analyzing a Query Execution Plan

Identifying the Costly Steps in an Execution Plan

Analyzing Index Effectiveness

Analyzing Join Effectiveness

Hash Join

Actual vs. Estimated Execution Plans

Plan Cache

Query Cost

Client Statistics

Execution Time

Statistics Io

Summary

image Chapter 4: Index Analysis

What Is an Index?

The Benefit of Indexes

Index Overhead

Index Design Recommendations

Examine the WHERE Clause and JOIN Criteria Columns

Use Narrow Indexes

Examine Column Uniqueness

Examine the Column Data Type

Consider Column Order

Consider the Type of Index

Clustered Indexes

Heap Tables

Relationship with Nonclustered Indexes

Clustered Index Recommendations

When to Use a Clustered Index

Poor Design Practices for a Clustered Index

Nonclustered Indexes

Nonclustered Index Maintenance

Defining the Bookmark Lookup

Nonclustered Index Recommendations

Clustered vs. Nonclustered Indexes

Benefits of a Clustered Index over a Nonclustered Index

Benefits of a Nonclustered Index over a Clustered Index

Advanced Indexing Techniques

Covering Indexes

A Pseudoclustered Index

Recommendations

Index Intersections

Index Joins

Filtered Indexes

Indexed Views

Index Compression

ColumnStore Indexes

Special Index Types

Full-Text

Spatial

XML

Additional Characteristics of Indexes

Different Column Sort Order

Index on Computed Columns

Index on BIT Data Type Columns

CREATE INDEX Statement Processed As a Query

Parallel Index Creation

Online Index Creation

Considering the Database Engine Tuning Advisor

Summary

image Chapter 5: Database Engine Tuning Advisor

Database Engine Tuning Advisor Mechanisms

Database Engine Tuning Advisor Examples

Tuning a Query

Tuning a Trace Workload

Tuning from the Procedure Cache

Database Engine Tuning Advisor Limitations

Summary

image Chapter 6: Lookup Analysis

Purpose of Lookups

Drawbacks of Lookups

Analyzing the Cause of a Lookup

Resolving Lookups

Using a Clustered Index

Using a Covering Index

Using an Index Join

Summary

image Chapter 7: Statistics Analysis

The Role of Statistics in Query Optimization

Statistics on an Indexed Column

Benefits of Updated Statistics

Drawbacks of Outdated Statistics

Statistics on a Nonindexed Column

Benefits of Statistics on a Nonindexed Column

Drawback of Missing Statistics on a Nonindexed Column

Analyzing Statistics

Density

Statistics on a Multicolumn Index

Statistics on a Filtered Index

Statistics Maintenance

Automatic Maintenance

Auto Create Statistics

Auto Update Statistics

Auto Update Statistics Asynchronously

Manual Maintenance

Manage Statistics Settings

Generate Statistics

Statistics Maintenance Status

Status of Auto Create Statistics

Status of Auto Update Statistics

Analyzing the Effectiveness of Statistics for a Query

Resolving a Missing Statistics Issue

Resolving an Outdated Statistics Issue

Recommendations

Backward Compatibility of Statistics

Auto Create Statistics

Auto Update Statistics

Automatic Update Statistics Asynchronously

Amount of Sampling to Collect Statistics

Summary

image Chapter 8: Fragmentation Analysis

Causes of Fragmentation

Page Split by an UPDATE Statement

Page Split by an INSERT Statement

Fragmentation Overhead

Analyzing the Amount of Fragmentation

Analyzing the Fragmentation of a Small Table

Fragmentation Resolutions

Dropping and Re-creating the Index

Re-creating the Index with the DROP_EXISTING Clause

Executing the ALTER INDEX REBUILD Statement

Executing the ALTER INDEX REORGANIZE Statement

Significance of the Fill Factor

Automatic Maintenance

Summary

image Chapter 9: Execution Plan Cache Analysis

Execution Plan Generation

Parser

Binding

Optimization

Execution Plan Caching

Components of the Execution Plan

Query Plan

Execution Context

Aging of the Execution Plan

Analyzing the Execution Plan Cache

Execution Plan Reuse

Ad Hoc Workload

Prepared Workload

Plan Reusability of an Ad Hoc Workload

Plan Reusability of a Prepared Workload

Parameter Sniffing

Query Plan Hash and Query Hash

Execution Plan Cache Recommendations

Explicitly Parameterize Variable Parts of a Query

Create Stored Procedures to Implement Business Functionality

Code with sp_executesql to Avoid Stored Procedure Maintenance

Implement the Prepare/Execute Model to Avoid Resending a Query String

Avoid Ad Hoc Queries

Prefer sp_executesql over EXECUTE for Dynamic Queries

Parameterize Variable Parts of Queries with Care

Do Not Allow Implicit Resolution of Objects in Queries

Summary

image Chapter 10: Query Recompilation

Benefits and Drawbacks of Recompilation

Identifying the Statement Causing Recompilation

Analyzing Causes of Recompilation

Schema or Bindings Changes

Statistics Changes

Deferred Object Resolution

SET Options Changes

Execution Plan Aging

Explicit Call to sp_recompile

Explicit Use of RECOMPILE

Avoiding Recompilations

Don’t Interleave DDL and DML Statements

Avoiding Recompilations Caused by Statistics Change

Using the KEEPFIXED PLAN Option

Disable Auto Update Statistics on the Table

Using Table Variables

Avoiding Changing SET Options Within a Stored Procedure

Using OPTIMIZE FOR Query Hint

Using Plan Guides

Summary

image Chapter 11: Query Design Analysis

Query Design Recommendations

Operating on Small Result Sets

Limit the Number of Columns in select_list

Use Highly Selective WHERE Clauses

Using Indexes Effectively

Avoid Nonsargable Search Conditions

BETWEEN vs. IN/OR

Avoid Arithmetic Operators on the WHERE Clause Column

Avoid Functions on the WHERE Clause Column

Avoiding Optimizer Hints

JOIN Hint

INDEX Hints

Using Domain and Referential Integrity

NOT NULL Constraint

Declarative Referential Integrity

Avoiding Resource-Intensive Queries

Avoid Data Type Conversion

Use EXISTS over COUNT(*) to Verify Data Existence

Use UNION ALL Instead of UNION

Use Indexes for Aggregate and Sort Conditions

Avoid Local Variables in a Batch Query

Be Careful When Naming Stored Procedures

Reducing the Number of Network Round-Trips

Execute Multiple Queries Together

Use SET NOCOUNT

Reducing the Transaction Cost

Reduce Logging Overhead

Reduce Lock Overhead

Summary

image Chapter 12: Blocking Analysis

Blocking Fundamentals

Understanding Blocking

Atomicity

Consistency

Isolation

Durability

Locks

Lock Granularity

Row-Level Lock

Page-Level Lock

Extent-Level Lock

Heap or B-tree Lock

Table-Level Lock

Database-Level Lock

Lock Operations and Modes

Lock Escalation

Lock Modes

Exclusive (X) Mode

Intent Shared (IS), Intent Exclusive (IX), and Shared with Intent Exclusive (SIX) Modes

Schema Modification (Sch-M) and Schema Stability (Sch-S) Modes

Bulk Update (BU) Mode

Key-range Mode

Lock Compatibility

Isolation Levels

Read Uncommitted

Read Committed

Repeatable Read

Serializable

Snapshot

Effect of Indexes on Locking

Effect of a Nonclustered Index

Effect of a Clustered Index

Effect of Indexes on the Serializable Isolation Level

Capturing Blocking Information

Capturing Blocking Information with SQL

Extended Events and the blocked_process_report Event

Blocking Resolutions

Optimize the Queries

Decrease the Isolation Level

Partition the Contended Data

Recommendations to Reduce Blocking

Automation to Detect and Collect Blocking Information

Summary

image Chapter 13: Deadlock Analysis

Deadlock Fundamentals

Choosing the Deadlock Victim

Using Error Handling to Catch a Deadlock

Deadlock Analysis

Collecting Deadlock Information

Analyzing the Deadlock

Avoiding Deadlocks

Accessing Resources in the Same Physical Order

Decreasing the Number of Resources Accessed

Convert a Nonclustered Index to a Clustered Index

Use a Covering Index for a SELECT Statement

Minimizing Lock Contention

Summary

image Chapter 14: Cursor Cost Analysis

Cursor Fundamentals

Cursor Location

Cursor Concurrency

Cursor Types

Cursor Cost Comparison

Cost Comparison on Cursor Location

Cost Comparison on Cursor Concurrency

Read-Only

Cost Comparison on Cursor Type

Default Result Set

Benefits

Multiple Active Result Sets

Drawbacks

Cursor Overhead

Analyzing Overhead with T-SQL Cursors

Cursor Recommendations

Summary

image Chapter 15: Database Performance Testing

Database Performance Testing

A Repeatable Process

Distributed Replay

Capturing Data with the Server Side Trace

Distributed Replay for Database Testing

Configuring the Client

Running the Distributed Tests

Conclusion

image Chapter 16: Database Workload Optimization

Workload Optimization Fundamentals

Workload Optimization Steps

Sample Workload

Capturing the Workload

Analyzing the Workload

Identifying the Costliest Query

Determining the Baseline Resource Use of the Costliest Query

Overall Resource Use

Detailed Resource Use

Analyzing and Optimizing External Factors

Analyzing the Connection Options Used by the Application

Analyzing the Effectiveness of Statistics

Analyzing the Need for Defragmentation

Analyzing the Internal Behavior of the Costliest Query

Analyzing the Query Execution Plan

Identifying the Costly Steps in the Execution Plan

Analyzing the Processing Strategy

Optimizing the Costliest Query

Modifying an Existing Index

Analyzing the Application of a Join Hint

Avoiding the Clustered Index Scan Operation

Modifying the Procedure

Analyzing the Effect on Database Workload

Iterating Through Optimization Phases

Summary

image Chapter 17: SQL Server Optimization Checklist

Database Design

Balancing Under- and Overnormalization

Benefiting from Entity-Integrity Constraints

Benefiting from Domain and Referential Integrity Constraints

Adopting Index-Design Best Practices

Avoiding the Use of the sp_ Prefix for Stored Procedure Names

Minimizing the Use of Triggers

Query Design

Use the Command SET NOCOUNT ON

Explicitly Define the Owner of an Object

Avoid Nonsargable Search Conditions

Avoid Arithmetic Expressions on the WHERE Clause Column

Avoid Optimizer Hints

Stay Away from Nesting Views

Ensure No Implicit Data Type Conversions

Minimize Logging Overhead

Adopt Best Practices for Reusing Execution Plans

Adopt Best Practices for Database Transactions

Eliminate or Reduce the Overhead of Database Cursors

Configuration Settings

Affinity Mask

Memory Configuration Options

Cost Threshold for Parallelism

Max Degree of Parallelism

Optimize for Ad Hoc Workloads

Query Governor Cost Limit

Fill Factor (%)

Blocked Process Threshold

Database File Layout

Database Compression

Database Administration

Keep the Statistics Up-to-Date

Maintain a Minimum Amount of Index Defragmentation

Cycle the SQL Error Log File

Avoid Database Functions Such As AUTO_CLOSE or AUTO_SHRINK

Database Backup

Incremental and Transaction Log Backup Frequency

Backup Distribution

Backup Compression

Summary

image Index

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.118.103