AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 307658
Accepted
adam.g
adam.g
Asked: 2022-02-18 13:07:08 +0800 CST2022-02-18 13:07:08 +0800 CST 2022-02-18 13:07:08 +0800 CST

已终止会话是否出现在查询存储中?

  • 772

是否有任何选项可以查看查询存储中的已终止会话?

我在问,因为我们有一个额外的工具,如果会话运行时间超过 30 分钟(KILL命令),它就会终止会话。

我想检查查询存储中的执行计划是否有已终止的查询。我在查询存储会话/查询中找不到被这个附加应用程序杀死的。

sql-server query-store
  • 2 2 个回答
  • 385 Views

2 个回答

  • Voted
  1. Zikato
    2022-02-19T00:15:12+08:002022-02-19T00:15:12+08:00

    他们没有。我已经测试过了。

    创建数据库并启用查询存储。

    CREATE DATABASE QueryStoreTest
    GO
    USE QueryStoreTest;
    GO
    ALTER DATABASE QueryStoreTest
    SET QUERY_STORE = ON
        (
          OPERATION_MODE = READ_WRITE,
          CLEANUP_POLICY = ( STALE_QUERY_THRESHOLD_DAYS = 1 ),
          DATA_FLUSH_INTERVAL_SECONDS = 900,
          MAX_STORAGE_SIZE_MB = 50,
          INTERVAL_LENGTH_MINUTES = 60,
          SIZE_BASED_CLEANUP_MODE = AUTO,
          MAX_PLANS_PER_QUERY = 10,
          WAIT_STATS_CAPTURE_MODE = ON,
          QUERY_CAPTURE_MODE = ALL /* this is required for the testing */
        );
    GO
    

    创建并运行一个耗时的查询。我用过这个:

    SELECT 
        *
    FROM sys.all_columns AS ac
    CROSS JOIN sys.all_parameters AS ap
    CROSS JOIN sys.all_objects AS ao
    

    在 SSMS 中运行查询完成或停止执行,查询可以在查询存储中找到。

    SELECT 
        qsq.query_id
        , qsq.query_hash
        , qsp.plan_id
        , qsqt.query_sql_text
        , qsrs.count_executions
        , qsrs.execution_type_desc
    FROM sys.query_store_query AS qsq
    LEFT JOIN sys.query_store_plan AS qsp
        ON qsp.query_id = qsq.query_id
    LEFT JOIN sys.query_store_query_text AS qsqt
        ON qsqt.query_text_id = qsq.query_text_id
    JOIN sys.query_store_runtime_stats AS qsrs
        ON qsrs.plan_id = qsp.plan_id
    WHERE qsqt.query_sql_text LIKE '%cross join%'
    AND qsqt.query_sql_text NOT LIKE '%query_store%'
    

    执行类型是Regular或Aborted。但是,如果您从另一个窗口终止会话,则count_executions既不会增加,也不会出现新记录。

    • 6
  2. Best Answer
    John K. N.
    2022-02-19T00:17:43+08:002022-02-19T00:17:43+08:00

    您可能需要考虑使用较新版本的 SQL Server 提供的扩展事件(快速启动扩展事件)功能仅跟踪花费超过一定时间的查询。

    让我们为您创建一个扩展事件,用于捕获运行时间超过 60 秒的语句(只是一个示例)。

    导航到 SSMS 中的扩展事件

    SQL Server 实例 |  管理 |  扩展活动 |  会话

    在这里,您可以右键单击Sessions分支以打开上下文菜单。

    新会议...

    新会话...上下文菜单项

    与标记的会话相反,我们将使用空的New Session...选项。

    新会话向导介绍

    新会话向导介绍对话框

    点击下一步 >

    新建会话向导设置会话属性

    新建会话向导设置会话属性对话框

    我们将为会话命名:Statement Duration > 1 Minute,然后单击Start the event session at server startup选项,然后单击Next > ..

    新会话向导选择模板

    新建会话向导选择模板对话框

    我们不会使用默认模板。下一个 >

    新会话向导选择要捕获的事件

    新建会话向导选择要捕获的事件对话框

    我们将只使用sql_transaction事件并使用相应的>箭头将其向右移动。下一步 >。

    新会话向导捕获全局字段

    新会话向导捕获全局字段对话框

    我们将至少选择sql_text字段,但我添加了更多类似的内容:

    • 客户端应用程序名称
    • 客户端主机名
    • 数据库名称
    • nt_username
    • 计划句柄
    • 查询哈希
    • 查询计划哈希
    • sql_text
    • 用户名

    ...然后点击Next >。

    新会话向导设置会话事件过滤器

    新建会话向导设置会话事件过滤器对话框

    我们将暂时过滤sqlos.task_execution_time并将值设置为6000。我们将返回此设置以将其更改为duration。点击下一步>。

    新建会话向导指定会话数据存储

    新建会话向导指定会话数据存储对话框

    您可以在此处指定要将数据存储在文件或 ring_bugger 目标中的位置。我们暂时使用一个文件。配置适合您的服务器的值,然后单击Next >。

    New Session Wizard Summary

    新建会话向导摘要对话框

    Validate your configuration and click on Next >.

    New Session Wizard Create Event Session

    新会话向导创建事件会话对话框

    Select both options to have something running and then click on Next >.

    Extended Events | Sessions | Statement Duration > 1 Minute

    Extended Events | Sessions | Statement Duration > 1 Minute picture

    You'll now have a new Extended Event and a windows displaying the current output of this Extended Event.

    Change Configuration

    Right-click the session and stop it. Right-click again and go into the properties:

    Session Properties

    Select the sql_transaction and then click on Configure >...

    Session Properties Configiuration Filter

    ...switch to the Filters tab and delete the current filter. Add a new filter with the values duration, greater than and 60000000 (microseconds).

    Click on OK and then start your session again in the Extended Events. You might have to select Watch Live Data again to have a current window open.

    Context Menu of Extended Event Session

    The Fun Part

    Now you have an Extended Event Session which will track statements that take longer than 60 seconds. Run a statement on that server with something like this:

    BEGIN TRAN
        UPDATE DemoDB.dbo.Tweets SET TweetText = 'something else' WHERE TweetID = 2;
        WAITFOR DELAY '00:02';
    COMMIT TRAN;
    

    After a minute you should see an event pop up in your Watch Live Data window in SSMS. It might look like this:

    Capture Data of Live Session

    You can read the statement and with the plan_handle you can even go and query the sys.dm_exec_text_query_plan DMV to have a look at the execution plan:

    SELECT 
    CAST(query_plan AS XML) 
    FROM sys.dm_exec_text_query_plan(0x06000100C0187C0A601FD200D902000001000000000000000000000000000000000000000000000000000000,0,-1)
    

    Which shows:

    Result Set of Query

    Click on the XML link and voilà:

    Graphical Execution Plan

    This (in my example) execution plan can then be used to see if things are running ok, or if you might benefit from additional indexes or query tuning.

    Answering Your Question

    Is there any option to see killed sessions in Query Store?

    If the statement has run before, but didn't take longer than your timeout, then these queries might be found in the Query Store.

    If the statement terminates, because something changed, then maybe. It depends on the reason for the time-out.

    按照我的解决方案中的描述创建一个扩展事件,而不是将其设置为 30 分钟,而是将超时设置为 29 分 59 秒,或者使用1799000000微秒的过滤器值。

    您现在应该能够在实时数据或创建的文件中跟踪长时间运行的事务。

    • 5

相关问题

  • SQL Server - 使用聚集索引时如何存储数据页

  • 我需要为每种类型的查询使用单独的索引,还是一个多列索引可以工作?

  • 什么时候应该使用唯一约束而不是唯一索引?

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve