sql server调试

介绍 (Introduction)

Often enough, multilayer software has bugs. SQL Server Extended Events system offers tools that can help find those bugs. A bug can happen in any layer – data, logic, or presentation. To fix those bugs, it helps to see the exact parameters and values that the presentation layer sends to the data layer. As a data layer product, SQL Server Extended Events can help with this.

多层软件经常有错误。 SQL Server扩展事件系统提供了可以帮助发现这些错误的工具。 错误可能发生在数据,逻辑或表示的任何层中。 为了修复这些错误,它有助于查看表示层发送到数据层的确切参数和值。 作为数据层产品, SQL Server扩展事件可以提供帮助。

演示应用 (The Demo Application)

For this article, we’ll work with a VB.net desktop application that I built for a customer. The application handles recipe management. The user sees a presentation layer that front-ends a locally hosted, completely relational SQL Server 2014 Enterprise database. First, we’ll see a bug in the Application Recipe Edit Module. Then, we’ll use the SQL Server Extended Events tool to understand and fix the bug.

对于本文,我们将使用我为客户构建的VB.net桌面应用程序。 该应用程序处理配方管理。 用户将看到一个表示层,该表示层位于本地托管的,完全相关SQL Server 2014 Enterprise数据库的前端。 首先,我们将在Application Recipe Edit Module中看到一个错误。 然后,我们将使用SQL Server扩展事件工具来理解和修复该错误。

错误 (The Bug)

We’ll edit a recipe in the Recipe Edit module, as seen in this screenshot:

我们将在“ 配方编辑”模块中编辑配方,如以下屏幕截图所示:

In the lower right Recipe Text control, we’ll edit the recipe text, as seen in the screenshot below:

在右下方的“ 配方文本”控件中,我们将编辑配方文本,如以下屏幕截图所示:

The recipe text shown here has the exact content, formatting, casing, etc. that we want. We’ll click the Save Recipe button at the lower right to save the changes. Sometime later, we’ll try to edit the recipe again. We’ll again open it in the Recipe Edit module, as seen in this screenshot:

此处显示的配方文本具有所需的确切内容,格式,大小写等。 我们将单击右下角的“ 保存配方”按钮以保存更改。 稍后,我们将尝试再次编辑配方。 我们将在“ 配方编辑”模块中再次打开它,如以下屏幕截图所示:

That previous edit shows the correct text, except now the recipe text has all upper casing. We just found a bug, and we need to fix it.

先前的编辑显示正确的文本,但现在配方文本具有全部大写字母。 我们刚刚发现了一个错误,我们需要对其进行修复。

调查错误 (Investigating the bug)

The Recipe Edit module calls the UPDATE_RECIPE stored procedure to update a recipe. We can call this stored procedure in a Query Analyzer, with parameters that match this test case. First, we’ll manually roll back the recipe, with this SQL Server statement:

配方编辑模块调用UPDATE_RECIPE存储过程来更新配方。 我们可以在查询分析器中使用与该测试用例匹配的参数来调用此存储过程。 首先,我们将使用此SQL Server语句手动回滚配方:

EXEC UPDATE_RECIPE 5, 'A TEST RECIPE', 'This is a test recipe.', 5

This screenshot shows this statement in a Query Analyzer tab:

此屏幕快照在“ 查询分析器”选项卡中显示了此语句:

The SELECT statement at line 4 verifies the RECIPES table record rollback. This screenshot shows that the Query Analyzer rollback change worked:

第4行的SELECT语句验证RECIPES表记录回滚。 此屏幕快照显示查询分析器回滚更改已起作用:

This screenshot runs UPDATE_RECIPE with the changes we originally wanted to make:

此屏幕快照以我们最初想要进行的更改运行UPDATE_RECIPE

It looks like the stored procedure ran correctly. We can again look at the recipe in the Recipe Edit tool, as shown in this screenshot:

看起来存储过程正确运行。 我们可以再次在“ 食谱编辑”工具中查看食谱,如以下屏幕截图所示:

Based on the research so far, the bug behavior shown in the third screenshot above most likely does not happen in the SQL Server data layer. We need to look outside this layer, and we’ll turn to the SQL Server Extended Events tool.

根据到目前为止的研究,上面第三张屏幕快照中显示的错误行为很可能不会在SQL Server数据层中发生。 我们需要查看这一层之外的内容,然后转到SQL Server扩展事件工具。

扩展事件工具 (The Extended Events Tool)

Microsoft offered the SQL Server Profiler tool since at least SQL Server 7. The Profiler captures SQL Server event and behavior “payloads” at a fine-grained level, and SQL developers and DBAs can use this for diagnostics. The Profiler can watch the parameter flows into the UPDATE_RECIPE stored procedure, so it would help here. However, in this article, An overview of the SQL Server Profiler, Hadi Fadlallah explained that Microsoft would eventually phase out the Profiler. As a replacement, Microsoft first released SQL Server Extended Events in SQL Server 2008. That version of SQL Server Extended Events covered fewer events compared to Profiler, and it did not have a UI. In SQL Server 2012, added a UI, and with every major release, SQL Server Extended Events design covers more events. Microsoft defines SQL Server Extended Events as “… a lightweight performance monitoring system that enables users to collect data needed to monitor and troubleshoot problems in SQL Server.” As we’ll see, it will help us solve the problem.

微软提供的SQL Server事件探查工具,因为至少SQL服务器7. 探查器捕获SQL Server的事件和行为的“有效载荷”在细粒度水平,SQL开发人员和数据库管理员可以使用此诊断。 事件探查器可以监视参数流入UPDATE_RECIPE存储过程的过程,因此在这里很有用。 但是,在本文“ SQL Server 分析器概述”中, Hadi Fadlallah解释说,微软最终将淘汰Profiler 。 作为替代,微软首次发布了SQL Server扩展事件 SQL Server 2008中,相对于探查版本的SQL Server扩展事件涉及较少的事件,它没有一个UI。 在SQL Server 2012中,添加了一个UI,并且在每个主要版本中, SQL Server扩展事件设计涵盖了更多事件。 Microsoft将 SQL Server扩展事件 定义为“…一种轻量级性能监视系统,使用户能够收集监视和解决SQL Server中的问题所需的数据。” 如我们所见,它将帮助我们解决问题。

建立扩展事件会话 (Build an Extended Events Session)

SQL Server Extended Events system operates on “sessions.” We’ll create a new Extended Event session here. First, launch SQL Server and expand Databases. Expand the RECIPES database, and drill into Stored Procedures. We’ll focus on the UPDATE_RECIPE stored procedure, highlighted in this screenshot:

SQL Server扩展事件系统在“会话”上运行。 我们将在此处创建一个新的扩展事件会话。 首先,启动SQL Server并展开数据库。 展开RECIPES数据库,并深入到“ 存储过程” 。 我们将专注于此屏幕截图中突出显示的UPDATE_RECIPE存储过程:

To build an Extended Event session, drill down to Extended Events -> Sessions in the Object Explorer, as shown below:

要构建扩展事件会话,请在对象资源管理器中深入到扩展事件 -> 会话 ,如下所示:

Right-click “Sessions” and drill down to New Session, as shown in this screenshot:

右键单击“ Sessions ”,然后向下钻取到New Session ,如以下屏幕快照所示:

We could pick “New Session Wizard” here, but “New Session” seems to involve less effort to build a session. The New Session window opens, as shown in this screenshot:

我们可以在这里选择“ New Session Wizard ”,但是“ New Session ”似乎不需要花费很多精力来建立一个会话。 如下面的屏幕快照所示,将打开“ 新会话”窗口:

For this demo, we’ll focus on the General and Events pages at the upper left, starting with the General page. As we build the session, we’ll see a lot of options and choices available, but we’ll focus only on what we need. This screenshot shows the General page for the session:

对于本演示中,我们将着重在常规活动页面的左上角,从常规页。 在构建会话时,我们会看到很多可用的选项,但是我们只会专注于我们需要的内容。 此屏幕快照显示了该会话的“ 常规”页面:

We named the session “UPDATE_RECIPE_session” and later on, we’ll see how to manually launch it and watch the live data on the screen. Next, we’ll configure the session at the Events page, as shown in this screenshot:

我们将会话命名为“ UPDATE_RECIPE_session ”,随后,我们将看到如何手动启动它并在屏幕上观看实时数据。 接下来,我们将在“ 事件”页面上配置会话,如以下屏幕截图所示:

First, we’ll pick the specific event we want in the Event library list box, although we can pick more. As of SQL Server 2014, SQL Server Extended Events covers more than 1300 events. Reference material about them might become hard to find, but as a start, we can look at the sys.dm_xe_objects table with this query:

首先,我们将在“ 事件库”列表框中选择所需的特定事件,尽管我们可以选择更多事件。 从SQL Server 2014开始SQL Server扩展事件涵盖1300多个事件。 关于它们的参考资料可能会变得很难找到,但首先,我们可以使用以下查询查看sys.dm_xe_objects表:

SELECT    *
FROM    sys.dm_xe_objects
ORDER BY  name

The following screenshot shows the basic information returned about the available events:

以下屏幕快照显示了返回的有关可用事件的基本信息:

In the highlighted row above, the description for rpc_completed says that it “Occurs when a remote procedure call has completed.” This looks promising because a stored procedure might count as a “remote procedure.” Other research showed that this guess is correct. Click the highlighted rpc_completed row. Then, click the circled right arrow to move this event to the Selected events list box, as shown in this screenshot:

在上面突出显示的行中,对rpc_completed的描述说它“在远程过程调用完成时发生。” 这看起来很有希望,因为存储过程可能算作“远程过程”。 其他研究表明,这种猜测是正确的。 单击突出显示的rpc_completed行。 然后,单击带圆圈的右箭头,将此事件移至“ 选定事件”列表框中,如以下屏幕截图所示:

This enables the “Configure” control at the upper right. Click it to open the event configuration options, as shown below:

这将启用右上角的“ 配置 ”控件。 单击它以打开事件配置选项 ,如下所示:

Click the Filter Predicates tab, and place these values in the Event configuration options list box row:

单击“ 筛选谓词”选项卡,然后将这些值放在“ 事件配置选项”列表框行中:

Field           =>          object_name

字段=> object_name

Operator    =>          =

运算子=> =

Value          =>          UPDATE_RECIPE

值=> UPDATE_RECIPE

Ignore the And/Or field. This screenshot shows the Events page Configuration section:

忽略“ 和/或”字段。 此屏幕快照显示了“ 事件”页面的“ 配置”部分:

Click OK at the lower right as the last step. This screenshot shows the new session in the Object Explorer:

最后一步,单击右下角的“ 确定” 。 此屏幕快照显示了对象浏览器中的新会话:

To launch UPDATE_RECIPE_session, right-click it in Object Explorer, and click Start Session, as shown in this screenshot:

要启动UPDATE_RECIPE_session ,请在Object Explorer中右键单击它,然后单击Start Session ,如以下屏幕截图所示:

Now, enable the Watch Live Data option. Click it as shown in this screenshot:

现在,启用“ 观看实时数据”选项。 单击此屏幕截图中所示:

The Watch Live Data option opens a Management Studio tab, as shown in this screenshot:

观看实时数据”选项将打开“ Management Studio”选项卡,如以下屏幕截图所示:

Rollback the recipe again, with this SQL Server statement:

使用此SQL Server语句再次回滚配方:

EXEC UPDATE_RECIPE 5, 'A TEST RECIPE', 'This is a test recipe.', 5

Now that we set up everything, we’ll edit the recipe as we did earlier, and watch the data flow. The following screenshot shows the session tab:

现在我们已经完成了所有设置,我们将像之前一样编辑配方,并观察数据流。 以下屏幕截图显示了会话选项卡:

In the lower pane, the statement row has this value:

在下部窗格中,语句行具有以下值:

exec  UPDATE_RECIPE@recipe_ID=5,@recipe_name=N'A TEST RECIPE',@recipe_text=N'THIS IS A TEST RECIPE. THIS WILL TEST THE EXTENDED EVENTS TOOL.',@category_ID=N'5',@key_values=default

Something changed the @recipe_text argument value to all upper case. We know from earlier steps in the investigation that the @recipe_text argument should look like this:

某些东西将@recipe_text参数值更改为全部大写。 从调查的早期步骤中我们知道@recipe_text参数应如下所示:

@recipe_text=N'This is a test recipe. This will test the extended events tool.'

Based on the investigation, the bug happened before the call to the UPDATE_RECIPE stored procedure. The front-end software probably has a bug. As a first guess, we should look in the code around the call to UPDATE_RECIPE, as shown in this screenshot:

根据调查,该错误发生在调用UPDATE_RECIPE存储过程之前。 前端软件可能存在错误。 首先猜测一下,我们应该在UPDATE_RECIPE调用周围查找代码,如以下屏幕快照所示:

We know the bug involves the @RECIPE_TEXT value, and we can see a potential problem with it at line 599, as shown in the screenshot below:

我们知道该错误涉及@RECIPE_TEXT值,并且可以在599行看到它的潜在问题,如下面的屏幕快照所示:

The VB.net UCase function converts text values to upper case. If we remove it, roll back the data, and try again, we’ll see that we fixed the bug, as shown in this screenshot:

VB.net UCase函数将文本值转换为大写。 如果删除它,请回滚数据,然后重试,我们将看到已修复了该错误,如以下屏幕截图所示:

As configured, the SQL Server Extended Events UPDATE_RECIPE_session only “sees” UPDATE_RECIPE calls that come from outside the server. As we saw earlier, this statement calls UPDATE_RECIPE:

按照配置, SQL Server扩展事件 UPDATE_RECIPE_session仅“看到”来自服务器外部的UPDATE_RECIPE调用。 如前所述,该语句调用UPDATE_RECIPE:

EXEC UPDATE_RECIPE 5, 'A TEST RECIPE', 'This is a test recipe.', 5

If we run it in Query Analyzer, the UPDATE_RECIPE_session tab won’t see that UPDATE_RECIPE stored procedure call.

如果我们在查询分析器中运行它,则UPDATE_RECIPE_session选项卡将看不到UPDATE_RECIPE存储过程调用。

结论 (Conclusion)

As we saw, the SQL Server Extended Events tool offers SQL Server professionals a flexible, powerful way to understand the product and system engineering of the software they build.

如我们所见, SQL Server扩展事件工具为SQL Server专业人士提供了一种灵活,强大的方式来了解他们所构建软件的产品和系统工程。

翻译自: https://www.sqlshack.com/using-sql-server-extended-events-to-debug-applications/

sql server调试

sql server调试_使用SQL Server扩展事件来调试应用程序相关推荐

  1. sql server 统计_看SQL Server 2016中的新实时查询统计信息

    sql server 统计 With the release of SQL Server 2016 also comes a great new feature to get a live view ...

  2. sql server作业_在SQL Server中报告作业失败并发出警报

    sql server作业 SQL Server Agent can be used to run a wide variety of tasks within SQL Server. The buil ...

  3. sql server死锁_了解SQL Server死锁图的图形表示

    sql server死锁 #contentor img { float: left; } #contentor img { float: left; } 介绍 (Introduction) If yo ...

  4. sql concat函数_使用SQL Plus(+)和SQL CONCAT函数SQL Server CONCATENATE操作

    sql concat函数 This article explores SQL Server Concatenate operations using the SQL Plus (+) operator ...

  5. sql server死锁_了解SQL Server中的死锁定义

    sql server死锁 This article explains the deadlock definition in SQL Server, and it also mentions how t ...

  6. sql server 循环_学习SQL:SQL Server循环简介

    sql server 循环 Loops are one of the most basic, still very powerful concepts in programming – the sam ...

  7. sql server 缓存_了解SQL Server查询计划缓存

    sql server 缓存 Whenever a query is run for the first time in SQL Server, it is compiled and a query p ...

  8. sql server序列_在SQL Server中实现序列聚类

    sql server序列 In this article, we will be discussing Microsoft Sequence Clustering in SQL Server. Thi ...

  9. sql server版本号_识别SQL Server版本号的不同方法

    sql server版本号 In this article, I am going to show different methods to identify the SQL Server versi ...

最新文章

  1. BestCoder Round #92 比赛记录
  2. 《数据科学R语言实践:面向计算推理与问题求解的案例研究法》一一2.1 引言...
  3. 2012到2020主要的CNN架构总结
  4. 牛逼哄哄的 RPC 框架,底层到底什么原理?
  5. Flask - app的配置和实例化Flask的参数
  6. 深入学习SAP UI5框架代码系列之七:控件数据绑定的三种模式 - One Way, Two Way和OneTime实现原理比较
  7. python---字符编码
  8. STL 合集(不断补充)
  9. 【es】ElasticSearch 插件开发
  10. Windows Server 2012 将资源发布到 AD DS
  11. DataTable 去重合并
  12. linux环境下vi编辑器的模式及模式切换方法,文件保存退出方法,Linux环境下vi/vim编辑器常用命令...
  13. linux操作系统日志查看,linux 如何查看系统日志
  14. 分享200个App移动端模板---总有一个适合你
  15. YouTube双字幕显示
  16. 江苏省无锡市高二计算机小高考,江苏新高考高二还要考小高考吗?江苏新高考高中怎么分科?...
  17. 前端获取本地ip和外网ip
  18. 分类变量 哑变量矩阵 指标矩阵_ML基础:协方差矩阵
  19. deepin v20桌面bug_“国产”Deepin桌面操作系统V20 正式版
  20. CeSharp107.1.4升级物联网浏览器

热门文章

  1. 吴裕雄 15-MySQL LIKE 子句
  2. android-activity生命周期方法
  3. hiho_100_八数码
  4. F - Warm up - hdu 4612(缩点+求树的直径)
  5. android:ellipsize省略文字用法(转载)
  6. MY-SQL常用命令
  7. 线上讲座——全国海关中心架构师王翔畅谈设计模式
  8. 【Vue2.0】— TodoList案例(十七)
  9. 【Vue】样式穿透 ::v-deep的具体使用
  10. 【Vue】—Vue组件基本介绍