How to get more details(user, execution time, IP address) for a given query ID on Azure SQL DB

MikeZheng-3371 46 Reputation points
2025-01-21T16:35:40.94+00:00

How can I get all the details(user, execution time, IP address, etc) for a given Query ID? I am using Azure SQL DB

Azure SQL Database
{count} votes

3 answers

Sort by: Most helpful
  1. hossein jalilian 10,740 Reputation points Volunteer Moderator
    2025-01-21T17:58:59.2+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    You can use a combination of system views and functions.

    SELECT 
        q.query_id,
        qt.query_sql_text,
        qp.plan_id,
        qrs.last_execution_time,
        qrs.count_executions,
        qrs.avg_duration,
        qrs.avg_cpu_time,
        qrs.avg_logical_io_reads
    FROM sys.query_store_query q
    JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
    JOIN sys.query_store_plan qp ON q.query_id = qp.query_id
    JOIN sys.query_store_runtime_stats qrs ON qp.plan_id = qrs.plan_id
    WHERE q.query_id = YOUR_QUERY_ID;
    
    

    For more detailed information, including the user and client IP address, you can combine this with other system views:

    SELECT 
        q.query_id,
        qt.query_sql_text,
        s.login_name AS [User],
        c.client_net_address AS [IP Address],
        qrs.last_execution_time,
        qrs.count_executions,
        qrs.avg_duration,
        qrs.avg_cpu_time,
        qrs.avg_logical_io_reads
    FROM sys.query_store_query q
    JOIN sys.query_store_query_text qt ON q.query_text_id = qt.query_text_id
    JOIN sys.query_store_plan qp ON q.query_id = qp.query_id
    JOIN sys.query_store_runtime_stats qrs ON qp.plan_id = qrs.plan_id
    OUTER APPLY sys.dm_exec_sql_text(q.last_compile_batch_sql_handle) AS st
    LEFT JOIN sys.dm_exec_sessions s ON s.session_id = st.session_id
    LEFT JOIN sys.dm_exec_connections c ON c.session_id = s.session_id
    WHERE q.query_id = YOUR_QUERY_ID;
    
    

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful


  2. Balmukund 176 Reputation points Microsoft Employee
    2025-05-12T18:35:18.9733333+00:00

    Query Store is a powerful feature in Azure SQL Database (and also in SQL Server) that helps you track query performance over time. It acts like a "flight recorder" for your database, capturing a summarized history (min, max, std. deviation) of query execution plans and runtime statistics. Query store is not meant for auditing and hence Query Store does not capture IP address or client host info.You can combine it with auditing to get the information of user/IP/client etc. from where the query was originated.You can also setup extended event to capture details of a query along with client details. something like below. Adding a filter on query text would help you getting client details.

    CREATE EVENT SESSION [Track_Query_Info]
    ON DATABASE
    ADD EVENT sqlserver.sql_statement_completed(
        ACTION (
            sqlserver.sql_text,
            sqlserver.client_app_name,
            sqlserver.client_hostname,
            sqlserver.username,
            sqlserver.client_ip,
            sqlserver.database_name,
            sqlserver.tsql_stack,
            sqlserver.session_id
        )
        WHERE (duration > 1000000) -- Optional: filter long-running queries
    )
    ADD TARGET package0.event_file (
        SET filename = 'QueryTracking.xel',
            max_file_size = 10,
            max_rollover_files = 5
    )
    WITH (STARTUP_STATE = ON);
    GO
    
    ALTER EVENT SESSION [Track_Query_Info] ON DATABASE STATE = START;
    
    

  3. PratikLad 1,585 Reputation points Microsoft External Staff Moderator
    2025-05-30T17:41:49.4166667+00:00

    In addition to Balmukund, you can check this

    Tracking both the performance of a query and the details about who ran it in Azure SQL Database requires using a few different tools, each with its own purpose. Query Store is great for analyzing how queries perform over time. It logs things like query text, execution plans, and performance metrics such as duration and CPU usage. But it doesn’t track who ran the query or where it came from — it doesn’t record usernames, client IP addresses, or hostnames.

    To get that kind of user and client info, you’ll need to use Extended Events or Azure SQL Auditing. Extended Events let you monitor specific query activity and can capture details like the username, client machine name, application name, and IP address. You can even filter events to target specific queries and save the results to a file for later review.

    Azure SQL Auditing is another option, especially useful for security and compliance. It logs database activity including the executed statements, the user who ran them, the client’s IP address, and when it happened. These logs can be stored in Azure Storage, sent to Log Analytics, or forwarded to Event Hubs depending on how you want to use or analyze them.

    By combining Query Store with Extended Events or Auditing, you get the full picture: Query Store tells you which queries are slow or causing issues, and the other tools tell you who’s running them and from where. Together, they make it easier to troubleshoot performance problems, track user activity, and keep your database environment secure and accountable.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.