2015-12-29 91 views
0

我有下面的查詢在SQL Server中運行。簡而言之,我有3個有點相同的查詢「聯盟」。當我開始查詢時,它會運行並在「結果」窗格中生成增量結果。但是,一旦它達到264,619條記錄(第一個5分鐘),它就停止生成結果,但查詢正在運行並且已經運行了30分鐘。任何想法爲什麼發生這種情況?我明白下面的查詢是長/半複雜的,所以我的道歉,我不能讓它變得更簡單(我希望有人能夠指出我可以做出查詢的方向簡單一點)。查詢下面是結果窗格的屏幕截圖。我想知道,如果查詢某處或運行僵持進入一個無限循環的/ etc:SQL查詢正在運行,但停止生成增量結果

select 
    Q1.[Transaction Identifier], 
    row_number() over (order by (select 1)) as 'Transaction Sequence Number', 
    Q1.HL_Acct_ID, 
    Q1.AcctNumber, 
    Q1.AcctType, 
    Q1.AcctSource, 
    Q1.DUNS_NBR, 
    Q1.DBPLCR_CONTACT_ID, 
    Q1.CellCode, 
    Q1.OfferCode, 
    Q1.ResponseType, 
    Q1.ResponseDate, 
    Q1.ResponseQuantity, 
    Q1.ResponseValue, 
    Q1.ResponseChannel, 
    Q1.[Cookie ID], 
    Q1.[IP Address], 
    Q1.[Device ID], 
    Q1.[CUSTOM_TEXT_01], 
    Q1.[CUSTOM_TEXT_02], 
    Q1.[CUSTOM_TEXT_03], 
    Q1.[CUSTOM_TEXT_04], 
    Q1.[CUSTOM_TEXT_05] 
    from 
    (
     select 
     'ACCT PROMORESP' as 'Transaction Identifier', 
     s.HL_ACCT_ID as 'HL_Acct_ID', 
     null as [AcctNumber], 
     null as [AcctType], 
     null as [AcctSource], 
     null as [DUNS_NBR], 
     null as [DBPLCR_CONTACT_ID], 
     s.CELLCODE as [CellCode], 
     case 
      when c.URL is not null then c.URL 
      else '-1' 
     end as [OfferCode], 
     case 
      when c.EventDate is not null then 'Click' 
      when c.EventDate is null then 
      case 
      when sub.status = 'unsubscribed' then 'Unsubscribe' 
      when sub.status = 'bounced' then 'Bounce' 
      when sub.status = 'held' then 'Bounce' 
      end 
     end as [ResponseType], 
     convert(varchar, c.EventDate, 112) as [ResponseDate], 
     null as [ResponseQuantity], 
     null as [ResponseValue], 
     'Email' as [ResponseChannel], 
     null as [Cookie ID], 
     null as [IP address], 
     null as [Device ID], 
     null as [CUSTOM_TEXT_01], 
     null as [CUSTOM_TEXT_02], 
     null as [CUSTOM_TEXT_03], 
     null as [CUSTOM_TEXT_04], 
     null as [CUSTOM_TEXT_05] 
     from C1111111.[151222_Hostess Ensemble] s with (nolock) 
     inner join C1111111._subscribers sub with (nolock) on sub.SubscriberKey = s.Email 
     left join C1111111._click c with (nolock) on c.JobID in 
     (
      select j.fkJobId from tblJobs_Lists j with (nolock) 
      inner join customobject c with (nolock) on j.customobjectid = c.customobjectid 
      where c.memberid = 1111111 
      and c.customobjectname like '151222_Hostess Ensemble' 
     ) 
     and c.SubscriberKey = s.Email 
     where c.EventDate is not null or (c.EventDate is null and (sub.status in ('unsubscribed','bounced','held'))) and c.isunique = 1 

     union all 

     select 
     'ACCT PROMORESP' as 'Transaction Identifier', 
     s.HL_ACCT_ID as 'HL_Acct_ID', 
     null as [AcctNumber], 
     null as [AcctType], 
     null as [AcctSource], 
     null as [DUNS_NBR], 
     null as [DBPLCR_CONTACT_ID], 
     s.CELLCODE as [CellCode], 
     '-1' as [OfferCode], 
     case 
      when o.EventDate is not null then 'Message Open' 
      when o.EventDate is null then 
      case 
      when sub.status = 'unsubscribed' then 'Unsubscribe' 
      when sub.status = 'bounced' then 'Bounce' 
      when sub.status = 'held' then 'Bounce' 
      end 
     end as [ResponseType], 
     convert(varchar, o.EventDate, 112) as [ResponseDate], 
     null as [ResponseQuantity], 
     null as [ResponseValue], 
     'Email' as [ResponseChannel], 
     null as [Cookie ID], 
     null as [IP address], 
     null as [Device ID], 
     null as [CUSTOM_TEXT_01], 
     null as [CUSTOM_TEXT_02], 
     null as [CUSTOM_TEXT_03], 
     null as [CUSTOM_TEXT_04], 
     null as [CUSTOM_TEXT_05] 
     from C1111111.[151222_Hostess Ensemble] s with (nolock) 
     inner join C1111111._subscribers sub with (nolock) on sub.SubscriberKey = s.Email 
     left join C1111111._open o with (nolock) on o.JobID in 
     (
      select j.fkJobId from tblJobs_Lists j with (nolock) 
      inner join customobject c with (nolock) on j.customobjectid = c.customobjectid 
      where c.memberid = 1111111 
      and c.customobjectname like '151222_Hostess Ensemble' 
     ) 
     and o.SubscriberKey = s.Email 
     where o.EventDate is not null or (o.EventDate is null and (sub.status in ('unsubscribed','bounced','held'))) and o.isunique = 1 

     UNION ALL 

     select 
     'ACCT PROMORESP' as 'Transaction Identifier', 
     s.HL_ACCT_ID as 'HL_Acct_ID', 
     null as [AcctNumber], 
     null as [AcctType], 
     null as [AcctSource], 
     null as [DUNS_NBR], 
     null as [DBPLCR_CONTACT_ID], 
     s.CELLCODE as [CellCode], 
     '-1' as [OfferCode], 
     null as [ResponseType], 
     convert(varchar, o.EventDate, 112) as [ResponseDate], 
     null as [ResponseQuantity], 
     null as [ResponseValue], 
     'Email' as [ResponseChannel], 
     null as [Cookie ID], 
     null as [IP address], 
     null as [Device ID], 
     null as [CUSTOM_TEXT_01], 
     null as [CUSTOM_TEXT_02], 
     null as [CUSTOM_TEXT_03], 
     null as [CUSTOM_TEXT_04], 
     null as [CUSTOM_TEXT_05] 
     from C1111111.[151222_Hostess Ensemble] s with (nolock) 
     inner join C1111111._subscribers sub with (nolock) on sub.SubscriberKey = s.Email 
     left join C1111111._open o with (nolock) on o.JobID in 
     (
      select j.fkJobId from tblJobs_Lists j with (nolock) 
      inner join customobject c with (nolock) on j.customobjectid = c.customobjectid 
      where c.memberid = 1111111 
      and c.customobjectname like '151222_Hostess Ensemble' 
     ) 
     left join C1111111._click cl with (nolock) on o.JobID in 
     (
      select j.fkJobId from tblJobs_Lists j with (nolock) 
      inner join customobject c with (nolock) on j.customobjectid = c.customobjectid 
      where c.memberid = 1111111 
      and c.customobjectname like '151222_Hostess Ensemble' 
     ) 
     and cl.SubscriberKey = s.Email 
     where cl.EventDate is null and o.EventDate is null and sub.status not in ('unsubscribed','bounced','held')) as Q1 

enter image description here

+1

的原因可能是在查詢計劃中,你得到上面的查詢可見,從我的一個猜測是,該第一個甚至第二個和第一個部分都可以返回該行因爲它們從表中出來,但最後一部分有一個排序或其他類型的阻塞運算符,它需要消耗表中的所有行,然後才能將它們傳遞給父操作符。 –

+3

我會嘗試分別運行3個部件中的每一個。也許第一個需要5分鐘跑,但第二個需要一個小時? – GendoIkari

+2

當我在連接中看到一個子選擇時,我總是評估是否可以使其成爲連接而不是「in」 - 通常它會以更快的速度運行 –

回答

1

沒有表定義,我不能確認這是正確的,但是這將是我拿在上面的簡化,大約在加入條件的兩個補丁的假設:

set transaction isolation level read uncommitted; 
WITH JobIDs AS 
(
    select j.fkJobId from tblJobs_Lists j 
    inner join customobject c on j.customobjectid = c.customobjectid 
    where c.memberid = 1111111 
    and c.customobjectname = '151222_Hostess Ensemble' 
) 
select 
     'ACCT PROMORESP' as 'Transaction Identifier', 
    row_number() over (order by (select 1)) as 'Transaction Sequence Number', 
    s.HL_ACCT_ID as 'HL_Acct_ID', 
     null as [AcctNumber], 
     null as [AcctType], 
     null as [AcctSource], 
     null as [DUNS_NBR], 
     null as [DBPLCR_CONTACT_ID], 
    s.CELLCODE as [CellCode], 
    X.OfferCode, 
    X.ResponseType, 
    convert(varchar, X.EventDate, 112) as [ResponseDate], 
     null as [ResponseQuantity], 
     null as [ResponseValue], 
     'Email' as [ResponseChannel], 
     null as [Cookie ID], 
     null as [IP address], 
     null as [Device ID], 
     null as [CUSTOM_TEXT_01], 
     null as [CUSTOM_TEXT_02], 
     null as [CUSTOM_TEXT_03], 
     null as [CUSTOM_TEXT_04], 
     null as [CUSTOM_TEXT_05] 
    from 
    (
     SELECT 
      s.HL_ACCT_ID, 
      s.CELLCODE, 
      s.Email, 
      case sub.status 
       when 'unsubscribed' then 'Unsubscribe' 
       when 'bounced' then 'Bounce' 
       when 'held' then 'Bounce' 
      end as [ResponseType], 
      sub.status 
     from C1111111.[151222_Hostess Ensemble] s 
     inner join C1111111._subscribers sub on sub.SubscriberKey = s.Email 
    ) as s 

    left join C1111111._open o on o.JobID in (select fkJobId from JobIDs) 
    -- DON'T YOU NEED A "and o.SubscriberKey = s.Email" HERE? 
    and o.SubscriberKey = s.Email 

    left join C1111111._click cl on cl.JobID in (select fkJobId from JobIDs) 
    -- LOOK ABOVE, was "o.JobID", **REALLY** 
    and cl.SubscriberKey = s.Email 

    cross apply (
     select 
      '-1' as [OfferCode], 
      case 
       when o.EventDate is not null then 'Message Open' 
       else s.[ResponseType] 
      end as [ResponseType], 
      o.EventDate   
     where o.EventDate is not null or (o.EventDate is null and (s.status in ('unsubscribed','bounced','held'))) and o.isunique = 1 
    UNION ALL 
     select 
      isnull(cl.URL,'-1') as [OfferCode], 
      case 
       when c.EventDate is not null then 'Click' 
       else s.[ResponseType] 
      end as [ResponseType], 
      cl.EventDate 
     where cl.EventDate is not null or (cl.EventDate is null and (s.status in ('unsubscribed','bounced','held'))) and cl.isunique = 1 
    union all 
     select 
      '-1' as [OfferCode], 
      NULL AS [ResponseType], 
      null as EventDate 
     where cl.EventDate is null and o.EventDate is null and s.status not in ('unsubscribed','bounced','held') 
    ) AS X 
相關問題