SQL Server: Performance Tuning and Optimization: Module: Locking and Concurrency Lesson: Blocking Student Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

SQL Server: Performance

Tuning and Optimization


 
Module: Locking and Concurrency

Lesson: Blocking

Student Lab Manual


Conditions and Terms of Use
Microsoft Confidential

This training package is proprietary and confidential, and is intended only for uses described in the training materials. Content
and software is provided to you under a Non-Disclosure Agreement and cannot be distributed. Copying or disclosing all or any
portion of the content and/or software included in such packages is strictly prohibited.

The contents of this package are for informational and training purposes only and are provided "as is" without warranty of any
kind, whether express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular
purpose, and non-infringement.

Training package content, including URLs and other Internet Web site references, is subject to change without notice. Because
Microsoft must respond to changing market conditions, the content should not be interpreted to be a commitment on the part
of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication. Unless
otherwise noted, the companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events
depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.

Copyright and Trademarks


© 2016 Microsoft Corporation. All rights reserved.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights
covering subject matter in this document. Except as expressly provided in written license agreement from
Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights,
or other intellectual property.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for
any purpose, without the express written permission of Microsoft Corporation.
For more information, see Use of Microsoft Copyrighted Content at

http://www.microsoft.com/en-us/legal/intellectualproperty/Permissions/default.aspx

DirectX, Hyper-V, Internet Explorer, Microsoft, Outlook, OneDrive, SQL Server, Windows, Microsoft Azure,
Windows PowerShell, Windows Server, Windows Vista, and Zune are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries. Other Microsoft products mentioned herein
may be either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other
countries. All other trademarks are property of their respective owners.

 
 

Table of Contents
 
LAB 1: LOCKING AND CONCURRENCY

EXERCISE 1: IDENTIFYING BLOCKING


 

Lab 1: Locking and Concurrency


 

Introduction
In this lab, you will simulate a production environment with blocking issues and deadlock
issues. You will practice how to troubleshoot these problems.

Objectives
After completing this lab, you will be able to:
         Identify blocking and analyze blocking issues.
         Use Extended Events to troubleshoot blocking and deadlock issues.
         Understand read-committed snapshot behavior and optimize heavy-blocking
environments.

Prerequisites
         A basic knowledge of locking concepts and transaction isolation level concepts
         A basic understanding of how to use a profiler trace

Estimated Time to Complete This Lab


30 minutes
 

Scenario
 
There are several applications that are based on the AdventureWorksdatabase. These
applications include online transaction processing (OLTP) type of applications, such as sales
order processing, and online analytical processing (OLAP) type applications, such as
statistical query. Recently, customers found that some queries are running slow, and the
development team reported that they see database 1205 errors in the application log.
Note that some of the problems might be related to stored procedures and triggers.
 
 

Exercise 1: Identifying Blocking


 

Objectives
In this exercise, you will:
         Identify blocking by using dynamic management views (DMVs).
         Identify blocking by using Extended Events.

Prerequisites
         Connect to the SQLPTO virtual machine.
         Signin by using the following credentials:
User name:SQLPTO\Administrator
Password:P@$$w0rd1

Note: The virtual machine for this workshop is time bombed for security purposes. You might need to
rearm the virtual machine if the activation expires. If you see a message to reactivate the virtual
machine, you can use the slmgr.vbs file with the rearm option as follows:

1.     Open an elevated command prompt (right-click Command Prompton the Startmenu


and click Run As Administrator).

2.     Execute the following command:


slmgr.vbs –rearm

Scenario
Customers report that some queries are running slow and they suspect that the databases have
blocking issues. Now, you need to identify the reason for the blocking issues and identify the
queries that are responsible.
 

Generate User Activities and Troubleshoot Blocking 


1.       Navigate to the E:\Labs\Module3\Exercise1 folder, and double-click the batch
file GenerateData.cmd. This file will run for roughly five minutes and will close
automatically when it completes. Do not wait for this batch file to complete. Continue to
the next step while it is running.
2.       Double-click the GenerateActivity.cmdbatch file. This action will call several Transact-
SQL (T-SQL) scripts to generate user activities on the server. As a result, you will see
several SQLCMDwindows open up. Do not close any of these command windows now.
Continue to the next step while the commands are running running.  
3.       Open ViewBlockingDMVs.sqlin Microsoft SQL Server Management Studio and run the
following SELECT statement to determine if any blocking is occurring. You might have
to run this a few times before you can find any blocking.
SELECT

CASE WHEN er.session_idIS NULL
              THEN es.session_id

              ELSE er.session_id

       END AS session_id,

       er.blocking_session_id,

       er.wait_time,

       er.wait_resource,

       er.wait_type,

       er.last_wait_type,

       er.status,

       CASE WHEN er.session_idIS NULL

              THEN (SELECT text

FROM sys.dm_exec_sql_text(ec.most_recent_sql_handle))

              ELSE (SELECT text

FROM sys.dm_exec_sql_text(er.sql_handle))

       END AS QueryText

FROM sys.dm_exec_connectionsec

       JOINsys.dm_exec_sessionses ON ec.session_id=es.session_id

       LEFTJOINsys.dm_exec_requestser ON es.session_id=er.session_id

WHERE er.blocking_session_id> 0

ORes.session_idIN (SELECT blocking_session_id

FROM sys.dm_exec_requests

WHERE blocking_session_id> 0)

 
 

Question A:          Which session_idhas a blocking_session_idcolumn value that is


greater than 0?
 
 
 
Question B:          What are the values of status, last_wait_type, and wait_resourceof
the blocked session?
 
 
 
4.       Looking at the query text of the blocking and blocked sessions, and then answer the
following question:
 
Question C:          Can you find the cause of the blocking by examining the text of the
SQL statement?
 
 
 
5.       Make sure your configuration parameter blocked process threshold (s) is set to 5,
otherwise use the following statement in the BlockedThreshold.sqlfile:
EXEC sys.sp_configureN'blocked process threshold (s)', N'5'

GO

RECONFIGURE

GO

 
6.       Open and run the BlockedProcessReport.sqlfile to create and start an Extended Events
session that will collect the Blocked Process Report event in an in-memory ring buffer.
CREATE EVENT SESSION [BlockedProcessReport]ON SERVER

ADD EVENT sqlserver.blocked_process_report

ADD TARGET package0.ring_buffer(SET max_events_limit=(0),max_memory=(2048))

WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPAT
CH_LATENCY=30SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSAL
ITY=OFF,STARTUP_STATE=OFF)

GO

ALTER EVENT SESSION [BlockedProcessReport]

ON SERVER

STATE=START

GO

 
7.       In the Management Studio Object Explorer, go to Management > Extended Events >
Sessions. If you do not see the BlockedProcessReportsession, right-click Sessions, and
      click Refresh. After you see the BlockedProcessReportsession, right-click it and
click Watch Live Data.
 

8.       Wait for some time to see if there is a blocking event captured in the ring buffer. After
you see an event, double-click the blocked_processrow in the bottom pane to view the
XML type of blocked process report.

      

 
Question

D:          What is the wait resource of the blocked process? Can you


identify the object from the picture
 
 
Question E:          What are the statements of the blocked process and the blocking
process? Think about how you can resolve the blocking scenario.
 
 
9.       Close the Extended Event Live Data window and any other windows that are open in
Management Studio. Right-click the BlockedProcessReportExtended Eventssession in
the Object Explorer, and click Stop Session. You can also delete the session at this point,
or keep it for future use. Close all the open Command Prompt windows to stop all the
database activity.

You might also like