08 - retrieving sql metadata and improving sql performance

Upload: chowdhury-golam-kibria

Post on 04-Nov-2015

227 views

Category:

Documents


0 download

DESCRIPTION

How to retrieve SQL metadata and how to optimize SQL performance.

TRANSCRIPT

  • 08 | Retrieving SQL Server Metadata and Improving Query Performance

  • 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

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

    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_NAME

    FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGE;

    SELECT VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME,TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,COLUMN_NAME

    FROM INFORMATION_SCHEMA.VIEW_COLUMN_USAGEWHERE COLUMN_NAME = BusinessEntityID

  • SELECT @@VERSION AS SQL_Version;

    SELECT SERVERPROPERTY('ProductVersion') AS version;

    SELECT SERVERPROPERTY('Collation') AS collation;

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

    SELECT referencing_schema_name,referencing_entity_name,referencing_class_desc

    FROM sys.dm_sql_referencing_entities('Sales.SalesOrderHeader', 'OBJECT');

    GO

  • 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

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

  • 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

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

  • CREATE 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

  • Table scan: SQL Server reads all table rows

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

  • Viewing graphical execution plans

    Display Estimated Execution Plan

    Include Actual Execution Plan

  • SET STATISTICS TIME ON;

    SET STATISTICS IO ON;

  • 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

  • Writing 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.