[英]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
查询存储的主要流程
Enabling the Query Store : Query Store works at the database level on the server. 启用查询存储 :查询存储在服务器上的数据库级别工作。
tempdb
database. tempdb
数据库启用查询存储。
sys.database_query_store_options
(Transact-SQL)sys.database_query_store_options
(Transact-SQL)
Collect Information in the Query Store : We collect all the available information from the three stores using Query Store DMV (Data Management Views). 在查询存储中收集信息 :我们使用查询存储DMV(数据管理视图)从三个存储中收集所有可用信息。
Query Plan Store: Persisting the execution plan information and it is accountable for capturing all information that is related to query compilation. 查询计划存储:保留执行计划信息,它负责捕获与查询编译有关的所有信息。
sys.query_store_query
(Transact-SQL)sys.query_store_plan
(Transact-SQL)sys.query_store_query_text
(Transact-SQL)sys.query_store_query
(Transact-SQL)sys.query_store_plan
(Transact-SQL)sys.query_store_query_text
(Transact-SQL)
Runtime Stats Store: Persisting the execution statistics information and it is probably the most frequently updated store. 运行时统计信息存储:保留执行统计信息,它可能是更新最频繁的存储。 These statistics represent query execution data.
这些统计信息表示查询执行数据。
sys.query_store_runtime_stats
(Transact-SQL)sys.query_store_runtime_stats
(Transact-SQL)
Query Wait Stats Store: Persisting and capturing wait statistics information. 查询等待统计信息存储:持久性和捕获等待统计信息。
sys.query_store_wait_stats
(Transact-SQL)sys.query_store_wait_stats
(Transact-SQL)
NOTE: Query Wait Stats Store is available only in SQL Server 2017+ 注意:查询等待状态存储仅在SQL Server 2017+中可用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.