我有一张有 200,000 行的表。我创建了一个视图,我根据不同的标准从该表中删除数据片段,这些标准符合我对构成重复记录的定义。我在下面有这样做的代码,我想知道是否有人可以建议一种更快/更有效的方法来编写这个查询。目前执行大约需要 20 秒,但我希望最多几秒钟来执行这个查询,如果不是更少的话。我正在使用 SQL Server 2005。我对 SQL 的了解非常初级,我很感激任何帮助。
WITH dsm_hardware_basic_cte AS
(
SELECT TOP 100 PERCENT
dbo.dsm_hardware_basic.[UUID]
,dbo.dsm_hardware_basic.[Name]
,dbo.dsm_hardware_basic.[LastAgentExecution]
,dbo.dsm_hardware_basic.[MaxUserRegistration]
,REPLACE(RIGHT([MaxUserRegistration], CHARINDEX('/', REVERSE([MaxUserRegistration])) - 1),'_ADMIN','') AS [MaxUserUsername]
,dbo.dsm_hardware_basic.[LastUserRegistration]
,REPLACE(RIGHT([LastUserRegistration], CHARINDEX('/', REVERSE([LastUserRegistration])) - 1),'_ADMIN','') AS [LastUserUsername]
,dbo.dsm_hardware_basic.[IPAddress]
,dbo.dsm_hardware_basic.[HostName]
,dbo.dsm_hardware_basic.[MACAddress]
FROM dbo.dsm_hardware_basic
)
SELECT TOP 100 PERCENT
dsm_hardware_basic_cte.[UUID]
,dsm_hardware_basic_cte.[Name]
,dsm_hardware_basic_cte.[LastAgentExecution]
,dsm_hardware_basic_cte.[MaxUserRegistration]
,dsm_hardware_basic_cte.[LastUserRegistration]
,dsm_hardware_basic_cte.[IPAddress]
,dsm_hardware_basic_cte.[HostName]
,dsm_hardware_basic_cte.[MACAddress]
FROM dsm_hardware_basic_cte
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [NameMACRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameMAC_filtered
ON duplicate_NameMAC_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameMAC_filtered.[NameMACRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [HostName] ORDER BY [LastAgentExecution] DESC) AS [NameHostNameRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameHostName_filtered
ON duplicate_NameHostName_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameHostName_filtered.[NameHostNameRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [MACAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameMACRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_HostNameMAC_filtered
ON duplicate_HostNameMAC_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_HostNameMAC_filtered.[HostNameMACRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [HostName], [IPAddress] ORDER BY [LastAgentExecution] DESC) AS [HostNameIPAddressRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_HostNameIPAddress_filtered
ON duplicate_HostNameIPAddress_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_HostNameIPAddress_filtered.[HostNameIPAddressRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [MaxUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameMaxUserRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameMaxUser_filtered
ON duplicate_NameMaxUser_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameMaxUser_filtered.[NameMaxUserRowNum] = 1
INNER JOIN
(
SELECT [UUID]
,ROW_NUMBER() OVER (PARTITION BY [Name], [LastUserUsername] ORDER BY [LastAgentExecution] DESC) AS [NameLastUserRowNum]
FROM dsm_hardware_basic_cte
) AS duplicate_NameLastUser_filtered
ON duplicate_NameLastUser_filtered.[UUID] = dsm_hardware_basic_cte.[UUID]
AND duplicate_NameLastUser_filtered.[NameLastUserRowNum] = 1