Query store was introduced in SQL Server 2016. It is often referred to as a “flight data recorder” for SQL Server. Its main function is that it captures the history of executed queries as well as certain statistics and execution plans. Furthermore, the data is persistent, unlike the plan cache in which the information is cleared upon a server restart or reboot. You can customize, within Query Store, how much and how long the query store can hold the data.

查询存储是SQL Server 2016中引入的。它通常被称为SQL Server的“飞行数据记录器”。 它的主要功能是捕获执行查询的历史记录以及某些统计信息和执行计划。 此外,数据是持久性的,与计划缓存不同,在计划缓存中,信息会在服务器重启或重新启动时清除。 您可以在查询存储中自定义查询存储可以保存数据的数量和时间。

This feature is enabled on the database level. Though, I have strongly urged many to enable this by default by enabling it in the model database. That way every time you create a new database on a particular instance, query store will already be running.

在数据库级别启用此功能。 但是,我强烈建议许多人通过在模型数据库中启用它来默认启用它。 这样,每次在特定实例上创建新数据库时,查询存储将已经在运行。

The query store is made up of 3 components.

查询存储由3个组件组成。

  • Plan store for holding execution plan information 计划存储,用于保存执行计划信息
  • Runtime stats for holding statistical information 用于保存统计信息的运行时统计信息
  • Wait for stats for holding wait statistics information 等待统计信息以保存等待统计信息

It’s great to have all this information in one location as opposed to gathering it separately with custom scripts.

最好将所有这些信息都放在一个位置,而不是使用自定义脚本分别收集这些信息。

The remainder of this article will present some use cases for query store. It isn’t meant to be overly technical (we can save that for another post). Rather, it will give us good talking points, especially when it comes to justifying to management why your environment could use this feature.

本文的其余部分将介绍查询存储的一些用例。 这并不意味着技术过高(我们可以将其保存在另一篇文章中)。 而是,它将为我们提供良好的论点,尤其是在向管理层证明您的环境可以使用此功能的理由时。

为什么要使用查询存储? (Why should I use the Query Store?)

Query store can be used for a number of things.  For example:

查询存储可用于许多事情。 例如:

  • Find out what was happening on my server last night? 找出昨晚服务器上发生的情况?
  • Identify and fix queries suffering from parameter sniffing or plan regression 识别并修复遭受参数嗅探或计划回归的查询
  • Testing (ie – when upgrading to a new version of SQL server, or development projects) 测试(即–升级到新版本SQL Server或开发项目时)

昨晚我的服务器上发生了什么事? (What happened on my server last night?)

Query store is great for a historical view of what your server was doing. It’s often referred to as “a flight data recorder”.

查询存储非常适合用于服务器历史记录的历史记录。 它通常被称为“飞行数据记录器”。

There were so many times where I was asked “what happened to so and so’s process last night? why did it run so long etc…”

有很多次我被问到“昨晚某事发生了什么? 它为什么运行了这么长时间等等?”

Query store would have saved me a ton of time to find out what was going on and ideas on how to correct it. Not only that, but the fact that we can identify it so quickly means that we can prevent this from happening again.

查询存储将为我节省大量时间,以了解发生了什么事情以及有关如何纠正它的想法。 不仅如此,而且我们能够如此Swift地识别它的事实意味着我们可以防止这种情况再次发生。

Once the query store is enabled you can look at the reports in the “query store” section of the databases. Please reference the image below.

启用查询存储后,您可以在数据库的“查询存储”部分中查看报告。 请参考下图。

识别并修复遭受参数嗅探或计划回归的查询: (Identify and Fix queries suffering from Parameter sniffing or Plan regression:)

There are times when the Query Optimizer may choose a different plan due to certain values for parameters. And in rare cases, this new plan is not optimal for most inputs and thus gives us degraded performance.

有时由于某些参数值,查询优化器可能会选择其他计划。 而且在极少数情况下,此新计划对于大多数输入而言并非最佳选择,因此会降低性能。

In the past, this was difficult to identify and remedy. Query store has empowered us as DBAs and developers. Utilizing the “Regressed Queries” option from SQL Server Management Studio can be used as a starting point for analysis. Now you have what SQL has identified as queries in which the execution metrics have been degraded.

过去,这很难确定和补救。 查询存储使我们成为DBA和开发人员。 利用SQL Server Management Studio中的“回归查询”选项可以用作分析的起点。 现在,您已经拥有SQL所标识的查询,其中执行指标已降级。

The graphical user interface makes it easy to see the multiple plans and which are more resource intensive. This visualization gives a starting point. From here you can see actual execution plan and make an educated decision as to which one you want to force (or not).

图形用户界面使您可以轻松地查看多个计划,并且这些计划的资源消耗更大。 该可视化提供了一个起点。 在这里,您可以查看实际的执行计划,并根据自己的意愿决定要(或不)强制执行哪一个。

In order to force a plan, it’s as simple as selecting the query plan and pressing the “force plan” button highlighted below.

为了强制执行计划,就像选择查询计划并按下下面突出显示的“强制计划”按钮一样简单。

测试中 (Testing)

There are actually a couple of scenarios in regard to testing. One would be for an upgrade or migration project. Another could be for a development project.

关于测试,实际上有两种方案。 一个将用于升级或迁移项目。 另一个可能是用于开发项目。

With a migration, you can do a dry run and then run a workload against a database with query store enabled in the old compatibility mode. Once that information is collected, set the databases compatibility mode to the latest and continue testing. Check the query store to see if there have been any plan regression and tune accordingly.

通过迁移,您可以进行空运行,然后针对在旧兼容模式下启用了查询存储的数据库运行工作负载。 收集到该信息后,将数据库兼容模式设置为最新并继续测试。 检查查询存储以查看是否存在任何计划回归并进行相应调整。

The approach might look something like this:

该方法可能看起来像这样:

  • Migrate the database to a new version of SQL server. But leave the compatibility level under the older version of SQL 将数据库迁移到新版本SQL Server。 但是将兼容性级别保留在旧版本SQL下
  • Enable query store on said database 在所述数据库上启用查询存储
  • Run workload against the database and let query store collect information. (make sure query store is sized properly depending on how much you think you need) 针对数据库运行工作负载,并让查询存储收集信息。 (确保查询存储的大小取决于您认为所需的大小)
  • Set the compatibility level for this database to the latest version of SQL Server 将此数据库的兼容性级别设置为SQL Server的最新版本
  • Check query store for any query regressions. Once you’ve identified anything, make sure to document it 检查查询存储中是否有任何查询回归。 确定任何内容后,请确保将其记录下来
  • From there either force plans or tune the query to run more efficiently 从那里强制计划或调整查询以更有效地运行

Look at the top consuming queries and see if you want to tune them or force a plan or just gather a baseline of statistics and execution plans for the major store procedures and queries.

查看消耗最大的查询,查看是否要调整它们或强制执行计划,还是只是收集主要商店过程和查询的统计数据和执行计划的基准。

Documentation is important, and we don’t do it enough. Gathering as much baseline information as possible early on will help tremendously as your database and workload increase over time.

文档很重要,我们做得还不够。 随着数据库和工作负载的不断增加,尽早收集尽可能多的基准信息将大有帮助。

You can utilize query store to analyze resources such as CPU, I/O and memory at the database level. This will allow you to see the patterns early on see how it either deviates over time or if there is a onetime occurrence that shows a change in the pattern.

您可以利用查询存储在数据库级别分析资源,例如CPU,I / O和内存。 这样一来,您可以及早查看模式,以了解其随着时间的变化或一次出现的情况表明模式发生了变化。

This is an example of doing our due diligence as we prepare to migrate or upgrade databases to new versions of SQL. Take advantage of it, document it, and present it to management.

这是在准备将数据库迁移或升级到新版本SQL时进行尽职调查的一个示例。 利用它,记录下来,并提交给管理层。

As with the above paragraph, the same approach can be used when working on a development project. You can identify queries, parameter sniffing ahead of time and either tune the queries or force the plan before the go-live date.

与上面的段落一样,在开发项目中可以使用相同的方法。 您可以识别查询,提前进行参数嗅探,或者调整查询或在上线日期之前强制计划。

查询所有商店? (Query store for all?)

One disadvantage of Query store is that it is only available for SQL Server 2016 and later versions. But thanks to the Open Query store project, developed by William Durkin, this functionality has been made available to everyone on SQL Server 2008 and higher.

查询存储的一个缺点是它仅适用于SQL Server 2016和更高版本。 但是感谢William Durkin开发的Open Query store项目 ,此功能已在SQL Server 2008及更高版本上的每个人使用。

Open Query store functionality is also included in ApexSQL Plan, a new, free tool for query plan analysis.

开放查询存储功能也包含在ApexSQL Plan中 , ApexSQL Plan是用于查询计划分析的新的免费工具。

结论: (Conclusion:)

I hope that you found this article informative. It was not meant to be overly technical, rather it’s intent was to give some ideas on how you can utilize query store in your environment. This is especially useful if you must bring it up to management so that they know how useful it will be to you as a DBA and in turn, how it will impact the business.

我希望您发现这篇文章有益。 这并不是要过分技术,而是要就如何在环境中利用查询存储给出一些想法。 如果您必须将其引入管理层,以使他们知道作为DBA的身份对您有多大用处,进而对您的业务有何影响,这将特别有用。

翻译自: https://www.sqlshack.com/use-cases-for-query-store-in-sql-server/

SQL Server中查询存储的用例相关推荐

  1. SQL Server 2016 查询存储性能优化小结

    SQL Server 2016已经发布了有半年多,相信还有很多小伙伴还没有开始使用,今天我们来谈谈SQL Server 2016 查询存储性能优化,希望大家能够喜欢 作为一个DBA,排除SQL Ser ...

  2. 如何在SQL Server中分析存储子系统性能

    介绍 (Introduction) To improve performance, it is common for DBAs to search in each aspect except anal ...

  3. SQL Server中查询时显示行号的方法

    如何在SQL中对行进行动态编号,加行号这个问题,在数据库查询中,是经典的问题. 方法整理如下: 代码基于pubs样板数据库 在SQL中,一般就这两种方法. 1.使用临时表 可以使用select int ...

  4. SQL Server 中查询非中文,非英文,非数字的特殊列

    今天在处理一个用户名数据库时,发现有些不正常的数据存在,按照逻辑,用户名只能是数字,字母,下划线和纯中文这样的字符组合存在,不应该有其他组合存在,但是发现数据库中由于各种历史原因,有些不正常的存在,如 ...

  5. SQL SERVER中查询无主键的SQL

    --生成表 IF  EXISTS ( SELECT  name                 FROM    sysobjects                 WHERE   xtype = ' ...

  6. SQL Server中查询所有的表、视图、列和存储过程

    SELECT * FROM INFORMATION_SCHEMA.TABLES SELECT * FROM INFORMATION_SCHEMA.VIEWS SELECT * FROM INFORMA ...

  7. Sql Server中查询当天,最近三天,本周,本月,最近一个月,本季度的数据的sql语句...

    --当天:select * from T_news where datediff(day,addtime,getdate())=0--最近三天:select * from T_news where d ...

  8. T-SQL查询进阶--理解SQL Server中索引的概念,原理以及其他(看了两次了,转了)

    简介 在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能.但索引可以在大多数情况下大大提升查询性能,在OLAP中尤其明显.要完全理解索 ...

  9. 理解SQL Server中索引的概念,原理以及其他

    简介 在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能.但索引可以在大多数情况下大大提升查询性能,在OLAP中尤其明显.要完全理解索 ...

最新文章

  1. MDT2010新功能(15)——完成部署后操作
  2. ASP.NET MVC 重写RazorViewEngine实现多主题切换
  3. nanopi 创建共享文件夹
  4. 软件开发的“黑名单”规则设定:benchmark
  5. Horspool 字符串快速查找算法
  6. 51Nod - 1381 硬币游戏
  7. 小程序按钮调用扫一扫_他在一个小程序“按钮”上动了个手脚,生意大火,赢得美人归!...
  8. 2核4G阿里云服务器被黑客抓鸡??然鹅一个操作就搞定
  9. BMP、GIF、TIFF、PNG、JPG和SVG格式图像的特点
  10. 刚开始接触编程也能轻松写的计算器代码(VS2019)(c语言)
  11. 移动硬盘做pe启动盘
  12. 国内高校大数据教研机构调研报告
  13. 数据结构与算法题目集7-32——哥尼斯堡的“七桥问题”
  14. Linux系列 使用vi文本编辑器
  15. 关于RO、RW、ZI的说明
  16. 万万没想到!我拒绝了一位知名VC大佬的创业合伙人邀请
  17. InnoDB和Myisam引擎的优缺点
  18. ArcGIS应用基础1 数据显示、查询及可视化
  19. JavaScript 根据经纬度得出中心点的经纬度
  20. 松江区专利工作试点和示范企业认定政策解读

热门文章

  1. STM32液晶显示HT1621驱动原理及程序代码
  2. Eclipse中如何更改字体大小?
  3. 上海雄联机械配件有限公司
  4. 修改Visual Studio中“添加新项”时默认添加的命名空间
  5. module.exports与exports
  6. LeetCode(404)——左叶子之和(JavaScript)
  7. idea系---懒人
  8. 【零基础学Java】—Calendar类(三十四)
  9. P1426 小鱼会有危险吗
  10. 现在离开哈尔滨需要做核酸检测吗?