LINQ2SQL:优化查询?LINQ2SQL

2023-09-06 08:22:23 作者:亡鱼深海花夕拾

我可以优化以任何方式下面的查询:

Can I optimized the following query in any way:

我们的目标是要设法找到4类根据其增值税和电子邮件也存在第二类型的客户。客户机可以有一个或多个clientusers

The goal is to try to find clients of type 4 that also exists as type2 based on their VAT and Email. A client can have one or more clientusers.

(我都合适的索引设置,我保证)

(I have all the appropriate indexes set, I promise)

from e in Clients.Where(h => h.ClientTypeId==4)
 join u in ClientUsers on e.Id equals u.ClientId
 where
 e.DeleteFlag.Equals("n") &&
 (

     (!(e.VAT == null || e.VAT.Equals("")) && Clients.Any(f => f.ClientTypeId == 2 && f.VAT.Equals(e.VAT)))

     || (!(e.Email == null || e.Email.Equals("")) &&
    	 (
    		 Clients.Any(f => f.ClientTypeId == 2 && f.Email.ToLower().Equals(e.Email.ToLower()))
    		 || (from f in ClientUsers join q in Clients on f.ClientId equals q.Id where f.Email.Equals(e.Email.ToLower()) && q.ClientTypeId == 2 select f.Id).Any()
    	 )
     )

     || (!(u.Email == null || u.Email.Equals("")) &&
    	 (
    		 Clients.Any(f => f.ClientTypeId == 2 && f.Email.ToLower().Equals(u.Email.ToLower()))
    		 || (from f in ClientUsers join q in Clients on f.ClientId equals q.Id where f.Email.Equals(u.Email.ToLower()) && q.ClientTypeId == 2 select f.Id).Any()
    	 )
     )

 )
 select e

结果是:

-- Region Parameters
DECLARE @p0 VarChar(1) SET @p0 = 'n'
DECLARE @p1 NVarChar(1) SET @p1 = ''
DECLARE @p2 Int SET @p2 = 2
DECLARE @p3 NVarChar(1) SET @p3 = ''
DECLARE @p4 Int SET @p4 = 2
DECLARE @p5 Int SET @p5 = 2
DECLARE @p6 NVarChar(1) SET @p6 = ''
DECLARE @p7 Int SET @p7 = 2
DECLARE @p8 Int SET @p8 = 2
DECLARE @p9 Int SET @p9 = 4
-- EndRegion
SELECT [t0].[Id], [t0].[AccountId], [t0].[CompanyName], [t0].[Address], [t0].[Address2], [t0].[PostCode], [t0].[PostArea], [t0].[ContactPerson], [t0].[Phone], [t0].[Email], [t0].[VAT], [t0].[Webpage], [t0].[Description], [t0].[Active], [t0].[PreRegcode], [t0].[PreRegcheck], [t0].[AccountCreated], [t0].[UpdateTimeStamp], [t0].[DeleteFlag], [t0].[ClientTypeId], [t0].[CampaignCodeId], [t0].[PayexId], [t0].[ApiKey], [t0].[InvoiceName], [t0].[Source], [t0].[ProspectStatusId], [t0].[ProspectLastCommentId], [t0].[ProspectCallbackDate]
FROM [Clients] AS [t0]
INNER JOIN [ClientUsers] AS [t1] ON ([t0].[Id]) = [t1].[ClientId]
WHERE ([t0].[DeleteFlag] = @p0) AND (((NOT (([t0].[VAT] IS NULL) OR ([t0].[VAT] = @p1))) AND (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [Clients] AS [t2]
    WHERE ([t2].[ClientTypeId] = @p2) AND ([t2].[VAT] = [t0].[VAT])
    ))) OR ((NOT (([t0].[Email] IS NULL) OR ([t0].[Email] = @p3))) AND ((EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [Clients] AS [t3]
    WHERE ([t3].[ClientTypeId] = @p4) AND (LOWER([t3].[Email]) = LOWER([t0].[Email]))
    )) OR (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [ClientUsers] AS [t4]
    INNER JOIN [Clients] AS [t5] ON [t4].[ClientId] = ([t5].[Id])
    WHERE ([t4].[Email] = LOWER([t0].[Email])) AND ([t5].[ClientTypeId] = @p5)
    )))) OR ((NOT (([t1].[Email] IS NULL) OR ([t1].[Email] = @p6))) AND ((EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [Clients] AS [t6]
    WHERE ([t6].[ClientTypeId] = @p7) AND (LOWER([t6].[Email]) = LOWER([t1].[Email]))
    )) OR (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [ClientUsers] AS [t7]
    INNER JOIN [Clients] AS [t8] ON [t7].[ClientId] = ([t8].[Id])
    WHERE ([t7].[Email] = LOWER([t1].[Email])) AND ([t8].[ClientTypeId] = @p8)
    ))))) AND ([t0].[ClientTypeId] = @p9)

查询工作,但需要5分钟来运行。

The query works but takes 5 minutes to run.

推荐答案

您使用的是什么数据库整理?查询可以进行重组,简化了一下,但首先它可能是一个主意,删除所有.ToLower等等,除非你使用的是区分大小写的整理...

What db collation are you using? The query can be restructured and simplified a bit, but first it might be an idea to drop all the .ToLower etc unless you are using a case sensitive collation...

编辑::您可以尝试改变你的查询一堆遗留在SQL优化器不自由发挥创意较小联合在一起的查询......(很多或在查询条件往往会导致即使有覆盖索引,以扫描)。

you could try changing your query to a bunch of smaller unioned queries that leave the sql optimizer less freedom to be creative... (many 'or' conditions in a query tend to lead to scans even if there are covering indexes).

例如:

(

from e in dc.Clients
join u in dc.ClientUsers on e.Id equals u.ClientId
join vc in dc.Clients on new { ClientTypeId = 2, e.VAT } equals new { vc.ClientTypeId, vc.VAT }
where e.ClientTypeId == 4
  && e.DeleteFlag.Equals("n")
  && e.VAT != null
  && e.VAT != "" 
select e

).Union(

from e in dc.Clients
join u in dc.ClientUsers on e.Id equals u.ClientId
join ec in dc.Clients on new { ClientTypeId = 2, e.Email } equals new { ec.ClientTypeId, ec.Email }
where e.ClientTypeId == 4
&& e.DeleteFlag.Equals("n")
&& e.Email != null
&& e.Email != ""
select e 

).Union(

from e in dc.Clients
join u in dc.ClientUsers on e.Id equals u.ClientId
join c1u in dc.ClientUsers on e.Email equals new c1u.Email
join c1c in dc.Clients on new { ClientTypeId = 2, c1u.ClientId } equals new { c1c.ClientTypeId, ClientId = c1c.Id }
where e.ClientTypeId==4
&& e.DeleteFlag.Equals("n")
&& e.Email != null
&& e.Email != ""
select e

).Union(

from e in dc.Clients
join u in dc.ClientUsers on e.Id equals u.ClientId
join c2u in dc.ClientUsers on u.Email equals c2u.Email
join c2c in dc.Clients on new { ClientTypeId = 2, c2u.ClientId } equals new { c2c.ClientTypeId, ClientId = c2c.Id }
where e.ClientTypeId==4
&& e.DeleteFlag.Equals("n")
&& u.Email != null
&& u.Email != ""
select e

)