Vous êtes sur la page 1sur 35

08 | Retrieving SQL Server Metadata

and Improving Query Performance


Brian Alderman | MCT, CEO / Founder of MicroTechPoint
Tobias Ternstrom | Microsoft SQL Server Program Manager
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 Performance
Querying system catalogs and dynamic management views, creating and executing stored procedures, improving SQL
Server query performance
--Pre-filtered to exclude system objects
SELECT name, object_id, schema_id, type, type_desc
FROM sys.tables;

--Includes system and user objects
SELECT name, object_id, schema_id, type, type_desc
FROM 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_USAGE
WHERE COLUMN_NAME = BusinessEntityID
SELECT @@VERSION AS SQL_Version;

SELECT SERVERPROPERTY('ProductVersion') AS version;

SELECT SERVERPROPERTY('Collation') AS collation;
SELECT session_id, login_time, program_name
FROM sys.dm_exec_sessions
WHERE 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 database
EXEC sys.sp_databases;

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

--multiple named parameters
EXEC 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 <schema_name.proc_name>
(<parameter_list)
AS
SELECT <body of SELECT statement>;
CREATE PROCEDURE Production.ProdsByProductLine
(@numrows AS int, @ProdLine AS nchar)
AS
SELECT TOP(@numrows) ProductID,
Name, ListPrice
FROM Production.Product
WHERE ProductLine = @ProdLine;

--Retrieve top 50 products with product line = M
EXEC 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)
AS
SELECT TOP(@numrows) ProductID,
Name, ListPrice
FROM Production.Product
WHERE ProductLine = @ProdLine;

--Retrieve top 50 products with product line = M
EXEC 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.

Vous aimerez peut-être aussi