Find a specific query in the Query Store?

 

Find a specific query in the Query Store

The SQL Server Query Store is an incredibly useful feature for monitoring and troubleshooting query performance issues. One of the handiest aspects of the Query Store is the ability to easily retrieve execution plans and query text for queries that have run in your database.

We can search for a specific query text within the Query Store using a query like this:

Copied!
/*https://www.dbascrolls.com*/

SELECT qsq.query_id, qsq.last_execution_time, qsqt.query_sql_text
FROM sys.query_store_query qsq
    INNER JOIN sys.query_store_query_text qsqt
    ON qsq.query_text_id = qsqt.query_text_id
WHERE qsqt.query_sql_text LIKE '%your query text%';


This will return all queries containing the text "your query text" with their ID, last execution time, and full query text. Very handy for tracking down queries! 

The Query Store gives tremendous visibility into what queries have executed and how well they have performed over time. It's an invaluable tool for SQL Server performance tuning and troubleshooting.

Disclaimer: This SQL query code was originally shared on Stack Overflow. I have tested and confirmed it works as intended and wanted to reshare it here to help others.
Lince Sebastian

Indian MSSQL DBA thriving in database management - ensuring efficiency and smooth operations. Devoted father of two and avid Clash of Clans player, driven by strategic thinking. Football fuels my passion - cheering Kerala Blasters and Arsenal. I share my professional and life insights through my blog.

*

Post a Comment (0)
Previous Post Next Post