Custom T-SQL Profiling with Extended Events and ML

By Tom Nonmacher

In an era where data is the new oil, optimizing SQL Server operations is more critical than ever. SQL Server 2022, Azure SQL, and the vast array of Microsoft technologies provide a robust platform for managing and deriving insights from our data. However, custom profiling using T-SQL, in conjunction with Extended Events and Machine Learning, can unlock new levels of performance and understanding. This blog post will guide you through the process of leveraging these technologies for custom profiling.

Before we delve in, let's understand the core components. T-SQL, or Transact-SQL, is Microsoft's extension of SQL that includes procedural programming, local variable, various support functions for string processing, date processing, mathematics, etc. Extended Events are a highly configurable and lightweight performance monitoring system that uses less CPU than other SQL Server methods. Machine Learning, integrated via SQL Server Machine Learning Services, allows R and Python execution in SQL Server.

The first step in custom profiling involves capturing SQL server events using Extended Events. This can be achieved using a simple T-SQL script. For example:


-- Create the Event session
CREATE EVENT SESSION [MySession] ON SERVER 
ADD EVENT sqlserver.sql_statement_completed
(SET collect_statement=(1))
ADD TARGET package0.event_file
(SET filename=N'MySession.xel', max_file_size=(5), max_rollover_files=(4))
WITH (MAX_MEMORY=4096KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS, 
MAX_DISPATCH_LATENCY=5 SECONDS, MAX_EVENT_SIZE=0KB, 
MEMORY_PARTITION_MODE=NONE, TRACK_CAUSALITY=OFF, 
STARTUP_STATE=OFF)
GO
-- Start the session
ALTER EVENT SESSION [MySession] ON SERVER STATE = START;
GO

The above T-SQL script creates an event session named "MySession" that tracks each SQL statement completion. The data is then stored in a file named "MySession.xel". This file can be analyzed later to understand query performance and identify bottlenecks.

On the other hand, Machine Learning can be used to analyze this data and predict future trends, making it a potent tool for proactive performance tuning. SQL Server Machine Learning Services provide in-database machine learning capabilities, enabling you to train predictive models using your SQL Server data. This can be particularly useful in predicting query execution times, database growth trends, and potential system bottlenecks.

Microsoft's Fabric, a distributed systems platform, can be used to manage and scale these applications. This platform can host services created using various programming models, including reliable services and reliable actors. Azure SQL, a fully-managed relational cloud database service, can be used to handle the data storage needs of these applications, providing scalability, high availability, and security.

Delta Lake and Databricks can be used to handle big data processing needs. Delta Lake is an open-source storage layer that brings reliability to data lakes. It provides ACID transactions, scalable metadata handling, and unifies streaming and batch data processing. Databricks, on the other hand, is a platform that combines data engineering, data science, machine learning, and analytics, and can be used to process and analyze the event data.

The OpenAI + SQL integration allows developers to use SQL to query data stored in OpenAI's GPT-3 model. This can be used to generate human-like text based on the event data, providing insights in a more human-readable form. Moreover, it can be used to provide context-aware suggestions for performance tuning based on the event data.

In conclusion, the combination of T-SQL, Extended Events, and Machine Learning can provide a powerful tool for custom SQL Server profiling. By incorporating technologies such as Microsoft Fabric, Azure SQL, Delta Lake, Databricks, and OpenAI, developers can build robust, scalable, and insightful profiling applications.

Check out the latest articles from all our sites:

Privacy Policy for sqlsupport.org

Last updated: May 15, 2026

sqlsupport.org respects your privacy and is committed to protecting any personal information you may provide while using this website.

This Privacy Policy document outlines the types of information that are collected and recorded by sqlsupport.org and how we use it.

Information We Collect

  • Internet Protocol (IP) addresses
  • Browser type and version
  • Pages visited
  • Time and date of visits
  • Referring URLs
  • Device type

Cookies and Web Beacons

sqlsupport.org uses cookies to store information about visitors preferences and to optimize the users experience.

How We Use Your Information

  • Operate and maintain our website
  • Improve user experience
  • Analyze traffic patterns
  • Prevent fraudulent activity

Contact

Email: admin@sqlsupport.org




A37F6D
Please enter the code from the image above in the box below.