我的SQL查看查询有问题。我有几个重复的查询,我认为它不应该是这样的。这只是一个较大的一部分:
WITH auditlogs ([AuditLogId], [OperationCode], [Operation], [Table], [RowId], [Timestamp], [UserUid], [UserName], [IpAddress], [OperationSet], [ContextUid], [Field], [OldValue], [NewValue]) AS
(
SELECT
al.[Id] AS [AuditLogId],
al.[Operation] as [OperationCode],
CASE al.[Operation]
WHEN 0 THEN 'New'
WHEN 1 THEN 'Update'
WHEN 2 THEN 'Delete'
ELSE 'Warning'
END AS [Operation],
al.[EntityName] AS [Table],
CASE al.[EntityId]
WHEN -1 THEN NULL
ELSE al.[EntityId]
END AS [RowId],
al.[Timestamp],
al.[UserUid],
al.[UserName],
al.[IpAddress],
al.[OperationSet],
al.[ContextUid],
alf.[Name],
alf.[OldValue],
alf.[NewValue]
FROM [AuditLog] AS al
INNER JOIN [AuditLogField] AS alf ON alf.AuditLogId = al.Id
)
SELECT [AuditLogId], --- ATTACHUSERS
[OperationCode], [Operation], [Table], [RowId], [Timestamp], alx.[UserUid], [UserName],
[IpAddress], [OperationSet], [ContextUid], 'User' AS [Field], null, u.[FirstName] + ' ' + u.[LastName], s.Id
FROM (
SELECT [AuditLogId],
[OperationCode], [Operation], [Table], [RowId], [Timestamp], [UserUid], [UserName],
[IpAddress], [OperationSet], [ContextUid],
MAX(case when [Field] = 'UserUid' then [NewValue] end) AS AddedUserUid,
MAX(case when [Field] = 'TenantId' then [NewValue] end) TenantId
FROM [auditlogs] AS al
WHERE al.[Table] = 'TenantUser' AND OperationCode = 0
GROUP BY [AuditLogId], [OperationCode], [Operation], [Table], [RowId], [Timestamp], al.[UserUid], [UserName],
[IpAddress], [OperationSet], [ContextUid]
) AS alx
INNER JOIN [User] AS u ON u.[Uid] = CAST(alx.AddedUserUid AS uniqueidentifier)
INNER JOIN [Tenant] AS t ON alx.TenantId = t.Id
INNER JOIN [Subscription] AS s ON s.TenantId = t.id
UNION ALL
SELECT al.[AuditLogId], --- DETTACHUSERS
al.[OperationCode], al.[Operation], al.[Table], al.[RowId], al.[Timestamp], al.[UserUid], al.[UserName],
al.[IpAddress], al.[OperationSet], al.[ContextUid], 'User' AS [Field], null, u.[FirstName] + ' ' + u.[LastName], s.Id
FROM [auditlogs] AS al
INNER JOIN (
SELECT TOP 1 *
FROM (
SELECT [AuditLogId],
[OperationCode], [Operation], [Table], [RowId], [Timestamp], [UserUid], [UserName],
[IpAddress], [OperationSet], [ContextUid],
MAX(case when [Field] = 'UserUid' then [NewValue] end) AS AddedUserUid,
MAX(case when [Field] = 'TenantId' then [NewValue] end) TenantId
FROM [auditlogs] AS al
WHERE al.[Table] = 'TenantUser' AND OperationCode = 0
GROUP BY [AuditLogId], [OperationCode], [Operation], [Table], [RowId], [Timestamp], al.[UserUid], [UserName],
[IpAddress], [OperationSet], [ContextUid]
) AS added
ORDER BY [Timestamp] DESC
) AS alx ON alx.RowId = al.RowId AND al.OperationCode = 2
INNER JOIN [User] AS u ON u.[Uid] = CAST(alx.AddedUserUid AS uniqueidentifier)
INNER JOIN [Tenant] AS t ON alx.TenantId = t.Id
INNER JOIN [Subscription] AS s ON s.TenantId = t.id
我知道我的视图可以由其他人组成,我的意思是,将它分成几个较小的视图,以便重用它们并避免重复的部分。但是,我想知道这是否是一个很好的方法,或者是否有另一种更好的方法。
拆分它,是最好的解决方案吗?
答案 0 :(得分:0)
CTE是在查询中重构代码的一种非常好的方法。
只需注意一点:这不会影响性能。 SQL Server仍然分别优化每个引用,无论好坏(在我看来)。这是微软的一个已知问题,他们甚至撰写了有关它的白皮书。例如,Here是对此优化的正式请求。
作为第二个注释,通常当您将表连接到自身的摘要时,窗口函数将更有效。你的查询相当复杂,所以我不确定这是否会有所帮助。
是的,你可以拥有多个cte。这是一个基本的例子:
with digits as (select 0 as digit union all select 1 as digit),
digits2 as (select d1.digit + d2.digit*10 as num from digits d1 cross join digits d2)
select *
from digits2
顺便说一下,这可以用来在SQL Server中生成数字序列。