Top Banner
Practical SQL Query Monitoring and Optimization Identify Data Layer Bottlenecks
25

Practical SQL query monitoring and optimization

May 10, 2015

Download

Data & Analytics

Ivo Andreev

Practical SQL query monitoring and optimization
Today the project owners demand results as soon as possible and most often - for yesterday. Time to market is crucial and it is practical to deliver bit-by-bit, get feedback and grow with the number of your customers. But as the project grows, the team does too and not all have the same expertise. As well rarely in the beginning the requirements clear enough to allow performance-wise SQL interaction. In most cases there does not exist an ORM that can solve this task for you and you will need to have hard T-SQL writer in the team. If you already know this story or are going this way then in this practical session we will share how to monitor, measure and optimize your SQL code and DB layer interaction.

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Practical SQL query monitoring and optimization

Practical SQL Query Monitoring

and Optimization Identify Data Layer Bottlenecks

Page 2: Practical SQL query monitoring and optimization

About me

� Project Manager @

� 10 years professional experience

� Microsoft Certified Specialist

[email protected]

� http://www.linkedin.com/in/ivelin

� Business Interests

� ASP.NET, AJAX, jQuery

� SOA, Integration

� GIS, Mapping

� SQL optimization

2 |

Page 3: Practical SQL query monitoring and optimization

A Well Known Story

Monitor and Measure Application

Analyze Tier Interaction

Getting Performance Hints

Identify Problematic Queries

Optimization Query Performance

Measure the Effect

Maintain

3 |

Page 4: Practical SQL query monitoring and optimization

Warning: A Bottleneck

� Visual Studio Performance Wizard� CPU and Memory Sampling

� Concurrency

� Tier Interaction

� SQL Server Profiler� Create trace

� Analyze trace results

� Replay trace

� Log analyzer� Most frequent and timely queries

� Identify long-running locks (and deadlocks)

� Identify I/O activity

� SQL Solutions Trace Analyzer / Deadlock Detector

Page 5: Practical SQL query monitoring and optimization

Row vs. Chunk at a Time

� Row at a time (RAT) processing� SELECT * FROM Activities WHERE ParentId = 1

� SELECT * FROM Activities WHERE ParentId = 2

� SELECT * FROM Activities WHERE ParentId = 16

� Chunk at a time (CAT) processing� SELECT * FROM Activities WHERE ParentId IN (1,2,16)

� Likely to happen when:

� Components with own DataSource extraction

� Complex code by different developers

Page 6: Practical SQL query monitoring and optimization

DEMO 1

� Row vs. Chunk at a Time

� Analyzing Trace Logs

Page 7: Practical SQL query monitoring and optimization

Getting Help

� Analyze Execution Plan

� Database Tuning Advisor

� Suggests indexes and statistics

� Opt.1: Improvement > 20%

� Opt.2: Improvement < 20%

� Dynamic Management Views

� Missing Indexes (sys.dm_db_missing_index_groups)

� Slowest Queries (sys.dm_exec_query_stats)

Page 8: Practical SQL query monitoring and optimization

DEMO 2

� Tuning Advisor

� Dynamic Management Views

Page 9: Practical SQL query monitoring and optimization

Analyze Query Plan

� Tools

� SQL Server Management Studio

� SQL Sentry: free @ http://www.sqlsentry.com

� Query Plan� Colour-scaled cost information

� Cost filter by CPU, I/O or both

� Better plan layout

� Top Operations

� Tuning Tips

Page 10: Practical SQL query monitoring and optimization

Query Plan Basics

� Table Scan (Heap)� Clustered index missing

� Scans data pages (all data)

� Index Scan� Scans index pages (less data)

� Relies on order of pages

� Index Seek� Highly selective queries (10-15% of rows)

� Indexed Views� Require: Schema binding

� Require: Deterministic functions

Page 11: Practical SQL query monitoring and optimization

DEMO 3

� Query Plan Explorer

� OR vs. UNION

Page 12: Practical SQL query monitoring and optimization

Indexes

� Index types

� Clustered / Non-clustered

� Filtered index(SQL 2008)

� Covering index

� Objective

� Optimal performance

� Reduce slow scans

� Difference in large data sets

Page 13: Practical SQL query monitoring and optimization

Are Indexes Magical ?

� Execute non-indexable queries� SELECT * FROM Employees WHERE CAST(PNumber AS INT) > 1

� SELECT * FROM Customers WHERE ContactName LIKE ‘%a%’

� SELECT * FROM Employees WHERE FName=‘a’ OR LName=‘b’

� Reject indexes because of their overhead

� Choosing Indexes� Measure query impact

� Measure index size

� Maintenance� Fragmentation = poor disk I/O

� Reorganize vs. Rebuild

� Automate: Nightly Job

� Anti-patterns� Create indexes blindly

Page 14: Practical SQL query monitoring and optimization

SQL Server Statistics

� Objective� WHERE clause order

� Statistics on data distribution

� Query Optimizer creates efficient plan

� Up-to-date check� Automatically created for index keys

� Estimated vs. Actual Rows

�Maintenance� DB Default: AUTOCREATE and AUTOUPDATE

� EXEC sp_updatestats

� Automate: Nightly Job

Page 15: Practical SQL query monitoring and optimization

User Defined Functions

� Procedures vs. Functions

� Performance vs. Reusability

� Complex conditions

� Used through the whole application

� i.e. IsCaseClosed

� Chunk at a Time

� Table valued parameters (SQL 2008 and on)

� Pass result from one function to next

Page 16: Practical SQL query monitoring and optimization

Optimize Locks

�Data Integrity

� Data commit waits

�NOLOCK

� Prevent Deadlocks

� Returns uncommitted data

� Incorrect results

� Less/More rows could be returned

� Update one entity, retrieve another

� Index page splits under heavy load(GUID)

Page 17: Practical SQL query monitoring and optimization

DEMO 4

� The NOLOCK Problem

Page 18: Practical SQL query monitoring and optimization

Common Table Expressions (CTE)

� Alias of a query (SQL 2005)

� Advantages

� Readability and reusability

� Implement recursive query

� Materializes when used

� Uses indexes (Table v@r not)

� Disadvantages� Can be used only once

� Must be used immediately

Page 19: Practical SQL query monitoring and optimization

Other

� UNION vs. UNION ALL

� Data Types� Do you really need BIGINT?

� Requires storage

� Requires time to read

� Requires memory

BIGINT INT Delta (MB) Delta (%)

Primary Key 1267 977 290 23%

By_PropID_CountryID 813 741 72 9%

By_String 303 230 73 24%

Page 20: Practical SQL query monitoring and optimization

SQL Resource Governor

� Shared DB Server

� Prioritize queries� Reporting vs. Dashboard

� Memory, CPU

� Connections, Parallelism

� RG Configuration� Pools

� Workload Groups

� Custom classification function

� Available in SQL Server Enterprise

Page 21: Practical SQL query monitoring and optimization

Test Under Load

� Life’s complicated

� Concurrent queries

� Multiple user

� Cached Buffers

� Tools

� SQL Server Profiler Replay

� SQL Server 2012 Distributed Replay� Snap In @ http://dreplaygui.codeplex.com/

� SQL Load Generator� http://sqlloadgenerator.codeplex.com/

Page 22: Practical SQL query monitoring and optimization

Summary

� Steps

� Capture/Analysis/Optimization

� Measure the improvement

� Maintain

� Remember

� Reoccurring improvement

� Ideally you should analyze all SQL

� Changes can affect SQL performance� in data size

� result set size

Page 23: Practical SQL query monitoring and optimization

DEMO 5

� Trace Replay

� SQL Load Generator

� Resource Governor

Page 24: Practical SQL query monitoring and optimization

Other Approaches

� Caching� Application (Static)

� Session

� Context

� WCF Session

� Entity Framework Caching

� Paging

� Lazy loading

� Parallel binding

� Historical / Live data tables

Page 25: Practical SQL query monitoring and optimization

SQL Saturday #152 Sponsors