Troubleshooting SQL On-demand or Serverless DMVs

Published 12-04-2020 09:21 AM 1,328 Views
Microsoft

I was working in a few SQL On-demand or SQL Serverless cases and based on an ex-PFE colleague Fabricio Catae script I created this one to help me with some troubleshooting scenarios. Feel free to adapt the script yourself for your scenario.

 

Basically, I want to take a snapshot of my DMVs while I am troubleshooting a scenario on SQLOD. So I asked the customer to repro the scenario while running my script and once is done. I work on the data collected and issue timestamps.

 

SET NOCOUNT ON
SET LOCK_TIMEOUT 30000

DECLARE @startDate DATETIME



SET NOCOUNT ON
SET LOCK_TIMEOUT 30000



WHILE 1=1
BEGIN


SET @startDate = GETDATE()


   print 'Begin Requests filtered ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

   select * from sys.dm_exec_requests where session_id >77

   print 'End Requests filtered ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

   print '================================================= ' 

   print 'Begin Requests  ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

   select * from sys.dm_exec_requests

   print 'End Requests  ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 
   print '================================================= ' 

   print 'Begin Sessions ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

   select * from sys.dm_exec_sessions 

   print 'End Sessions ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

   print '================================================= ' 

   print 'Begin Queries running ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

	   SELECT 
		'Running' as [Status],
		Transaction_id as [Request ID],
		'SQL On-demand' as [SQL Resource],
		s.login_name as [Submitter],
		s.Session_Id as [Session ID],
		req.start_time as [Submit time],
		req.start_time as [Start time],
		'N/A' as [End time],
		req.command as [Request Type],
		SUBSTRING(
			sqltext.text, 
			(req.statement_start_offset/2)+1,   
			(
				(
					CASE req.statement_end_offset  
						WHEN -1 THEN DATALENGTH(sqltext.text)  
						ELSE req.statement_end_offset  
					END - req.statement_start_offset
				)/2
			) + 1
		) as [Query Text],
		req.total_elapsed_time as [Duration],
		'N/A' as [Queued Duration],
		req.total_elapsed_time as [Running Duration],
		'N/A' as [Data processed in bytes],
		'N/A' as [Workload group],
		'N/A' as [Source],
		'N/A' as [Pipeline],
		'N/A' as [Importance],
		'N/A' as [Classifier]
	FROM 
    sys.dm_exec_requests req
    CROSS APPLY sys.dm_exec_sql_text(sql_handle) sqltext
    JOIN sys.dm_exec_sessions s ON req.session_id = s.session_id
 
    print 'End Queries running ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate()))   
    print '================================================= ' 

    print 'Begin Query requests greater than 50 ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 

	SELECT 
		req.session_id, req.blocking_session_id AS 'blocked', 
		req.database_id AS db_id, req.command, 
		req.total_elapsed_time AS 'elapsed_time', req.cpu_time, req.granted_query_memory AS 'granted_memory', req.logical_reads, 
		req.wait_time, CAST(req.wait_type AS VARCHAR(16)) AS 'wait_type', 
		req.open_transaction_count AS 'tran_count', 
		req.reads, req.writes,  
		req.start_time, req.status, req.connection_id, req.user_id, 
		req.group_id, -- KATMAI (SQL2008)
		req.transaction_id, req.request_id, 
		CAST(req.plan_handle AS VARBINARY(26)) AS 'plan_handle', 
		CAST(req.sql_handle AS VARBINARY(26)) AS 'sql_handle', 
		req.nest_level,
		req.statement_start_offset AS 'stmt_start', req.statement_end_offset AS 'stmt_end', 
		req.query_hash, req.query_plan_hash
	FROM sys.dm_exec_requests req
	WHERE group_id > 1 AND session_id<>@@SPID 
	and req.session_id > 50


	print 'End Query requests greater than 50 ' + CONVERT(VARCHAR(24), GETDATE(), 121) + ' ' + convert(VARCHAR(12), datediff(ms,@startDate,getdate())) 
	print '================================================= ' 

	waitfor delay '00:00:03'

END 


 

 

This is as you probably noticed  a script with an infinite loop : WHILE 1=1

 

Basically, you would need to connect to SQL On Demand: Server-ondemand.sql.azuresynapse.net

SQlonde.png

 

Once connected. Run the script using the result as a text option on SSMS. The grid default option would be nearly  impossible to read properly:

 

result_text.png

 

While running the script as I mentioned before, please recreate the situation that you want to monitor, as for example, I wanted to monitor a block\lock scenario. Once is done, stop the script as it is an infinite loop and look for the information that you want to find over the script results.

 

That is it!

Liliam UK Engineer

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-1955869%22%20slang%3D%22en-US%22%3ETroubleshooting%20SQL%20On-demand%20or%20Serverless%20DMVs%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1955869%22%20slang%3D%22en-US%22%3E%3CP%3EI%20was%20working%20in%20a%20few%20SQL%20On-demand%20or%20SQL%20Serverless%20cases%20and%20based%20on%20an%20ex-PFE%20colleague%20Fabricio%20Catae%20script%20I%20created%20this%20one%20to%20help%20me%20with%20some%20troubleshooting%20scenarios.%20Feel%20free%20to%20adapt%20the%20script%20yourself%20for%20your%20scenario.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBasically%2C%20I%20want%20to%20take%20a%20snapshot%20of%20my%20DMVs%20while%20I%20am%20troubleshooting%20a%20scenario%20on%20SQLOD.%20So%20I%20asked%20the%20customer%20to%20repro%20the%20scenario%20while%20running%20my%20script%20and%20once%20is%20done.%20I%20work%20on%20the%20data%20collected%20and%20issue%20timestamps.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3ESET%20NOCOUNT%20ON%0ASET%20LOCK_TIMEOUT%2030000%0A%0ADECLARE%20%40startDate%20DATETIME%0A%0A%0A%0ASET%20NOCOUNT%20ON%0ASET%20LOCK_TIMEOUT%2030000%0A%0A%0A%0AWHILE%201%3D1%0ABEGIN%0A%0A%0ASET%20%40startDate%20%3D%20GETDATE()%0A%0A%0A%20%20%20print%20'Begin%20Requests%20filtered%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20select%20*%20from%20sys.dm_exec_requests%20where%20session_id%20%26gt%3B77%0A%0A%20%20%20print%20'End%20Requests%20filtered%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20print%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20'%20%0A%0A%20%20%20print%20'Begin%20Requests%20%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20select%20*%20from%20sys.dm_exec_requests%0A%0A%20%20%20print%20'End%20Requests%20%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%20%20%20print%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20'%20%0A%0A%20%20%20print%20'Begin%20Sessions%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20select%20*%20from%20sys.dm_exec_sessions%20%0A%0A%20%20%20print%20'End%20Sessions%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20print%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20'%20%0A%0A%20%20%20print%20'Begin%20Queries%20running%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20%20%20%20SELECT%20%0A%20%20'Running'%20as%20%5BStatus%5D%2C%0A%20%20Transaction_id%20as%20%5BRequest%20ID%5D%2C%0A%20%20'SQL%20On-demand'%20as%20%5BSQL%20Resource%5D%2C%0A%20%20s.login_name%20as%20%5BSubmitter%5D%2C%0A%20%20s.Session_Id%20as%20%5BSession%20ID%5D%2C%0A%20%20req.start_time%20as%20%5BSubmit%20time%5D%2C%0A%20%20req.start_time%20as%20%5BStart%20time%5D%2C%0A%20%20'N%2FA'%20as%20%5BEnd%20time%5D%2C%0A%20%20req.command%20as%20%5BRequest%20Type%5D%2C%0A%20%20SUBSTRING(%0A%20%20%20sqltext.text%2C%20%0A%20%20%20(req.statement_start_offset%2F2)%2B1%2C%20%20%20%0A%20%20%20(%0A%20%20%20%20(%0A%20%20%20%20%20CASE%20req.statement_end_offset%20%20%0A%20%20%20%20%20%20WHEN%20-1%20THEN%20DATALENGTH(sqltext.text)%20%20%0A%20%20%20%20%20%20ELSE%20req.statement_end_offset%20%20%0A%20%20%20%20%20END%20-%20req.statement_start_offset%0A%20%20%20%20)%2F2%0A%20%20%20)%20%2B%201%0A%20%20)%20as%20%5BQuery%20Text%5D%2C%0A%20%20req.total_elapsed_time%20as%20%5BDuration%5D%2C%0A%20%20'N%2FA'%20as%20%5BQueued%20Duration%5D%2C%0A%20%20req.total_elapsed_time%20as%20%5BRunning%20Duration%5D%2C%0A%20%20'N%2FA'%20as%20%5BData%20processed%20in%20bytes%5D%2C%0A%20%20'N%2FA'%20as%20%5BWorkload%20group%5D%2C%0A%20%20'N%2FA'%20as%20%5BSource%5D%2C%0A%20%20'N%2FA'%20as%20%5BPipeline%5D%2C%0A%20%20'N%2FA'%20as%20%5BImportance%5D%2C%0A%20%20'N%2FA'%20as%20%5BClassifier%5D%0A%20FROM%20%0A%20%20%20%20sys.dm_exec_requests%20req%0A%20%20%20%20CROSS%20APPLY%20sys.dm_exec_sql_text(sql_handle)%20sqltext%0A%20%20%20%20JOIN%20sys.dm_exec_sessions%20s%20ON%20req.session_id%20%3D%20s.session_id%0A%20%0A%20%20%20%20print%20'End%20Queries%20running%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%20%20%0A%20%20%20%20print%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20'%20%0A%0A%20%20%20%20print%20'Begin%20Query%20requests%20greater%20than%2050%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%0A%20SELECT%20%0A%20%20req.session_id%2C%20req.blocking_session_id%20AS%20'blocked'%2C%20%0A%20%20req.database_id%20AS%20db_id%2C%20req.command%2C%20%0A%20%20req.total_elapsed_time%20AS%20'elapsed_time'%2C%20req.cpu_time%2C%20req.granted_query_memory%20AS%20'granted_memory'%2C%20req.logical_reads%2C%20%0A%20%20req.wait_time%2C%20CAST(req.wait_type%20AS%20VARCHAR(16))%20AS%20'wait_type'%2C%20%0A%20%20req.open_transaction_count%20AS%20'tran_count'%2C%20%0A%20%20req.reads%2C%20req.writes%2C%20%20%0A%20%20req.start_time%2C%20req.status%2C%20req.connection_id%2C%20req.user_id%2C%20%0A%20%20req.group_id%2C%20--%20KATMAI%20(SQL2008)%0A%20%20req.transaction_id%2C%20req.request_id%2C%20%0A%20%20CAST(req.plan_handle%20AS%20VARBINARY(26))%20AS%20'plan_handle'%2C%20%0A%20%20CAST(req.sql_handle%20AS%20VARBINARY(26))%20AS%20'sql_handle'%2C%20%0A%20%20req.nest_level%2C%0A%20%20req.statement_start_offset%20AS%20'stmt_start'%2C%20req.statement_end_offset%20AS%20'stmt_end'%2C%20%0A%20%20req.query_hash%2C%20req.query_plan_hash%0A%20FROM%20sys.dm_exec_requests%20req%0A%20WHERE%20group_id%20%26gt%3B%201%20AND%20session_id%26lt%3B%26gt%3B%40%40SPID%20%0A%20and%20req.session_id%20%26gt%3B%2050%0A%0A%0A%20print%20'End%20Query%20requests%20greater%20than%2050%20'%20%2B%20CONVERT(VARCHAR(24)%2C%20GETDATE()%2C%20121)%20%2B%20'%20'%20%2B%20convert(VARCHAR(12)%2C%20datediff(ms%2C%40startDate%2Cgetdate()))%20%0A%20print%20'%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20'%20%0A%0A%20waitfor%20delay%20'00%3A00%3A03'%0A%0AEND%20%0A%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20as%20you%20probably%20noticed%26nbsp%3B%20a%20script%20with%20an%20infinite%3CSTRONG%3E%20loop%20%3A%20WHILE%201%3D1%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBasically%2C%20you%20would%20need%20to%20connect%20to%20SQL%20On%20Demand%3A%20Server-ondemand.sql.azuresynapse.net%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22SQlonde.png%22%20style%3D%22width%3A%20477px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237759iBFB3F51B84F6A5F6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22SQlonde.png%22%20alt%3D%22SQlonde.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20connected.%20Run%20the%20script%20using%20the%20result%20as%20a%20text%20option%20on%20SSMS.%20The%20grid%20default%20option%20would%20be%20nearly%26nbsp%3B%20impossible%20to%20read%20properly%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22result_text.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237760iEF5DB4F5F0A3B9A5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22result_text.png%22%20alt%3D%22result_text.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhile%20running%20the%20script%20as%20I%20mentioned%20before%2C%20please%20recreate%20the%20situation%20that%20you%20want%20to%20monitor%2C%20as%20for%20example%2C%20I%20wanted%20to%20monitor%20a%20block%5Clock%20scenario.%20Once%20is%20done%2C%20stop%20the%20script%20as%20it%20is%20an%20infinite%20loop%20and%20look%20for%20the%20information%20that%20you%20want%20to%20find%20over%20the%20script%20results.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%20is%20it!%3C%2FP%3E%0A%3CP%3ELiliam%20UK%20Engineer%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1955869%22%20slang%3D%22en-US%22%3E%3CP%3ETroubleshooting%20SQL%20On-demand%20or%20Serverless%20DMVs%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1955869%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESynapse%20Monitoring%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESynapse%20SQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Dec 07 2020 02:45 AM
Updated by: