简体   繁体   English

SQL 长时间运行查询/最大化服务器资源,例如 RAM/CPU

[英]SQL Long running query / maxing out server resource e.g. RAM/CPU

I originally posted the following thread SQL Query - long running / taking up CPU resource我最初发布了以下线程SQL 查询 - 长时间运行/占用 CPU 资源

My issue was the SARGABILITY of my query, having addressed that (see the previous thread but in short I was using a lot of ISNULL functions which were bypassing the index scans) I am now having further issues.我的问题是我的查询的 SARGABILITY,已经解决了这个问题(请参阅上一个线程,但简而言之,我使用了很多绕过索引扫描的 ISNULL 函数)我现在遇到了更多问题。

My SQL server settings are as below:我的 SQL 服务器设置如下:

cost threshold for parallelism 5并行性的成本阈值5

max degree of parallelism 0最大并行度0

My query still takes 2:13 to run and causes CPU / Memory spikes, I have a largely capable server eg 64GB RAM so resource is not the issue.我的查询仍然需要 2:13 才能运行并导致 CPU/内存峰值,我有一个功能强大的服务器,例如 64GB RAM,因此资源不是问题。 See query below:请参阅下面的查询:

WITH CTE AS 
(
    SELECT R.Id AS ResultId,
        r.JobId,
        r.CandidateId,
        R.Email,
        CAST(0 AS BIT) AS EmailSent,
        NULL AS EmailSentDate,
        'PICKUP' AS EmailStatus,
        GETDATE() AS CreateDate,
        C.Id AS UserId,
        C.Email AS UserEmail,
        NULL AS Subject
    FROM RESULTS R
    INNER JOIN JOB J ON R.JobId = J.Id
    INNER JOIN Consultant C ON J.UserId = C.Id
    WHERE 
        J.DCApproved = 1
        AND (J.Closed = 0 OR J.Closed IS NULL)
        AND (R.Email <> '' OR R.Email IS NOT NULL)
        AND (R.EmailSent = 0 OR R.EmailSent IS NULL)
        AND R.EmailSentDate IS NULL -- email has not been sent
        AND (R.EmailStatus = '' OR R.EmailStatus IS NULL)
        AND (R.IsEmailSubscribe = 'True' OR R.IsEmailSubscribe IS NULL)
        -- not already been emailed for this job
        AND NOT EXISTS (
            SELECT SMTP.Email
            FROM SMTP_Production SMTP
            WHERE SMTP.JobId = R.JobId AND SMTP.CandidateId = R.CandidateId
        )
        -- not unsubscribed
        AND NOT EXISTS (        
            SELECT u.Id FROM Unsubscribe u
            WHERE (u.EmailAddress = R.Email OR (u.EmailAddress IS NULL AND R.Email IS NULL)) 
        )
        AND NOT EXISTS (
            SELECT SMTP.Id FROM SMTP_Production SMTP
            WHERE SMTP.EmailStatus = 'PICKUP' AND SMTP.CandidateId = R.CandidateId
        )   
        AND C.Id NOT IN (
            -- LIST OF IDS
        )
        AND J.Id NOT IN (
            -- LIST OF IDS
        )
        AND J.ClientId NOT IN 
        (
            -- LIST OF IDS
        )
)

SELECT 
    CTE.ResultId,
    CTE.JobId,
    CTE.CandidateId,
    CTE.Email,
    CTE.EmailSent,
    CTE.EmailSentDate,
    CTE.EmailStatus,
    CTE.CreateDate,
    CTE.UserId,
    CTE.UserEmail,
    NULL
FROM CTE
  INNER JOIN 
    (
        SELECT *, row_number() over(partition by CTE.Email, CTE.CandidateId order by CTE.EmailSentDate desc) as rn
        FROM CTE

    ) DCTE ON CTE.ResultId = DCTE.ResultId AND DCTE.rn = 1

See Indexes for the Results table below, something does not seem right on the below:请参阅下面结果表的索引,下面的内容似乎不正确:

/****** Object:  Index [_dta_index_Results_7_2107154552__K35_K2_K3_K34_K36_K8_K33_K1]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [_dta_index_Results_7_2107154552__K35_K2_K3_K34_K36_K8_K33_K1] ON [dbo].[Results]
(
    [EmailSentDate] ASC,
    [JobId] ASC,
    [AryaCandidateId] ASC,
    [EmailSent] ASC,
    [EmailStatus] ASC,
    [Email] ASC,
    [IsEmailSubscribe] ASC,
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [ACI_CMT_APPLICANTS]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [ACI_CMT_APPLICANTS] ON [dbo].[Results]
(
    [Email] ASC
)
INCLUDE (   [Id],
    [AryaCandidateId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [ACI_Job]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [ACI_Job] ON [dbo].[Results]
(
    [AryaCandidateId] ASC,
    [JobId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [ACI_Results]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [ACI_Results] ON [dbo].[Results]
(
    [AryaCandidateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [gen_smtp_auto]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [gen_smtp_auto] ON [dbo].[Results]
(
    [EmailSentDate] ASC,
    [Email] ASC,
    [IsEmailSubscribe] ASC,
    [EmailSent] ASC,
    [EmailStatus] ASC
)
INCLUDE (   [Id],
    [JobId],
    [AryaCandidateId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Hot]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Hot] ON [dbo].[Results]
(
    [JobId] ASC,
    [Action] ASC
)
INCLUDE (   [Engaged]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [IX_Results]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [IX_Results] ON [dbo].[Results]
(
    [Id] ASC,
    [JobId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [IX_Results_1]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [IX_Results_1] ON [dbo].[Results]
(
    [Id] ASC,
    [JobId] ASC,
    [AryaCandidateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [JobMetrics]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [JobMetrics] ON [dbo].[Results]
(
    [JobId] ASC,
    [Source] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [KEY_CAMPAIGN]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [KEY_CAMPAIGN] ON [dbo].[Results]
(
    [ResumeDownloadedDate] ASC,
    [ResumeDownloadStatus] ASC,
    [KeywordCampaignId] ASC,
    [Source] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [MISSING_CREATEDATE]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [MISSING_CREATEDATE] ON [dbo].[Results]
(
    [CreateDate] ASC
)
INCLUDE (   [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [MISSING_MOVERSPROB]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [MISSING_MOVERSPROB] ON [dbo].[Results]
(
    [MoversProbability] ASC
)
INCLUDE (   [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [MISSING_SORTORDER]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [MISSING_SORTORDER] ON [dbo].[Results]
(
    [SortOrder] ASC
)
INCLUDE (   [Id]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Proto_Resume_Downloa]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Proto_Resume_Downloa] ON [dbo].[Results]
(
    [JobId] ASC,
    [ResumeDownloadedDate] ASC,
    [ResumeDownloadStatus] ASC,
    [Location] ASC,
    [Source] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Result_Email]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Result_Email] ON [dbo].[Results]
(
    [Email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Result_Email_Send]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Result_Email_Send] ON [dbo].[Results]
(
    [EmailSentDate] ASC
)
INCLUDE (   [Id],
    [JobId],
    [AryaCandidateId],
    [Email],
    [IsEmailSubscribe],
    [EmailSent],
    [EmailStatus]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Results_JobId_ACI_Email]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Results_JobId_ACI_Email] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [Id],
    [AryaCandidateId],
    [Email]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [RESULTS_JOBID_ALL]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [RESULTS_JOBID_ALL] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [Id],
    [AryaCandidateId],
    [CandidateScore],
    [FirstName],
    [LastName],
    [Telephone],
    [Email],
    [AddressLine1],
    [Location],
    [Postcode],
    [Resume],
    [CurrentJob],
    [CurrentCompany],
    [Skills],
    [Experience],
    [Education],
    [AryaUpdateDate],
    [Industry],
    [Source],
    [LinkedIn],
    [Facebook],
    [Twitter],
    [MoversLabel],
    [MoversProbability],
    [SortOrder],
    [CreateDate],
    [ResumeId],
    [IsEmailSubscribe],
    [EmailSent],
    [EmailSentDate],
    [EmailStatus],
    [Registered],
    [HasVoyagerData],
    [Action],
    [Engaged],
    [FormattedCV],
    [CV],
    [DerivedSource],
    [VoyCode],
    [IsEmailEngaged],
    [IsSMSEngaged],
    [KeywordCampaignId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [ResultsGetResultsbyConsultantId]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [ResultsGetResultsbyConsultantId] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [Id],
    [AryaCandidateId],
    [CandidateScore],
    [FirstName],
    [LastName],
    [Telephone],
    [Email],
    [AddressLine1],
    [Location],
    [Postcode],
    [Resume],
    [CurrentJob],
    [CurrentCompany],
    [Skills],
    [Experience],
    [Education],
    [AryaUpdateDate],
    [Industry],
    [Source],
    [LinkedIn],
    [Facebook],
    [Twitter],
    [MoversLabel],
    [MoversProbability],
    [DOB],
    [SortOrder],
    [ResumeDownloaded],
    [ResumeDownloadedDate],
    [ResumeDownloadStatus],
    [CreateDate],
    [ResumeId],
    [IsEmailSubscribe],
    [EmailSent],
    [EmailSentDate],
    [EmailStatus],
    [Action],
    [Engaged],
    [SentToArya],
    [IgnoreEmailSent],
    [IgnoreEmailSentDate],
    [FormattedCV],
    [CV],
    [DerivedSource],
    [IsEmailEngaged],
    [IsSMSEngaged]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Stats_Results_JOB_ACI_ACTION_ENGAGED]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JOB_ACI_ACTION_ENGAGED] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [AryaCandidateId],
    [Action],
    [Engaged]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
/****** Object:  Index [Stats_Results_JobId_ACI]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JobId_ACI] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [AryaCandidateId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Stats_Results_JobId_ACI_Action_Engaged]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JobId_ACI_Action_Engaged] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [AryaCandidateId],
    [Action],
    [Engaged]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Stats_Results_JobId_ACI_DERIVED]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JobId_ACI_DERIVED] ON [dbo].[Results]
(
    [JobId] ASC
)
INCLUDE (   [AryaCandidateId],
    [DerivedSource]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Stats_Results_JobId_SOURCE_ACI]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JobId_SOURCE_ACI] ON [dbo].[Results]
(
    [JobId] ASC,
    [Source] ASC
)
INCLUDE (   [AryaCandidateId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object:  Index [Stats_Results_JobId_Source_ACI_V2]    Script Date: 17/10/2018 15:06:18 ******/
CREATE NONCLUSTERED INDEX [Stats_Results_JobId_Source_ACI_V2] ON [dbo].[Results]
(
    [JobId] ASC,
    [Source] ASC
)
INCLUDE (   [AryaCandidateId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Results] ADD  CONSTRAINT [DF_Results_CreateDate]  DEFAULT (getdate()) FOR [CreateDate]
GO

I have some suggestions for you to reduce the execution time:我有一些建议可以减少执行时间:

  1. if the email is empty for the master record, you don't need to run the sub query:如果主记录的电子邮件为空,则不需要运行子查询:

so instead of this statement:所以而不是这个声明:

AND NOT EXISTS (        
            SELECT u.Id FROM Unsubscribe u
            WHERE (u.EmailAddress = R.Email OR (u.EmailAddress IS NULL AND R.Email IS NULL)) 
        )

use below statement:使用以下语句:

 AND (NOT EXISTS (        
            SELECT u.Id FROM Unsubscribe u
            WHERE u.EmailAddress = R.Email ) 
        ) or  R.Email IS NULL) -- you dont need to check is it is null 
  1. I recommend you to reduce or notation as much as you can, please try to use union instead of OR.我建议您尽可能减少或使用符号,请尝试使用 union 而不是 OR。 You can find some examples in below link:您可以在以下链接中找到一些示例:

SQL Performance UNION vs OR SQL 性能 UNION 与 OR

  1. as I understood you can use JOBID to filter SMTP_Production records, If you can do so:据我了解,您可以使用 JOBID 来过滤 SMTP_Production 记录,如果可以:

instead of this statement而不是这个声明

 AND NOT EXISTS (
            SELECT SMTP.Id FROM SMTP_Production SMTP
            WHERE SMTP.EmailStatus = 'PICKUP' AND SMTP.CandidateId = R.CandidateId -- can we add SMTP.JobId = R.JobId
        ) 

you can use below你可以在下面使用

 AND NOT EXISTS (
            SELECT SMTP.Id FROM SMTP_Production SMTP
            WHERE SMTP.EmailStatus = 'PICKUP' AND SMTP.CandidateId = R.CandidateId and SMTP.JobId = R.JobId 
        ) 

final version of the query might be like this:查询的最终版本可能是这样的:

WITH CTE AS 
(
    SELECT R.Id AS ResultId,
        r.JobId,
        r.CandidateId,
        R.Email,
        CAST(0 AS BIT) AS EmailSent,
        NULL AS EmailSentDate,
        'PICKUP' AS EmailStatus,
        GETDATE() AS CreateDate,
        C.Id AS UserId,
        C.Email AS UserEmail,
        NULL AS Subject
    FROM RESULTS R
    INNER JOIN JOB J ON R.JobId = J.Id
    INNER JOIN Consultant C ON J.UserId = C.Id
    WHERE 
        J.DCApproved = 1
        AND (J.Closed <> 1)
        AND (R.Email <> '' OR R.Email IS NOT NULL)
        AND (R.EmailSent <> 1)
        AND R.EmailSentDate IS NULL -- email has not been sent
        AND (R.EmailStatus = '' OR R.EmailStatus IS NULL)
        AND (R.IsEmailSubscribe <> 'False')
        -- not already been emailed for this job
        AND NOT EXISTS (
            SELECT SMTP.Email
            FROM SMTP_Production SMTP
            WHERE SMTP.JobId = R.JobId AND SMTP.CandidateId = R.CandidateId
        )
        -- not unsubscribed
          AND ((NOT EXISTS (        
            SELECT u.Id FROM Unsubscribe u
            WHERE u.EmailAddress = R.Email ) 
        ) or  R.Email IS NULL) )
         AND NOT EXISTS (
            SELECT SMTP.Id FROM SMTP_Production SMTP
            WHERE SMTP.EmailStatus = 'PICKUP' AND SMTP.CandidateId = R.CandidateId and SMTP.JobId = R.JobId 
        ) 
        AND C.Id NOT IN (
            -- LIST OF IDS
        )
        AND J.Id NOT IN (
            -- LIST OF IDS
        )
        AND J.ClientId NOT IN 
        (
            -- LIST OF IDS
        )
)

SELECT 
    CTE.ResultId,
    CTE.JobId,
    CTE.CandidateId,
    CTE.Email,
    CTE.EmailSent,
    CTE.EmailSentDate,
    CTE.EmailStatus,
    CTE.CreateDate,
    CTE.UserId,
    CTE.UserEmail,
    NULL
FROM CTE
  INNER JOIN 
    (
        SELECT *, row_number() over(partition by CTE.Email, CTE.CandidateId order by CTE.EmailSentDate desc) as rn
        FROM CTE

    ) DCTE ON CTE.ResultId = DCTE.ResultId AND DCTE.rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 慢 SQL 查询最大化 CPU - Slow SQL query maxing out CPU SQL查询-长时间运行/占用CPU资源 - SQL Query - long running / taking up CPU resource 尽管在例如 Access 中工作,SQL 查询却没有提供任何结果? - SQL Query delivers no results despite working in e.g. Access? 修复连接池最大化/查找到 SQL 服务器的打开连接 - Fixing Connection Pool maxing out / Finding open connections to SQL Server SQL Server CPU利用率与长时间运行的查询 - SQL Server CPU utilization vs Long running queries SQL搜索查询多个Where子句,例如,名字和姓氏 - SQL Search Query for Multiple Where Clauses e.g. Firstname AND Surname 如何使用SQL查询获取Progress OpenEdge数据库信息,例如数据库版本 - How to use a SQL query to get the Progress OpenEdge database information, e.g. database version SQL查询选择例如进行了3次特定购买的买方 - SQL query to select e.g. buyer which made 3 specific buys 是否在没有安装程序的情况下分发SQL Server Express(例如,松散的DLL)? - Distributing SQL Server Express without an installer (e.g. loose DLLs)? 是否可以“测量” Web场条件下每个用户对SQL Server数据库的使用情况(例如,以MB为单位)? - Is it possible to “measure” the usage (e.g. in MBs) per user, of an SQL Server database in web farm conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM