我有以下疑问。我不确定为什么使用内连接的人花费更少的时间来执行,不应该花费比第一个花费更多的时间?
查询#1:
CREATE TABLE #TEMPORAL
(
ID INT,
NAME NVARCHAR(300),
LASTNAME NVARCHAR(100),
PARENTID INT,
[LEVEL] INT
)
;WITH CTE AS
(
SELECT
ID, PARENTID, 0 [LEVEL], FIRSTNAME, LASTNAME
FROM
PERSON
WHERE
ID = 1123643
UNION ALL
SELECT
P.ID, P.PARENTID, C.[LEVEL] + 1, P.FIRSTNAME, P.LASTNAME
FROM
PERSON P
INNER JOIN
CTE C ON C.ID = P.PARENTID
)
INSERT INTO #TEMPORAL (ID, NAME, LASTNAME, PARENTID, [LEVEL])
(SELECT
ID, FIRSTNAME, LASTNAME, [LEVEL]
FROM
CTE)
SELECT ID, NAME, LASTNAME, PARENTID, [LEVEL]
FROM #TEMPORAL
查询#2:
CREATE TABLE #TEMPORAL
(
ID INT,
NAME NVARCHAR(300),
LASTNAME NVARCHAR(100),
PARENTID INT,
[LEVEL] INT
)
;WITH CTE AS
(
SELECT
ID, PARENTID, 0 [LEVEL]
FROM
PERSON
WHERE
ID = 1123643
UNION ALL
SELECT
P.ID, P.PARENTID, C.[LEVEL] + 1
FROM
PERSON P
INNER JOIN
CTE C ON C.ID = P.PARENTID
)
INSERT INTO #TEMPORAL (ID, PARENTID, [LEVEL])
(SELECT ID, PARENTID, [LEVEL]
FROM CTE)
SELECT
ID, PARENTID, P.FIRSTNAME, P.LASTNAME
FROM
#TEMPORAL T
INNER JOIN
PERSON P ON T.ID = P.ID
所以,我对于为什么会发生这种情况感到非常困惑。你能给我一些解释吗?此外,如果有更好的选择来实现我想要的东西,那将很高兴知道。