我有一张表,其中有16M记录。我正在尝试使用下面的查询来使用CTE查找所需的结果,但运行查询需要超过45个Minuit。现在我应该打破查询还是有以下查询的替代方案?
DELETE FROM Table1
WHERE IDD_Memid = @memid;
WITH CTE_Example(MJD_Introducer, MJD_memid, Depth, MJD_doj, mjd_lr)
AS (SELECT MJD_Introducer, MJD_MemID, 0 AS Depth, MJD_DOJ,
MJD_LR
FROM Table2
WHERE MJD_MemID = '10000001'
UNION ALL
SELECT MJD_Introducer,
MJD_MemID,
CTE_Example.Depth + 1 AS Depth,
MJD_DOJ,
MJD_LR
FROM Table2
JOIN CTE_Example ON Table2.MJD_Introducer = CTE_Example.MJD_memid)
INSERT INTO Table1(IDD_MEMID, IDD_DOWNID,
IDD_LEVEL)
SELECT @memid AS 'IDD_MEMID', MJD_memid AS 'IDD_DOWNID',
Depth AS 'IDD_LEVEL'
FROM CTE_Example
WHERE CTE_Example.MJD_memid <> @memid
ORDER BY CTE_Example.Depth, CTE_Example.MJD_memid
OPTION (MAXRECURSION 30000);