brian alderman | mct, ceo / founder of microtechpoint

35
Click to edit Master subtitle style 08 | Retrieving SQL Server Metadata and Improving Query Performance Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager

Upload: presta

Post on 22-Feb-2016

51 views

Category:

Documents


0 download

DESCRIPTION

08 | Retrieving SQL Server Metadata and Improving Query Performance . Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager. Course Topics. Querying system c atalogs and DMVs Creating and executing s tored procedures - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Click to edit Master subtitle style08 | Retrieving SQL Server

Metadata and Improving Query Performance

Brian Alderman | MCT, CEO / Founder of MicroTechPointTobias Ternstrom | Microsoft SQL Server Program Manager

Page 2: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Course TopicsQuerying Microsoft SQL Server 2012 Jump Start05 | SET Operators, Windows Functions, and Grouping SET operators, Windows functions, GROUPING sets (PIVOT, UNPIVOT, CUBE, ROLLUP)

06 | Modifying Data INSERT, UPDATE, and DELETE statements, use of defaults, constraints, and triggers, OUTPUT

07 | Programming with T-SQL Using T-SQL programming elements, implementing error handling, understanding and implementing transactions

08 | Retrieving SQL Server Metadata and Improving Query PerformanceQuerying system catalogs and dynamic management views, creating and executing stored procedures, improving SQL Server query performance

Page 3: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Querying system catalogs and DMVsCreating and executing stored proceduresImproving SQL Server query performanceMonitoring SQL Server performance

Module Overview

Page 4: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

System Catalogs and DMVs

Page 5: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

System catalog viewsBuilt-in views that provide information about the system catalogUse standard query methods to return metadata

Column lists, JOIN, WHERE, ORDER BYSome views are filtered to display only user objects, some views include system objects

--Pre-filtered to exclude system objectsSELECT name, object_id, schema_id, type, type_descFROM sys.tables;

--Includes system and user objectsSELECT name, object_id, schema_id, type, type_descFROM sys.objects;

Page 6: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Information schema viewsViews stored in the INFORMATION_SCHEMA system schemaReturn system metadata per ISO standard, used by third-party toolsMaps standard names (catalog, domain) to SQL Server names (database, user-defined data type)SELECT TABLE_CATALOG, TABLE_SCHEMA,

TABLE_NAME, TABLE_TYPEFROM INFORMATION_SCHEMA.TABLES;

SELECT VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAMEFROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE;

SELECT VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAMEFROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGEWHERE COLUMN_NAME = ‘BusinessEntityID’

Page 7: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

System metadata functionsReturn information about settings, values, and objects in SQL ServerCome in a variety of formats

Some marked with a @@ prefix, sometimes incorrectly referred to as global variables: @@VERSIONSome marked with a () suffix, similar to arithmetic or string functions: ERROR_NUMBER()Some special functions marked with a $ prefix: $PARTITION

Queried with a standard SELECT statement:SELECT @@VERSION AS SQL_Version;

SELECT SERVERPROPERTY('ProductVersion') AS version;

SELECT SERVERPROPERTY('Collation') AS collation;

Page 8: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Querying DMVs and functionsDynamic management views are queried like standard views:

Dynamic management functions are queried as table-valued functions, including parameters:

SELECT session_id, login_time, program_nameFROM sys.dm_exec_sessionsWHERE is_user_process = 1;

SELECT referencing_schema_name, referencing_entity_name, referencing_class_descFROM sys.dm_sql_referencing_entities(

'Sales.SalesOrderHeader', 'OBJECT');GO

Page 9: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

About dynamic management objectsThe nearly 200 dynamic management views (DMVs) and functions return server state informationDMVs include catalog information as well as administrative status information, such as object dependenciesDMVs are server-scoped (instance-level) or database-scopedRequires VIEW SERVER STATE or VIEW DATABASE STATE permission to query DMVsUnderlying structures change over time, so avoid writing SELECT * queries against DMVsCategories include;

Naming pattern Description

db Database-related information

exec Query execution-related information

io I/O statistics

os SQL Server Operating System (SQLOS) information

tran Transaction-related information

Page 10: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Using System Catalogs and DMVs

Demo

Page 11: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Stored Procedures

Page 12: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Executing stored proceduresUse the EXECUTE or EXEC command before the name of the stored procedurePass parameters by position or name, separated by commas when applicable

--no parameters so lists all databaseEXEC sys.sp_databases;

--single parameter of name of table EXEC sys.sp_help N'Sales.Customer';

--multiple named parametersEXEC sys.sp_tables

@table_name = '%', @table_owner = N'Sales';

Page 13: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Common system stored proceduresDatabase engine procedures can provide general metadata

sp_help, sp_helplanguagesp_who, sp_lock

Catalog procedures can be used as an alternative to system catalog views and functions:

Unlike system views, there is no option to select which columns to return

Name Description

sp_databases Lists databases in an instance of SQL Server

sp_tables Returns a list of tables or views, except synonyms

sp_columns Returns column information for the specified objects

Page 14: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Executing system stored proceduresSystem stored procedures:

Marked with an sp_ prefixStored in a hidden resource database Logically appear in the sys schema of every user and system database

Best practices for execution include: Always use EXEC or EXECUTE rather than just calling by nameInclude the sys schema name when executingName each parameter and specify its appropriate data type--This example uses EXEC, includes the sys schema name, --and passes the table name as a named Unicode parameter --to a procedure accepting an NVARCHAR(776)--input parameter.EXEC sys.sp_help @objname = N'Sales.Customer';

Page 15: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Creating procedures that return rowsStored procedures can be wrappers for simple or complex

SELECT statementsProcedures may include input and output parameters as

well as return valuesUse CREATE PROCEDURE statement:

Change procedure with ALTER PROCEDURE statementNo need to drop, recreate

CREATE PROCEDURE <schema_name.proc_name>(<parameter_list) ASSELECT <body of SELECT statement>;

Page 16: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Creating procedures that accept parametersInput parameters passed to procedure logically behave like local variables within procedure codeAssign name with @prefix, data type in procedure headerRefer to parameter in body of procedure

CREATE PROCEDURE Production.ProdsByProductLine(@numrows AS int, @ProdLine AS nchar) ASSELECT TOP(@numrows) ProductID,

Name, ListPriceFROM Production.ProductWHERE ProductLine = @ProdLine;

--Retrieve top 50 products with product line = MEXEC Production.ProdsByProductLine 50, ‘M’

Page 17: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Creating and executing stored procedures

Demo

Page 18: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Improving SQL Server Query Performance

Page 19: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Writing well-performing queriesOnly retrieve what you need

In the SELECT clause, only use needed columns – avoid *Use a WHERE clause, filter to return only needed rows

Improve search performance of WHERE clauseAvoid expressions that manipulate columns in the predicate

Minimize use of temporary tables or table variablesUse windowing functions or other set-based operations when possible

Avoid cursors and other iterative approachesWork with your DBA to arrange good indexes to support filters, joins, and orderingLearn how to address tasks with different query approaches to compare performance

Page 20: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Indexing in SQL ServerSQL Server accesses data by using indexes or by scanning all rows in a tableIndexes also supports ordering operations such as grouping, joining, and ORDER BY clauses

Table scan: SQL Server reads all table rows

Index seek/scan: SQL Server uses indexes to find rows

Page 21: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

SQL Server indexes: performance considerationsCheck query execution plans to see if indexes are present and being used as expected

For query writers who are not DBAs or database developers, the ability to recognize problems with indexes, such as the use of table scans when you expect an index to be used, can be very helpful in tuning an application

Page 22: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Distribution statisticsDistribution statistics describe the distribution and the uniqueness, or selectivity, of dataStatistics, by default, are created and updated automaticallyStatistics are used by the query optimizer to estimate the selectivity of data, including the size of the resultsLarge variances between estimated and actual values might indicate a problem with the estimates, which may be addressed through updating statistics

Page 23: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Avoiding cursorsCursors contradict the relational model, which operates on setsCursors typically require more code than set-based approachCursors typically incur more overhead during execution than a comparable set-based operationAlternatives to cursors:

Windowing functionsAggregate functions

Appropriate uses for cursors:Generating dynamic SQL codePerforming administrative tasks

Page 24: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Improving query performanceDemo

Page 25: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Monitoring SQL Server Query Performance

Page 26: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

What is an execution plan?Review of the process of executing a query:

Parse, resolve, optimize, executeAn execution plan includes information on which tables to access, which indexes, what joins to perform

If statistics exist for a relevant column or index, then the optimizer will use them in its calculations

SQL Server tools provide access to execution plans to show how a query was executed or how it would be executed

Plans available in text format (deprecated), XML format, and graphical renderings of XML

Plan viewer accessible in results pane of SSMS

Page 27: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Actual and estimated execution plansExecution plans graphically represent the methods that SQL Server uses to execute the statements in a T-SQL querySSMS provides access to two forms of execution plans:

Estimated execution plans do not execute the query. Instead, they display the plan that SQL Server would likely use if the query were run.Actual execution plans are returned the next time the query is executed. They display the plan that was actually used by SQL Server

Page 28: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Viewing graphical execution plansEnable execution plan viewers in SSMS

Display Estimated Execution Plan

Include Actual Execution Plan

Page 29: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Interpreting the execution planRead the plan right to left, top to bottom

Hover the mouse pointer over an item to see additional information

Percentages indicate cost of operator relative to total queryThickness of lines between operators indicates relative number of rows passing throughFor issues, look for thick lines leading into high-cost operatorsIn an actual execution plan, note any differences between estimated and actual values

Large variances may indicate problems with estimates

Page 30: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Displaying Query StatisticsSQL Server provides detailed runtime information about the execution of a querySTATISTICS TIME will show time spent parsing and compiling a query

STATISTICS IO will show amount of disk activity generated by a query

SET STATISTICS TIME ON;

SET STATISTICS IO ON;

Page 31: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

Displaying query performance

Demo

Page 32: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

SummarySystem catalog views are built-in views that provide information about the system catalog and are used to return metadata using a standard query method

INFORMATION_SCHEMA views store ISO standard system schema that can be used by third-party tools to determine information about the system. The standard names (catalog, domain) map to SQL Server names (database, user-defined data type)Return system metadata per ISO standard, used by third-party tools

System metadata functions return information about settings, values, and objects in SQL Server that can be queried with standard SELECT statements

Dynamic management objects are dynamic management views (DMVs) and functions that return server state information

Page 33: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

SummaryStored procedures are run using the EXECUTE or EXEC command When applicable you can pass parameters by name or position with each parameter separated by a commaSystem stored procedures have an sp_ prefix and are stored in a hidden system database called resource. They logically appear in the sys schema of every user and system databaseYou can create stored procedures that accept parameters and returns rows of data

CREATE PROCEDURE Production.ProdsByProductLine(@numrows AS int, @ProdLine AS nchar) ASSELECT TOP(@numrows) ProductID,

Name, ListPriceFROM Production.ProductWHERE ProductLine = @ProdLine;

--Retrieve top 50 products with product line = MEXEC Production.ProdsByProductLine 50, ‘M’

Page 34: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

SummaryWriting well-performing queries will improve your SQL Server performance. Improvements can be made by only retrieving the data you need which means specify the exact columns you want returned instead of using *, and also use the WHERE clause to return only the rows you need

Be sure to understand the benefits of indexing and create indexes that support filters, joins, and ordering. If possible avoid using cursors and other iterative approaches

Utilize execution plans to view information on which tables to access, which indexes to use, what joins to perform. Execution plans provide a graphical representation of the methods that SQL Server uses to execute a T-SQL query. View these plans from right to left, and top to bottom and view additional information by hovering your mouse over items displayed in the plan.

Page 35: Brian Alderman | MCT, CEO / Founder of MicroTechPoint

©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics 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.