在SQL Server上,递归CTE是广度优先还是深度优先?

时间:2018-10-12 17:23:34

标签: sql-server graph common-table-expression cycle

我具有以下递归CTE

(trades.Shares/ 
       (SELECT SUM(Shares) 
             FROM db.Trades WHERE id IN 
 ('102370''102371','102372','102373','102374','102375','102376','102377','102380','102400')),12) 
 order by trades.id) AS Weights   -- <- here

有点难读,但这是图形和结果表

Graph representation

Sql Result

我有类似图形的数据,正在使用WITH relatedpages (custompageid, relatedcustompageid, [level], [path]) AS ( SELECT custompageid AS custompageid, relatedcontentid AS relatedcustompageid, 1 AS [level], CAST((CAST(custompageid AS VARCHAR) + n' ' + CAST(relatedcontentid AS VARCHAR)) AS VARCHAR) AS [path] FROM custompagerelatedcontent related WHERE related.custompageid = 1913 UNION ALL SELECT subrelatedpages.relatedcustompageid AS custompageid, subrelated.relatedcontentid AS relatedcustompageid, subrelatedpages.level + 1 AS [level], CAST(subrelatedpages.path + n' ' + CAST(subrelated.relatedcontentid AS VARCHAR) AS VARCHAR) AS [path] FROM relatedpages subrelatedpages INNER JOIN custompagerelatedcontent subrelated ON subrelatedpages.relatedcustompageid = subrelated.custompageid WHERE subrelatedpages.path NOT LIKE n'%' + CAST(subrelated.relatedcontentid AS VARCHAR) + n'%' ) SELECT custompageid, relatedcustompageid, [level], [path] FROM relatedpages ORDER BY level 列进行处理,以避免出现周期

但是我怎么知道SQL Server是执行深度优先还是深度优先搜索?

0 个答案:

没有答案