Execution Plans in practice - how to make SQL Server queries faster - Damian Widera

Preview:

Citation preview

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Execution plans in practice – how to

make SQL Server queries faster

Damian Widera

Microsoft Data Platform MVP

EUVIC

@damianwidera

http://sqlblog.com/blogs/damian_widera/default.aspx

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Many thanks to our sponsors & partners!

GOLD

SILVER

PARTNERS

PLATINUM

POWERED BY

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Come to Poland on October 1st!

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Damian Widera

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

EUVIC

PALO ALTO

NOWY JORK

WARSZAWA

KATOWICE

GLIWICE

BIELSKO BIAŁA

WROCŁAW

CZĘSTOCHOWA

GDYNIA

KRAKÓW

BYDGOSZCZ

WIEDEŃ

BIAŁYSTOK

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Customers…

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• What is bad in terms of query execution

• What was bad in previous examples is not always

bad…

• SQL Server 2016 – how could it help?

Today I am going to tell about….

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

SQL Server School of Art

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• A query takes long time to execute

• A query consumes too much resources

• Then we have:

–Reduced concurency

–More locks that should be

• Locks contentions, waits, etc…

What is bad in terms of query execution

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• A query (logical) is parsed and optimized based on

all available information (cost optimization)

• Estimation != Guestimation

• Plan(s) is (are) produced

How it works

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Bad row estimates can impact a variety of decisions including:

• index selection seek vs. scan operations,

• parallel versus serial execution,

• join algorithm selection,

• inner vs. outer physical join selection (e.g. build vs. probe),

• spool generation,

• bookmark lookups vs. full clustered or heap table access,

• stream or hash aggregate selection

Why it is so important?

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• Index or table scans (*): May indicate a need

for better or additional indexes (*)

– THIS IS NOT ALWAYS WRONG!!!!

• Bookmark Lookups: Consider changing the current

clustered index, consider using a covering index, limit the

number of columns in the SELECT statement.

• Filter: Remove any functions in the WHERE clause, don’t

include views in your Transact-SQL code, may need

additional indexes.

What to look for…

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• Sort: Does the data really need to be sorted? Can an index be used to avoid sorting? Can sorting be done at the client more efficiently? „Hidden” sort. Nonlinearoperation

• Spool. Used as a cache in the QP. Reason – lack of indexesor uniqueness. Implemented as a hidden tables in tempdb

• Hash. Used for joins and aggregations. Builds a hashtablein memory (or in tempdb). Means there is no good index.

What to look for…

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

• When index scan is the best option

–Two examples

• High density -> Seek

• Low density -> Scan

• When cursor can be the best option (especially for

SQL Server 2008 R2 and older)

–Running total

What was bad in previous example is not always bad…

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

New functionality that makes DBAs’ happy - Query

Store

will be available in all editions

SQL Server 2016 – how could it help?

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Query data store

Durability latency controlled by DB option

DATA_FLUSH_INTERNAL_SECONDS

Compile

Execute

Plan store

Runtime stats

Query Store

schema

Collects query texts (plus all relevant properties)

Stores all plan choices and performance metrics

Works across restarts / upgrades / recompiles

Dramatically lowers the bar for performance troubleshooting

New Views

Intuitive and easy plan forcing

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Query Store schema explained

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Query Store Dashboard

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Q & A