SQL SERVER – Extended Event to Capture Memory Grant Feedback

It has been over 3 weeks since SQL Server 2019 launched and after reading my recent blog posts about the Memory Grant Feedback, I was approached by one of the leading banks in Europe to help them build an efficient architecture and performance-optimized system Comprehensive Database Performance Health Check. In this blog post, we will learn about the extended event to capture memory grant feedback. You can read more about Memory Grant Feedback over here, here, here, here, here and here.

SQL SERVER - Extended Event to Capture Memory Grant Feedback capturememory0-800x237

Brief History

While I worked with the client to get ready for their migration of SQL Server 2019, we realized that there are few queries giving us trouble with regards to performance. As I have done a similar task, just a week before for another client, I exactly knew the problem for my client. I have previously blogged about it why I have disabled the Memory Grant Feedback at the Query Level.

However, as this time we realized that there are many queries which were facing the same issue, we decided to identify those queries with the help of the Extended Event. Once we identified the queries with the extended events, we were able to tune the queries together.

Capture Memory Grant Feedback

Let us see how we can create an extended event to Capture Memory Grant Feedback.

You can create an extended event by running the following script:

ADD EVENT sqlserver.memory_grant_feedback_loop_disabled(
ADD EVENT sqlserver.memory_grant_updated_by_feedback(

However, if you want to use the wizard, you can follow the steps displaced here in the images.

Now once the extended event is created and enabled. Right-click on it and select “Watch Live Data“. Initial it will be empty. Now go to the blog post mentioned here and create the stored procedure displayed in the blog post. Next, run the following stored procedure 20 times by executing the following code. As we are running two stored procedure the total execution will be of 40 times.

EXEC GetStockDetails 120
EXEC GetStockDetails 223
GO 20

Continue to watch the live data while the stored procedure is executing and you will see various events being recorded in the live stream. Even though the stored procedure is executed a total of 40 times, it will record lesser events in the extended events as it will stop the feedback loop.

You will notice that after 32nd execution, the memory grant update by feedback is disabled.

If you look carefully in the details area, you can also find the stored procedure which is creating this behavior.

At my client’s place, we identified all such stored procedures and tuned them one at a time. Currently, their system is running extremely smooth and very efficient.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Exit mobile version