Long Running Queries in SQL Server

How to Identify and Store Long Running Queries in a Table

Spread the love

Scenario

How to Identify and Store Currently Long Running Queries in a Table?

To retrieve information about currently executing requests on a Microsoft SQL Server database, we can use several Dynamic Management Views (DMVs) to gather information about the status of the requests, sessions, and connections associated with the database.

  • sys.dm_exec_requests : DMV that contains information about currently executing requests in the SQL Server database.
  • sys.dm_exec_sessions : DMV that contains information about the sessions connected to the SQL Server database.
  • sys.dm_exec_connections : DMV that contains information about the connections established to the SQL Server database.

So with the above three DMVs we can write a query to find the currently running queries in SQL Server with the below-listed information.

  • SPID: The ID of the session associated with the request.
  • Status: The status of the session.
  • Login: The login name associated with the session.
  • Host: The hostname associated with the session.
  • BlkBy: The ID of the session that is blocking this request, if applicable.
  • DBName: The name of the database associated with the request.
  • CommandType: The type of command being executed by the request.
  • SQLStatement: The text of the SQL statement being executed.
  • ObjectName: The name of the object associated with the statement, if applicable.
  • ElapsedMS: The total elapsed time for the request.
  • CPUTime: The CPU time consumed by the request.
  • IOReads: The total number of logical and physical reads performed by the request.
  • IOWrites: The total number of writes performed by the request.
  • LastWaitType: The last wait type for the request.
  • StartTime: The start time of the request.
  • Protocol: The network transport protocol used by the connection associated with the request.
  • ConnectionWrites: The number of writes performed by the connection.
  • ConnectionReads: The number of reads performed by the connection.
  • ClientAddress: The client network address associated with the connection.
  • Authentication: The authentication scheme used by the connection.

Query to Identify Currently Long Running Queries

The query filters the results to show only requests associated with a database named ‘ABCD’ and sessions with an ID greater than 50. The results are sorted by CPU time consumed by each request in descending order.

As per our requirement, we can change the conditions by changing the database name or removing the whole database name condition.

Currently running Long Running Queries
Currently running Long Running Queries

Automating the query to Identify and Store Long Running Queries in a Table

Now to store the output of the above query we will use a table in a database and also run the query by an agent job in intervals of 10 – 15 minutes. Before that, we will convert the above query to a stored procedure.

Creating a stored procedure with the above query to Identify Currently Long Running Queries and in a Table

Creating a table to store the data

With the below query, we will create a table in a database to store the output.

Now if we execute the stored procedure, it will push the output data of the query to the table.

Now to check the table we may run a select statement on the table.

Creating a job to automate the process

Here’s a step-by-step guide to creating a job in SQL Server:

  1. Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
  2. Expand the “SQL Server Agent” node in the Object Explorer.
  3. Right-click the “Jobs” folder and select “New Job…” from the context menu.
  4. In the “New Job” dialog box, provide a name for the job in the “Name” field.
  5. In the “Steps” section, click the “New” button to create a new job step.
  6. In the “New Job Step” dialog box, provide a name for the step in the “Step name” field.
  7. In the “Type” field, select the type of job step you want to create (e.g., Transact-SQL script, Operating system (CmdExec), etc.).
  8. In the “Command” field, enter the command you want to run for the job step (e.g., the path to an executable file, a Transact-SQL script, etc.).
  9. In the “Advanced” section, you can configure additional options such as the database context, the output file, and the error file.
  10. Click “OK” to close the “New Job Step” dialog box.
  11. In the “Job Steps” section, you can add additional steps as needed.
  12. In the “Schedules” section, you can configure the schedule for the job. To create a new schedule, click the “New” button.
  13. In the “New Job Schedule” dialog box, provide a name for the schedule in the “Name” field.
  14. In the “Frequency” section, select the frequency for the schedule (e.g., one-time, daily, weekly, etc.).
  15. In the “Start” and “End” fields, set the start and end dates and times for the schedule.
  16. Click “OK” to close the “New Job Schedule” dialog box.
  17. In the “Notifications” section, you can configure notifications to be sent when the job starts, succeeds or fails.
  18. In the “Target servers” section, you can select the SQL Server instances where the job should be run.
  19. Click “OK” to create the job.

Once you have created the job, you can start it manually or wait for it to start automatically based on the schedule you have configured. You can also monitor the status of the job, modify its properties, or delete it as needed.


Spread the love
Author: OTi
Meet OTi, the brains behind the popular tech blog "OurTechIdeas.com." OTi is an accomplished author, tech enthusiast, and an expert in SQL Server. With over a decade of experience in the tech industry, he has become a trusted voice in the world of database management. OTi's passion for technology began at a young age, and he has been tinkering with computers ever since. He graduated with a degree in Computer Science and has since worked for some of the biggest tech companies in the world. His experience has given him a deep understanding of how technology works and how it can be used to improve our lives.

Leave a Reply

Your email address will not be published. Required fields are marked *