Get data to lab report

I started with series of post, I would like to show how to prepare simple testing environment, you can get the data when you test or compare queries performance for example, and you would like to save results and visualise them.

In this post we create simple table structure and SQL procedure which will be used in next post as Chart dataset.

I would like to save following information’s:

  • Test identification – identification of test – for example testing clustered vs nanocluster index, sorting group vs hash group, etc.
  • Task name – identification of task we made in test – for example task1 = table with clustered index, task2 heap table
  • Counter name (for example number of reads, writes, rows, etc.)
  • Counter value – measured results of tests

As you can see the structure is very simple, I believe we could design much sophisticated solution but for our purposes will be enough. Next time we can extend our solution.

Here is the final structure:

SET
ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER
ON
GO
 
CREATE TABLE [dbo].[LabTable](
    [WorkoutName] [sysname] NOT NULL,
    [TaskOwnerName] [sysname] NOT NULL,
    [CounterName] [sysname] NOT NULL,
    [CounterValue] [sql_variant] NULL,
    [InsertDateTime] [datetime] NULL
)
ON [PRIMARY]
GO
 
ALTER
TABLE [dbo].[LabTable] ADD
CONSTRAINT [df_InsertDateTime] DEFAULT (getdate())
FOR [InsertDateTime]
GO

Stored procedure returning data to dataset:

SET
ANSI_NULLS
ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
CREATE PROCEDURE [dbo].[SSRSGetDatset]
@WorkoutName SYSNAME
AS
BEGIN
SELECT WorkoutName,
     TaskOwnerName,
     CounterName,
     TRY_CONVERT(BIGINT,TRY_CONVERT(DATETIME, CounterValue))
AS CounterValue,
     InsertDateTime
FROM dbo.LabTable
WHERE WorkoutName = @WorkoutName
END
GO

In next post we fill structures with tested data and create graph to visualise our results. Stay tuned.

Get query plan from cache

SELECT *, OBJECT_NAME(qplan.objectid), SUBSTRING(sqltext.text,st.statement_start_offset/2 +1,CASE
WHEN st.statement_end_offset = -1 THEN
LEN(CONVERT(nvarchar(max), sqltext.text)) * 2 ELSE st.statement_end_offset end
- st.statement_start_offset /2 )
AS query_text FROM
sys.dm_exec_cached_plans cplans INNER JOIN
sys.dm_exec_query_stats st ON st.plan_handle=cplans.plan_handle 
CROSS APPLY sys.dm_exec_text_query_plan (cplans.plan_handle,st.statement_start_offset,st.statement_end_offset) qplan CROSS APPLY sys.dm_exec_sql_text(st.sql_handle) sqltext

Script to get query plan from cache. It gets execution plan for particular sql statements.

Extended events advanced

 I would like to describe useful queries for extended events in more detail in this post Extended events get description. I will follow my previous post where I put few basic queries to get some extended events info. 

Extended events are triggers occurring in SQL system. Each extended event has its own set of properties -data you get from result. Extended events are collected in packages, events can be combined from different packages. List of packages defined in SQL server instance 

SELECT * FROM sys.dm_xe_packages

 Beside of that each event can fire other actions synchronously int the same thread within event was invoked. Action extend extended events with own data in response.

For example, if you wold like to get sql text of a query it is defined by an action.

SELECT *
  FROM sys.dm_xe_objects o 
 WHERE o.object_type='action'

To filter event data, you can define Predicates. There is a list of objects you can use as predicate source.

 SELECT *
 FROM sys.dm_xe_objects o 
WHERE o.object_type='pred_source'

List of operators

SELECT *
  FROM sys.dm_xe_objects o 
 WHERE o.object_type='pred_compare'

 To start collecting data you must define session object which is set of actions, event data, predicates and target object.

 Target object collects output event data by synchronous or asynchronous way. Data can be collected for example to buffer, file, Windows Events, etc.

SELECT * FROM sys.dm_xe_objects o WHERE o.object_type='target' 

 User – friendly description of internal key words which could be returned from events data

SELECT *
  FROM sys.dm_xe_objects o 
 WHERE o.object_type='map' 

  Relationship between events implemented in created sessions and actions

SELECT * FROM sys.dm_xe_session_event_actions

 Data types you can expect from data collection.

SELECT *
  FROM sys.dm_xe_objects o 
 WHERE o.object_type='type'

 Next time I would like to share some solutions implemented using extended events. Stay tuned.