我有一组表,我要加入这些表以创建一个记录集,然后对该集合运行附加选择以检索一些记录。 代码中的查询已在视图中用作CTE。
仅检查子查询时,它的运行速度非常快,如0.01-0.02秒。 如果使用临时表检索记录,则同样适用。当我计划在视图中使用它时,临时表解决方案超出了范围。常规查询的运行时间为37-50分钟。
SELECT
CallDate
,MediaChannel
,SubCategory
,Vendor
,BusinessVertical
,SUM(NumberOfLeads) AS NumberOfLeads
,CASE
WHEN SUM([CostPerLead]) <> 0
THEN SUM([CostPerLead])
ELSE NULL
END AS Cost
,[CostPerLead]
,SourceName
,ParentLeadSource
,IsBillable
,dvce_type
FROM (
SELECT [PhoneLabel]
,[DialogTechCallId] = cd.DialogTechCallId
,[LeadId] = c.LeadId
,[CostPerLead] = CAST(cpl.cost AS INT)
,[SourceName] = bt.LeadCompany
,[ParentLeadSource] = ftlc.fruit
,[DialogTechPhoneNumber] = cd.CalledNumber
,[CallDate] = CAST(cd.[CallDateTime] AS DATE)
,[CallType] = cd.CallType
,[TalkTime] = cd.[TalkTimeMinutes]
,[TalkTimeSeconds] = CASE
WHEN ISNUMERIC(cd.[TalkTimeMinutes]) = 1
THEN CAST(cd.[TalkTimeMinutes] AS DECIMAL(10, 2)) * 60
ELSE 0
END
,[TimeToQualify] = bt.Billabletime
,[IsBillable] = CASE
WHEN ISNUMERIC(cd.[TalkTimeMinutes]) = 1
THEN CASE
WHEN CAST(cd.[TalkTimeMinutes] AS DECIMAL(10, 2)) * 60
>= CAST(bt.Billabletime AS INT)
THEN 1
WHEN bt.Billabletime = 900
THEN 1
ELSE 0
END
ELSE 0
END
,[MediaChannel] = ftlc.channel2
,[SubCategory] = ftlc.sub
,[Vendor] = ftlc.vendor
,[BusinessVertical] = ftlc.business_vertical
,[NumberOfLeads] = 1
,[dvce_type] = ftlc.dvce_type
FROM [dbo].[Abc] d WITH (NOLOCK)
LEFT JOIN [dt].[cde] cd WITH (NOLOCK) ON d.FullDate =
CAST(cd.CallDateTime AS DATE)
LEFT JOIN [dt].[efg] c WITH (NOLOCK) ON cd.DialogtechCallId =
c.DialogTechCallid
INNER JOIN [dt].[hij] m WITH (NOLOCK) ON cd.CallerId =
m.DialogTechPhoneNumber
INNER JOIN [dt].[klm] bt WITH (NOLOCK) ON m.LeadSourceInfoId =
bt.LeadSourceId AND cd.[CallDateTime] BETWEEN bt.[StartDateTime]
AND ISNULL(bt.[EndDateTime], GETDATE())
INNER JOIN [dbo].[jkl] ftlc WITH (NOLOCK) ON bt.ParentLeadSource =
ftlc.fruit
INNER JOIN dbo.xyz cpl WITH (NOLOCK) ON ftlc.lead_company =
cpl.lead_company AND cd.[CallDateTime] >= cpl.[start_date]
AND cd.[CallDateTime] <= ISNULL(cpl.[end_date], GETDATE())
WHERE CAST(cd.[CallDateTime] AS DATE) >= '2018-08-01'
AND CASE WHEN ISNUMERIC(cd.[TalkTimeMinutes]) = 1
THEN CASE
WHEN CAST(cd.[TalkTimeMinutes] AS DECIMAL(10, 2)) * 60
>= CAST(bt.Billabletime AS INT)
THEN 1
WHEN bt.Billabletime = 900
THEN 1
ELSE 0
END
ELSE 0
END = 1
) sub
GROUP BY
CallDate
,MediaChannel
,SubCategory
,Vendor
,BusinessVertical
,SourceName
,ParentLeadSource
,IsBillable
,dvce_type
,CostPerLead;
----该查询正在视图中使用,因此需要解决子查询问题并减少运行时间。
答案 0 :(得分:0)
您的子查询中有一些未在外部查询中使用的数据。您的某些联接还会将NULL值引入NULL值,这会降低性能。
在不知道您的表结构或函数,也没有任何实际数据的情况下,我想出了以下简化的查询版本。它会从子查询中删除未使用的列,随后无用的连接,简化的App_Resources/Android/src/main/AndroidManifest.xml
语句以及从主查询中删除CASE
,因为您正在对此进行汇总。
CostPerLead