我有两个数据库。一个是带有修订号和日期的颠覆日志,另一个包含修订期间更改的修订号和路径。我的查询每月查找最承诺的目录。问题是它需要几分钟才能运行。谁能帮我优化这个野兽般的查询?我确信有更好的方法来做到这一点。
SELECT [Directory]
,[Month]
,COUNT([PathMonth]) OVER (PARTITION BY [PathMonth]) AS [Count] INTO ##temp
FROM
(SELECT [Path]
,[Month]
,[Directory]
,[Directory] + [Month] AS [PathMonth]
FROM
(SELECT [Path]
,SUBSTRING([Path], 0, LEN([Path]) - CHARINDEX('/', REVERSE([Path])) + 1) AS [Directory]
,CONVERT(CHAR(4), [LogDate], 120) + '-' + CONVERT(CHAR(2), [LogDate], 110) AS [Month]
FROM [SubversionLog] JOIN [PathsLog] ON [SubversionLog].[Revision] = [PathsLog].[Revision]
WHERE [Path] LIKE '/%/%/%/_%'
) one) two
ORDER BY [Month]
SELECT * INTO ##tempTwo
FROM ##temp
GROUP BY [Directory], [Month], [Count]
SELECT t1.[Directory]
,t1.[Month]
,t1.[Count]
FROM ##tempTwo t1 LEFT JOIN ##tempTwo t2 ON t1.[Month] = t2.[Month] AND t1.[Count] < t2.[Count]
WHERE t2.[Count] IS NULL
GROUP BY t1.[Directory], t1.[Month], t1.[Count]
ORDER BY [Month] DESC
IF EXISTS (SELECT * FROM ##temp)
DROP TABLE ##temp
IF EXISTS (SELECT * FROM ##tempTwo)
DROP TABLE ##tempTwo
这项工作的一半是将 YYYY-MM-DD HH:MM:SS.SSS 时间戳格式化为 YYYY-MM 并将文件名路径转换为目录。