SQL where 条件中的 IN 子句导致大数据出现性能问题
SQL IN clause in where condition causing performance issue with large data
以下查询在我的数据库中运行良好,但它在客户数据库中产生了巨大的性能问题。我知道我在 where 条件中使用了 IN 子句,这给我带来了这个问题。但我不知道如何解决这个问题。
declare @AccountId int
set @AccountId = 1200
declare @IsLinkedAccountsNotes bit
set @IsLinkedAccountsNotes =1
declare @EventType varchar(100)
set @EventType = ''
SELECT
u.loginName as InteractionLoginName,
u.userName as InteractionUserName,
e.*
FROM
lat.events e
INNER JOIN dbo.MasterEvents me ON me.EventId = e.EventId
LEFT JOIN dbo.Users u ON e.UserId = u.ID
WHERE
(me.AccountId = @AccountId OR
(@IsLinkedAccountsNotes = 1 AND me.AccountId IN (SELECT DISTINCT [number] FROM dbo.Linking_LinkedAccounts WHERE linked_number = @AccountId) AND e.EventType = 'AccountNoteAdded'))
我知道是 where 子句中的第二个条件导致了问题。而且我在各种帖子中看到使用连接可以解决这个问题。但是我不知道如何在 where 条件下使用 join 。
或者有没有其他方法可以做到这一点。
请帮忙。
可以试试
JOIN (SELECT DISTINCT [number] FROM dbo.Linking_LinkedAccounts WHERE linked_number = @AccountId) lla ON lla.number = me.AccountId
LEFT JOIN 并过滤掉 where 条件中的空值。
EXISTS 而不是 IN。
您的问题很可能是 "catch all query"。您可以尝试按以下方式拆分案例
SELECT u.loginName AS InteractionLoginName,
u.userName AS InteractionUserName,
e.*
FROM lat.events e
INNER JOIN dbo.MasterEvents me
ON me.EventId = e.EventId
LEFT JOIN dbo.Users u
ON e.UserId = u.ID
WHERE me.AccountId = @AccountId
UNION ALL
SELECT u.loginName AS InteractionLoginName,
u.userName AS InteractionUserName,
e.*
FROM lat.events e
INNER JOIN dbo.MasterEvents me
ON me.EventId = e.EventId
LEFT JOIN dbo.Users u
ON e.UserId = u.ID
WHERE @IsLinkedAccountsNotes = 1
AND e.EventType = 'AccountNoteAdded'
AND me.AccountId IN (SELECT [number]
FROM dbo.Linking_LinkedAccounts
WHERE linked_number = @AccountId
AND [number] <> @AccountId)
以下查询在我的数据库中运行良好,但它在客户数据库中产生了巨大的性能问题。我知道我在 where 条件中使用了 IN 子句,这给我带来了这个问题。但我不知道如何解决这个问题。
declare @AccountId int
set @AccountId = 1200
declare @IsLinkedAccountsNotes bit
set @IsLinkedAccountsNotes =1
declare @EventType varchar(100)
set @EventType = ''
SELECT
u.loginName as InteractionLoginName,
u.userName as InteractionUserName,
e.*
FROM
lat.events e
INNER JOIN dbo.MasterEvents me ON me.EventId = e.EventId
LEFT JOIN dbo.Users u ON e.UserId = u.ID
WHERE
(me.AccountId = @AccountId OR
(@IsLinkedAccountsNotes = 1 AND me.AccountId IN (SELECT DISTINCT [number] FROM dbo.Linking_LinkedAccounts WHERE linked_number = @AccountId) AND e.EventType = 'AccountNoteAdded'))
我知道是 where 子句中的第二个条件导致了问题。而且我在各种帖子中看到使用连接可以解决这个问题。但是我不知道如何在 where 条件下使用 join 。 或者有没有其他方法可以做到这一点。
请帮忙。
可以试试
JOIN (SELECT DISTINCT [number] FROM dbo.Linking_LinkedAccounts WHERE linked_number = @AccountId) lla ON lla.number = me.AccountId
LEFT JOIN 并过滤掉 where 条件中的空值。
EXISTS 而不是 IN。
您的问题很可能是 "catch all query"。您可以尝试按以下方式拆分案例
SELECT u.loginName AS InteractionLoginName,
u.userName AS InteractionUserName,
e.*
FROM lat.events e
INNER JOIN dbo.MasterEvents me
ON me.EventId = e.EventId
LEFT JOIN dbo.Users u
ON e.UserId = u.ID
WHERE me.AccountId = @AccountId
UNION ALL
SELECT u.loginName AS InteractionLoginName,
u.userName AS InteractionUserName,
e.*
FROM lat.events e
INNER JOIN dbo.MasterEvents me
ON me.EventId = e.EventId
LEFT JOIN dbo.Users u
ON e.UserId = u.ID
WHERE @IsLinkedAccountsNotes = 1
AND e.EventType = 'AccountNoteAdded'
AND me.AccountId IN (SELECT [number]
FROM dbo.Linking_LinkedAccounts
WHERE linked_number = @AccountId
AND [number] <> @AccountId)