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

08 | Retrieving SQL Server Metadata and Improving Query Performance Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft

Embed Size (px)

Citation preview

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

Course Topics

Querying Microsoft SQL Server 2012 Jump Start

05 | 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

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

Module Overview

System Catalogs and DMVs

System catalog views

Built-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;

--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;

Information schema views

Views 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 TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE

FROM 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_USAGE;

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

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

System metadata functions

Return 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;

SELECT @@VERSION AS SQL_Version;

SELECT SERVERPROPERTY('ProductVersion') AS version;

SELECT SERVERPROPERTY('Collation') AS collation;

Querying DMVs and functions

Dynamic 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 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

SELECT referencing_schema_name, referencing_entity_name, referencing_class_descFROM sys.dm_sql_referencing_entities(

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

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

Using System Catalogs and DMVs

Demo

Stored Procedures

Executing stored procedures

Use 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';

--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';

Common system stored procedures

Database 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

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';

--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';

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>;

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

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’

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’

Creating and executing stored procedures

Demo

Improving SQL Server Query Performance

Writing well-performing queries

Only retrieve what you needIn 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

Indexing in SQL Server

SQL 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

SQL Server indexes: performance considerations

Check 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

Distribution statistics

Distribution 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

Avoiding cursors

Cursors 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

Improving query performance

Demo

Monitoring SQL Server Query Performance

What is an execution plan?

Review of the process of executing a query:Parse, resolve, optimize, execute

An 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

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

Viewing graphical execution plans

Enable execution plan viewers in SSMS

Display Estimated Execution Plan

Include Actual Execution Plan

Interpreting the execution plan

Read the plan right to left, top to bottomHover 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

Displaying Query Statistics

SQL 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 TIME ON;

SET STATISTICS IO ON;SET STATISTICS IO ON;

Displaying query performance

Demo

Summary

System 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

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’

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’

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.

©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.