module_8 retrieving sql metadata and improving sql performanceod1.pptx

Upload: princessgemali

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    1/35

    08 | Retrieving SQL Server Metadata

    and Improving Query Performance

    Brian Alderman | MCT, CEO / Founder of MicroTechPoint

    Tobias Ternstrom | Microsoft SQL Server Program Manager

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    2/35

    Querying Microsoft SQL Server 2012 Jump Start

    05 | SET Operators, Windows Functions, and GroupingSET operators, Windows functions, GROUPING sets (PIVOT, UNPIVOT, CUBE, ROLLUP)

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

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

    08 | Retrieving SQL Server Metadata and Improving Query Performance

    Querying system catalogs and dynamic management views creating and executing stored procedures improving SQL

    Server query performance

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    3/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    4/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    5/35

    --Pre-filtered to exclude system objects

    SELECT name,object_id,schema_id,type,type_desc

    FROMsys.tables;

    --Includes system and user objects

    SELECTname,object_id,schema_id,type,type_descFROMsys.objects;

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    6/35

    Information schema views

    SELECTTABLE_CATALOG, TABLE_SCHEMA,

    TABLE_NAME, TABLE_TYPE

    FROM INFORMATION_SCHEMA.TABLES;

    SELECTVIEW_CATALOG,VIEW_SCHEMA,VIEW_NAME,

    TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,

    COLUMN_NAME

    FROMINFORMATION_SCHEMA.VIEW_COLUMN_USAGE;

    SELECTVIEW_CATALOG,VIEW_SCHEMA,VIEW_NAME,

    TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,

    COLUMN_NAME

    FROMINFORMATION_SCHEMA.VIEW_COLUMN_USAGE

    WHERECOLUMN_NAME = BusinessEntityID

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    7/35

    SELECT@@VERSIONASSQL_Version;

    SELECTSERVERPROPERTY('ProductVersion')ASversion;

    SELECTSERVERPROPERTY('Collation')AScollation;

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    8/35

    SELECTsession_id,login_time,program_name

    FROMsys.dm_exec_sessions

    WHEREis_user_process =1;

    SELECTreferencing_schema_name,

    referencing_entity_name,

    referencing_class_desc

    FROM sys.dm_sql_referencing_entities(

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

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    9/35

    Naming pattern Description

    db Database-related informationexec Query execution-related information

    io I/O statistics

    os SQL Server Operating System (SQLOS) information

    tran Transaction-related information

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    10/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    11/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    12/35

    --no parameters so lists all database

    EXECsys.sp_databases;

    --single parameter of name of table

    EXECsys.sp_helpN'Sales.Customer';

    --multiple named parameters

    EXECsys.sp_tables

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

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    13/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    14/35

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

    EXECsys.sp_help@objname =N'Sales.Customer';

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    15/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    16/35

    CREATEPROCEDUREProduction.ProdsByProductLine

    (@numrows ASint,@ProdLine ASnchar)

    ASSELECTTOP(@numrows)ProductID,

    Name,ListPrice

    FROM Production.Product

    WHERE ProductLine =@ProdLine;

    --Retrieve top 50 products with product line = M

    EXECProduction.ProdsByProductLine 50, M

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    17/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    18/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    19/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    20/35

    Table scan: SQL Server reads all table rows

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

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    21/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    22/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    23/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    24/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    25/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    26/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    27/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    28/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    29/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    30/35

    SETSTATISTICSTIMEON;

    SETSTATISTICSIO ON;

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    31/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    32/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    33/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    34/35

  • 8/11/2019 Module_8 Retrieving SQL Metadata and Improving SQL Performanceod1.pptx

    35/35

    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, I MPLIED OR STATUTORY, AS TO THE I NFORMATION IN THIS PRESENTATION.