22
@ITCAMPRO #ITCAMP16 Community 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

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

  • Upload
    itcamp

  • View
    842

  • Download
    1

Embed Size (px)

Citation preview

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

@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

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Many thanks to our sponsors & partners!

GOLD

SILVER

PARTNERS

PLATINUM

POWERED BY

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Come to Poland on October 1st!

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Damian Widera

Page 5: Execution Plans in practice - how to make SQL Server queries faster - 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

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Customers…

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

@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….

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

SQL Server School of Art

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

@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

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

@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

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

@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?

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

@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…

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

@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…

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

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

@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…

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

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

@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?

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

@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

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Query Store schema explained

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Query Store Dashboard

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

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

@ITCAMPRO #ITCAMP16Community Conference for IT Professionals

Q & A