我需要一些帮助,以找出构建此查询的最佳方法,而且我认为我认为CTE是最好的主意。如果有更好的方法,我愿意接受其他建议。
此应用程序具有几种不同的对象类型。我当前关注的对象的简单层次结构如下:Group -> Catalog(s) -> Item(s) and Feature(s) -> Features (items also connect to features) -> Feature Values
。这些对象中的每一个也都进行了版本控制,因此它们可以是第一个版本,也可以是多个深度版本。 (提供此层次结构只是为了了解要实现的目标。
我要处理的数据都包含在History
表中。该表包含以下列:HistoryID, HistoryObjectID, HistoryObjectCode, HistoryObjectTypeID, HistoryMessage, HistoryObjectTypeObjectID, Timestamp, ParentHistoryID, ParentObjectID, HistoryObjectGuid, NetworkID
这是我目前用于查询的内容。我的问题是在“基本情况”中返回的每一行都会发生递归:
WITH n(HistoryID, HistoryObjectID, HistoryObjectTypeID, HistoryObjectTypeObjectID, HistoryObjectCode, HistoryMessage, UserID, Timestamp, ParentHistoryID, ParentObjectID, HistoryObjectGuid) AS (
SELECT HistoryID, HistoryObjectID, HistoryObjectTypeID, HistoryObjectTypeObjectID, HistoryObjectCode, HistoryMessage, UserID, Timestamp, ParentHistoryID, ParentObjectID, HistoryObjectGuid
FROM [DBName].[History]
WHERE HistoryObjectID = @HistoryObjectID
UNION ALL
SELECT nplus1.HistoryID, nplus1.HistoryObjectID, nplus1.HistoryObjectTypeID, nplus1.HistoryObjectTypeObjectID, nplus1.HistoryObjectCode, nplus1.HistoryMessage, nplus1.UserID, nplus1.Timestamp, nplus1.ParentHistoryID, nplus1.ParentObjectID, nplus1.HistoryObjectGuid
FROM [DBName].History as nplus1
INNER JOIN n on n.HistoryObjectTypeObjectID = nplus1.ParentObjectID)
SELECT n.HistoryID, n.HistoryObjectID, n.HistoryObjectCode, n.HistoryObjectTypeID, n.HistoryMessage, n.HistoryObjectTypeObjectID, n.Timestamp, n.ParentHistoryID, n.ParentObjectID, n.HistoryObjectGuid, u.NetworkID
FROM n INNER JOIN [CatalogBuilder].[User] u on n.UserId = u.UserId
例如,使用特定ID的以下查询运行之一将返回1653行,而在基本情况下将返回28行。如果在最终的select语句中添加distinct
,则只会得到我真正感兴趣的49行。显而易见的问题是,由于不必要的递归,该查询需要花费非常长的时间才能运行。
我愿意接受有关如何执行此操作的任何建议。
编辑:添加示例数据
所需结果(返回14行):
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
| HistoryID | HistoryObjectID | HistoryObjectCode | HistoryObjectTypeID | HistoryMessage | HistoryObjectTypeObjectID | Timestamp | ParentHistoryID | ParentObjectID | HistoryObjectGuid | NetworkID |
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
| 313404 | 572668 | TEST CATALOG CREATION | 3 | Created catalog version 1.0.0 for catalog TESTCAT | 201 | 2019-01-04 15:02:22.1533333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319335 | 572668 | CATALOGCODE | 3 | Updated Catalog code to CATALOGCODE | 201 | 2019-01-23 21:29:19.6933333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319538 | 572668 | CATALOGCODE | 3 | Set English description to "new desc edited" | 201 | 2019-02-04 18:32:11.2166667 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319539 | 572668 | CATALOGCODE | 3 | Added ADP to Catalog | 201 | 2019-02-04 18:32:14.3100000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319544 | 572668 | CATALOGCODE | 3 | Added USD to Catalog | 201 | 2019-02-05 13:36:11.5433333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319551 | 572668 | CATALOGCODE | 3 | Added CAD to Catalog | 201 | 2019-02-06 14:35:28.9100000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319552 | 572668 | CATALOGCODE | 3 | Added USD to Catalog | 201 | 2019-02-06 14:35:28.9700000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
返回的行太多(56行):
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
| HistoryID | HistoryObjectID | HistoryObjectCode | HistoryObjectTypeID | HistoryMessage | HistoryObjectTypeObjectID | Timestamp | ParentHistoryID | ParentObjectID | HistoryObjectGuid | NetworkID |
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
| 313404 | 572668 | TEST CATALOG CREATION | 3 | Created catalog version 1.0.0 for catalog TESTCAT | 201 | 2019-01-04 15:02:22.1533333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319335 | 572668 | CATALOGCODE | 3 | Updated Catalog code to CATALOGCODE | 201 | 2019-01-23 21:29:19.6933333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319538 | 572668 | CATALOGCODE | 3 | Set English description to "new desc edited" | 201 | 2019-02-04 18:32:11.2166667 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319539 | 572668 | CATALOGCODE | 3 | Added ADP to Catalog | 201 | 2019-02-04 18:32:14.3100000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319544 | 572668 | CATALOGCODE | 3 | Added USD to Catalog | 201 | 2019-02-05 13:36:11.5433333 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319551 | 572668 | CATALOGCODE | 3 | Added CAD to Catalog | 201 | 2019-02-06 14:35:28.9100000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319552 | 572668 | CATALOGCODE | 3 | Added USD to Catalog | 201 | 2019-02-06 14:35:28.9700000 | NULL | NULL | 3CE6BD8D-3AF4-46D2-9004-976113E110CD | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319685 | 585464 | TESTITEM8 | 4 | Created Item TESTITEM8 - 1.0 | 52051 | 2019-02-11 15:32:49.8633333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319686 | 585464 | TESTITEM8 | 4 | Set English name to "Test item #8" | 52051 | 2019-02-11 15:32:54.0033333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319687 | 585464 | TESTITEM8 | 4 | Created item version 1.0 | 52051 | 2019-02-11 15:32:56.8800000 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319688 | 585464 | TESTITEM8 | 4 | Set English description to "Test Item Description" | 52051 | 2019-02-11 15:33:00.4266667 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319689 | 585465 | NEW-FEATURE-CREATION | 6 | Set English name to "test" | 19224 | 2019-02-11 16:27:33.6400000 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319690 | 585465 | NEW-FEATURE-CREATION | 6 | Created feature version 1.0 | 19224 | 2019-02-11 16:27:33.9066667 | NULL | 201 | 55ACDB43-51E6-4A5D-94DB-8B0808F946D6 | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
| 319692 | 585464 | TESTITEM8 | 4 | Linked feature NEW-FEATURE to item | 52051 | 2019-02-11 18:59:17.2133333 | NULL | 201 | FB3D799F-2038-4CE1-A27E-0C6F6EBE607F | hmiedema@steelcase.com |
+-----------+-----------------+-----------------------+---------------------+----------------------------------------------------+---------------------------+-----------------------------+-----------------+----------------+--------------------------------------+------------------------+
对于基本案例的每次运行,似乎都将返回“额外”行。在基本情况下返回了7行,而在其他7个条目中返回了49行,这些行应该是唯一的,这是期望的最后14行来自的位置(基本情况中的7行+其他7个唯一行)。