以下查询需要花费大量时间才能将结果返回到两个不同的数据库中。有没有办法简化这个查询?
WITH tblParent AS
(
SELECT *
FROM REFERENCES
WHERE referenced_id = 208593
UNION ALL
SELECT REFERENCES.*
FROM REFERENCES
JOIN tblParent ON REFERENCES.referenced_id = tblParent.entity_Id
)
SELECT DISTINCT(entity_Id)
FROM tblParent
WHERE entity_Id <> 208593 AND field_type = 'ChildField'
OPTION(MAXRECURSION 5)
答案 0 :(得分:1)
这应该简化它:
<figure>
<img src="http://www.innomate.com/application/files/4514/8474/3363/front01.png" />
<figcaption>Testing</figcaption>
</figure>
通过检查c值,很明显它是一个子值。我假设这个文本对所有孩子都有field_type ='ChildField'。
(REFERENCES是保留字,DISTINCT不是函数)
答案 1 :(得分:0)
由于您只对子entityId感兴趣,因此只需在递归CTE中选择所需的字段即可。
WITH tblParent AS (
SELECT entity_Id, referenced_id as baseId
FROM [REFERENCES]
WHERE referenced_id = 208593
UNION ALL
SELECT t.entity_Id, cte.baseId
FROM tblParent cte
JOIN [REFERENCES] t
ON (t.referenced_id = cte.entity_Id
AND t.entity_Id <> cte.baseId -- to avoid a circular reference
)
WHERE t.field_type = 'ChildField'
)
SELECT DISTINCT entity_Id
FROM tblParent
WHERE entity_Id <> baseId
OPTION(MAXRECURSION 5)
你可能想要仔细检查一下reference_id是否有索引。