简体   繁体   English

为什么我必须对这些层次结构查询强制订购/

[英]Why do I have to Force Order with these hierarchy queries/

Below is an example of a query I might run where, for each category, I want the NumberOfCourses to represent not only that specific category but also any child categories under it. 以下是我可能在其中运行的查询的示例,其中对于每个类别,我希望NumberOfCourses不仅代表该特定类别,还代表该类别下的所有子类别。 I think the query is fairly self explanatory. 我认为该查询很容易解释。

select  c.CategoryID, courses.MarketID, count(distinct courses.CourseID) NumberOfCourses
from    Category c
        join CategoryHierarchy tch on tch.HierarchyKey like '%~' + cast(c.CategoryID as varchar) + '~%'
        join vLiveEvents courses on tch.CategoryID = courses.CategoryID
where   courses.MarketID is not null
group by c.CategoryHumanID, courses.MarketID

When I run this as is it may take almost two minutes, however if I add the hint Option (Force Order) then it only takes a few seconds to run. 当我按原样运行时,可能会花费近两分钟,但是,如果我添加了提示Option (Force Order)则只需几秒钟即可运行。 So my question is am I doing something wrong that's causing SQL to create a bad plan or is the SQL engine actually just not good at optimizing hierarchy joins like this? 所以我的问题是我做错了什么导致SQL创建一个错误的计划,还是SQL引擎实际上并不擅长像这样优化层次结构联接?

I tried including the sql plan, but it's way too long and SO won't let me have that many characters. 我尝试包括sql计划,但是它太长了,因此不会让我拥有那么多字符。 I'm happy to share it though if anyone can tell me how to do so. 我很高兴分享它,尽管有人可以告诉我该怎么做。

EDIT: I guess probably not every knows how these kinds of hierarchies work. 编辑:我想可能不是每个人都知道这些层次结构如何工作。 They hierarchy key will look something like ~1234~5678~9123~ where 1234 is the parent of 5678 which is the parent of 9123. By doing a like comparison on a CategoryID I can include all child categories in the results. 它们的层次结构键看起来像〜1234〜5678〜9123〜,其中1234是5678的父级,这是9123的父级。通过对CategoryID进行类似比较,我可以在结果中包括所有子类别。

Starting from SQL Server 2016+, Query Store feature was introduced to monitor performance. 从SQL Server 2016+开始,引入了查询存储功能以监视性能。 It provides insight into query plan choice and performance. 它提供有关查询计划选择和性能的见解。

It also provides an option to force plan. 它还提供了强制执行计划的选项。

It's not a complete replacement of trace or extended events, but as it's evolving from version to version, we might get a fully functional query store in future releases from SQL Server. 它不是跟踪事件或扩展事件的完整替代品,但是随着版本之间的演进,我们可能会在SQL Server的将来版本中获得功能齐全的查询存储。 The primary flow of Query Store 查询存储的主要流程

  1. SQL Server existing components interact with query store by utilising Query Store Manager. SQL Server现有组件通过利用查询存储管理器与查询存储进行交互。
  2. Query Store Manager determines which Store should be used and then passes execution to that store (Plan or Runtime Stats or Query Wait Stats) 查询存储管理器确定应使用哪个存储,然后将执行传递给该存储(计划或运行时统计或查询等待统计)
    • Plan Store - Persisting the execution plan information 计划存储-保留执行计划信息
    • Runtime Stats Store - Persisting the execution statistics information 运行时统计信息存储-持久执行统计信息
    • Query Wait Stats Store - Persisting wait statistics information. 查询等待统计信息存储-持久等待统计信息。
  3. Plan, Runtime Stats and Wait store uses Query Store as an extension to SQL Server. 计划,运行时统计信息和等待存储使用查询存储作为SQL Server的扩展。

在此处输入图片说明

  1. Enabling the Query Store : Query Store works at the database level on the server. 启用查询存储 :查询存储在服务器上的数据库级别工作。

    • Query Store is not active for new databases by default. 默认情况下,查询存储对新数据库无效。
    • You cannot enable the query store for the master or tempdb database. 您不能为master或tempdb数据库启用查询存储。
    • Available DMV 可用的DMV

      sys.database_query_store_options (Transact-SQL) sys.database_query_store_options (Transact-SQL)

  2. Collect Information in the Query Store : We collect all the available information from the three stores using Query Store DMV (Data Management Views). 在查询存储中收集信息 :我们使用查询存储DMV(数据管理视图)从三个存储中收集所有可用信息。

NOTE: Query Wait Stats Store is available only in SQL Server 2017+ 注意:查询等待状态存储仅在SQL Server 2017+中可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这两个带有“ EXISTS”的查询的行为不同? - Why do these two queries with 'EXISTS' behave differently? 如何使用CTE递归来建立组织层次结构? - How do I use CTE recursion to build an organization hierarchy? 我如何根据值进行层次结构数据 - How can i do a hierarchy data based on a value 如何根据给定表中的层次结构从列中删除值? - How do I delete values from a column based on a Hierarchy in a given table? SQL Server:更新顺序,为什么可以使用,我可以信任它吗? - SQL Server : Update order, Why does it work and can I trust it? 我如何获得从星期一开始的星期几订单 - How do I get the Day of Week order to start with Monday 如果在应用程序中使用SQL Server,是否必须安装SQL Server? - Do I have to install SQL Server if i use it in my application? 如何在SQL Server 2008中查找字符串中具有点层次结构顺序的数字顺序的下一项 - How to find next item for a numeric order with dot hierarchy order in string in SQL Server 2008 如何加快对具有 200 万行的表的批量更新查询? - How do I speed up the batch of update queries over the table with 2 million rows? 在 SQL Server 2012 中的多级层次结构查询中按特定顺序对子项进行排序 - Order child items in a certain order in a multi-level hierarchy query in SQL Server 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM