2010-11-30 92 views
3

我正在尋找優化一個需要過多時間運行的查詢(大約1.5小時),並且通過查看執行計劃,最大的排水渠之一似乎是從中插入數據主要查詢到一個臨時表中。當我將數據插入到一個臨時表有以下過程:插入到臨時表查詢

過程(成本%)

Compute Scalar (0%) <-- Stream Aggregate (0%) <-- Sort(0%) <-- Table Spool (eager spool) (10%) 

這是有道理的,但到底是什麼都不給我的是,這似乎9倍?

我可以包含查詢/索引信息,如果這有幫助,但查詢本身很長,我不想將這個問題寫入一篇文章!

謝謝, 達芬。

編輯包含查詢 - 抱歉的長度!

IF object_id('tempdb..#category') IS NOT NULL DROP TABLE #category 
CREATE TABLE #category 
(
contact_id NVARCHAR(255) 
,donor_class NVARCHAR(255) 
,payment_type NVARCHAR(255) 
,donation_type NVARCHAR(255) 
,approach_channel NVARCHAR(255) 
,count1987 INT 
,sum1987 DECIMAL(18,3) 
,count1988 INT 
,sum1988 DECIMAL(18,3) 
,count1989 INT 
,sum1989 DECIMAL(18,3) 
,count1990 INT 
,sum1990 DECIMAL(18,3) 
,count1991 INT 
,sum1991 DECIMAL(18,3) 
,count1992 INT 
,sum1992 DECIMAL(18,3) 
,count1993 INT 
,sum1993 DECIMAL(18,3) 
,count1994 INT 
,sum1994 DECIMAL(18,3) 
,count1995 INT 
,sum1995 DECIMAL(18,3) 
,count1996 INT 
,sum1996 DECIMAL(18,3) 
,count1997 INT 
,sum1997 DECIMAL(18,3) 
,count1998 INT 
,sum1998 DECIMAL(18,3) 
,count1999 INT 
,sum1999 DECIMAL(18,3) 
,count2000 INT 
,sum2000 DECIMAL(18,3) 
,count2001 INT 
,sum2001 DECIMAL(18,3) 
,count2002 INT 
,sum2002 DECIMAL(18,3) 
,count2003 INT 
,sum2003 DECIMAL(18,3) 
,count2004 INT 
,sum2004 DECIMAL(18,3) 
,count2005 INT 
,sum2005 DECIMAL(18,3) 
,count2006 INT 
,sum2006 DECIMAL(18,3) 
,count2007 INT 
,sum2007 DECIMAL(18,3) 
,count2008 INT 
,sum2008 DECIMAL(18,3) 
,count2009 INT 
,sum2009 DECIMAL(18,3) 
,count2010 INT 
,sum2010 DECIMAL(18,3) 
,category05 NVARCHAR(255) 
,category06 NVARCHAR(255) 
,category07 NVARCHAR(255) 
,category08 NVARCHAR(255) 
,category09 NVARCHAR(255) 
,category10 NVARCHAR(255) 
,[1987] NVARCHAR(4) 
,[1988] NVARCHAR(4) 
,[1989] NVARCHAR(4) 
,[1990] NVARCHAR(4) 
,[1991] NVARCHAR(4) 
,[1992] NVARCHAR(4) 
,[1993] NVARCHAR(4) 
,[1994] NVARCHAR(4) 
,[1995] NVARCHAR(4) 
,[1996] NVARCHAR(4) 
,[1997] NVARCHAR(4) 
,[1998] NVARCHAR(4) 
,[1999] NVARCHAR(4) 
,[2000] NVARCHAR(4) 
,[2001] NVARCHAR(4) 
,[2002] NVARCHAR(4) 
,[2003] NVARCHAR(4) 
,[2004] NVARCHAR(4) 
,[2005] NVARCHAR(4) 
,[2006] NVARCHAR(4) 
,[2007] NVARCHAR(4) 
,[2008] NVARCHAR(4) 
,[2009] NVARCHAR(4) 
,[2010] NVARCHAR(4) 
) 
CREATE INDEX #idx_category ON #category (contact_id) 

;WITH complete_finances as 
(
(
SELECT 
don.supporter_id AS contact_id 
,don.id AS gift_id 
,YEAR(don.date_received) AS calendar_year 
,YEAR(don.date_received) - CASE WHEN MONTH(don.date_received) < 4 THEN 1 ELSE 0 END AS financial_year 
,don.date_received AS date_received 
,don.event_id AS event_id 
,SUM(CASE WHEN don.gift_aid_status <> 4 THEN don.value_gross * ((dfa.percentage)/100) 
      WHEN don.gift_aid_status = 4 AND don.value_net > don.value_gross 
      AND don.value_net <> 0  THEN don.value_net * ((dfa.percentage)/100) 
      ELSE don.value_gross * ((dfa.percentage)/100) 
      END 
     ) AS donation_value 
,(CASE WHEN don.payment_method IN (1, 2, 10, 11, 12, 200000, 200001, 200002, 200003, 200004, 200007, 200008, 200009 --single payments 
              ,7,13,14) -- single payment refunds 
        THEN 'single' 
     WHEN don.payment_method IN (3,4,5,9,200006) 
        THEN 'regular' 
     ELSE 'other' END) AS type1 
,CASE WHEN dfa.fund_id IN --all emergency funds 
    (
    select f.id 
    FROM _audit a 
    INNER JOIN _fund f ON a.article_id = f.id 
    WHERE a.entity_name = 'Fund' 
    AND a.Changes LIKE 
    '%finance_code2%OldValue>3%' 
    UNION 
    select 
    id 
    FROM _fund 
    WHERE (finance_code2 LIKE '3%' 
    OR finance_code2 LIKE '9%') 
    AND finance_code2 IS NOT NULL 
    ) THEN 'Emergency' else 'Non-emergency' end as type2 
FROM donation don WITH (nolock) 
INNER JOIN donation_fund_allocation dfa WITH (nolock) ON dfa.donation_id = don.id 
WHERE don.supporter_id IS NOT NULL 
AND don.status = 4 -- posted donations only 
AND don.value_gross <> 0 -- to include refunds as well 
GROUP BY don.supporter_id 
,don.id 
,don.date_received 
,don.event_id 
,don.payment_method 
,dfa.fund_id 
) 
UNION ALL 
(
SELECT 
caw.contact_id AS contact_id 
,caw.id AS gift_id 
,YEAR(caw.bank_date) AS calendar_year 
,YEAR(caw.bank_date) - CASE WHEN MONTH(caw.bank_date) < 4 THEN 1 ELSE 0 END AS financial_year 
,caw.bank_date AS date_received 
,caw.event AS event_id 
,(CASE  WHEN caw.gift_aid_status <> 4 THEN caw.amount_exc_gift_aid 
      WHEN caw.gift_aid_status = 4 THEN amount_inc_gift_aid 
      END 
     ) AS donation_value 
,'CAW gift' AS type1 
,'Non-emergency' AS type2 
FROM gifts caw WITH (nolock) 
WHERE caw.contact_id IS NOT NULL 
AND caw.amount_exc_gift_aid <> 0 
) 
) 
,category AS 
(
SELECT 
cmf.contact_id AS contact_id 
,lfu.description AS donor_class 
,ISNULL(cmf.type1,'Total') AS payment_type 
,ISNULL(lac.description, 'Total') AS approach_channel 
--,ISNULL(CASE WHEN type1 = 'CAW gift' THEN 'CAW Gift' 
--  WHEN type1 = 'Single' THEN 'Non CAW gift' 
--  WHEN type1 = 'Regular' THEN 'Non CAW gift' 
--  WHEN type1 = 'Other' THEN 'Non CAW gift' 
--  END,'Total') AS payment_type2 
,ISNULL(cmf.type2,'Total') AS donation_type 
,SUM(CASE WHEN cmf.financial_year = '1987' THEN 1 ELSE 0 END)     AS count1987 
,SUM(CASE WHEN cmf.financial_year = '1988' THEN cmf.donation_value ELSE 0 END) AS sum1987 
,SUM(CASE WHEN cmf.financial_year = '1988' THEN 1 ELSE 0 END)    AS count1988 
,SUM(CASE WHEN cmf.financial_year = '1988' THEN cmf.donation_value ELSE 0 END) AS sum1988 
,SUM(CASE WHEN cmf.financial_year = '1989' THEN 1 ELSE 0 END)    AS count1989 
,SUM(CASE WHEN cmf.financial_year = '1989' THEN cmf.donation_value ELSE 0 END) AS sum1989 
,SUM(CASE WHEN cmf.financial_year = '1990' THEN 1 ELSE 0 END)    AS count1990 
,SUM(CASE WHEN cmf.financial_year = '1990' THEN cmf.donation_value ELSE 0 END) AS sum1990 
,SUM(CASE WHEN cmf.financial_year = '1991' THEN 1 ELSE 0 END)    AS count1991 
,SUM(CASE WHEN cmf.financial_year = '1991' THEN cmf.donation_value ELSE 0 END) AS sum1991 
,SUM(CASE WHEN cmf.financial_year = '1992' THEN 1 ELSE 0 END)    AS count1992 
,SUM(CASE WHEN cmf.financial_year = '1992' THEN cmf.donation_value ELSE 0 END) AS sum1992 
,SUM(CASE WHEN cmf.financial_year = '1993' THEN 1 ELSE 0 END)    AS count1993 
,SUM(CASE WHEN cmf.financial_year = '1993' THEN cmf.donation_value ELSE 0 END) AS sum1993 
,SUM(CASE WHEN cmf.financial_year = '1994' THEN 1 ELSE 0 END)    AS count1994 
,SUM(CASE WHEN cmf.financial_year = '1994' THEN cmf.donation_value ELSE 0 END) AS sum1994 
,SUM(CASE WHEN cmf.financial_year = '1995' THEN 1 ELSE 0 END)    AS count1995 
,SUM(CASE WHEN cmf.financial_year = '1995' THEN cmf.donation_value ELSE 0 END) AS sum1995 
,SUM(CASE WHEN cmf.financial_year = '1996' THEN 1 ELSE 0 END)    AS count1996 
,SUM(CASE WHEN cmf.financial_year = '1996' THEN cmf.donation_value ELSE 0 END) AS sum1996 
,SUM(CASE WHEN cmf.financial_year = '1997' THEN 1 ELSE 0 END)    AS count1997 
,SUM(CASE WHEN cmf.financial_year = '1997' THEN cmf.donation_value ELSE 0 END) AS sum1997 
,SUM(CASE WHEN cmf.financial_year = '1998' THEN 1 ELSE 0 END)    AS count1998 
,SUM(CASE WHEN cmf.financial_year = '1998' THEN cmf.donation_value ELSE 0 END) AS sum1998 
,SUM(CASE WHEN cmf.financial_year = '1999' THEN 1 ELSE 0 END)    AS count1999 
,SUM(CASE WHEN cmf.financial_year = '1999' THEN cmf.donation_value ELSE 0 END) AS sum1999 
,SUM(CASE WHEN cmf.financial_year = '2000' THEN 1 ELSE 0 END)    AS count2000 
,SUM(CASE WHEN cmf.financial_year = '2000' THEN cmf.donation_value ELSE 0 END) AS sum2000 
,SUM(CASE WHEN cmf.financial_year = '2001' THEN 1 ELSE 0 END)    AS count2001 
,SUM(CASE WHEN cmf.financial_year = '2001' THEN cmf.donation_value ELSE 0 END) AS sum2001 
,SUM(CASE WHEN cmf.financial_year = '2002' THEN 1 ELSE 0 END)    AS count2002 
,SUM(CASE WHEN cmf.financial_year = '2002' THEN cmf.donation_value ELSE 0 END) AS sum2002 
,SUM(CASE WHEN cmf.financial_year = '2003' THEN 1 ELSE 0 END)    AS count2003 
,SUM(CASE WHEN cmf.financial_year = '2003' THEN cmf.donation_value ELSE 0 END) AS sum2003 
,SUM(CASE WHEN cmf.financial_year = '2004' THEN 1 ELSE 0 END)    AS count2004 
,SUM(CASE WHEN cmf.financial_year = '2004' THEN cmf.donation_value ELSE 0 END) AS sum2004 
,SUM(CASE WHEN cmf.financial_year = '2005' THEN 1 ELSE 0 END)    AS count2005 
,SUM(CASE WHEN cmf.financial_year = '2005' THEN cmf.donation_value ELSE 0 END) AS sum2005 
,SUM(CASE WHEN cmf.financial_year = '2006' THEN 1 ELSE 0 END)    AS count2006 
,SUM(CASE WHEN cmf.financial_year = '2006' THEN cmf.donation_value ELSE 0 END) AS sum2006 
,SUM(CASE WHEN cmf.financial_year = '2007' THEN 1 ELSE 0 END)    AS count2007 
,SUM(CASE WHEN cmf.financial_year = '2007' THEN cmf.donation_value ELSE 0 END) AS sum2007 
,SUM(CASE WHEN cmf.financial_year = '2008' THEN 1 ELSE 0 END)    AS count2008 
,SUM(CASE WHEN cmf.financial_year = '2008' THEN cmf.donation_value ELSE 0 END) AS sum2008 
,SUM(CASE WHEN cmf.financial_year = '2009' THEN 1 ELSE 0 END)    AS count2009 
,SUM(CASE WHEN cmf.financial_year = '2009' THEN cmf.donation_value ELSE 0 END) AS sum2009 
,SUM(CASE WHEN cmf.financial_year = '2010' THEN 1 ELSE 0 END)    AS count2010 
,SUM(CASE WHEN cmf.financial_year = '2010' THEN cmf.donation_value ELSE 0 END) AS sum2010 
FROM complete_finances cmf 
INNER JOIN contact con WITH (nolock) ON con.id = cmf.contact_id 
INNER JOIN [l_function] lfu WITH (nolock) ON lfu.code = con.class 
LEFT OUTER JOIN event eve with (nolock) on eve.id = cmf.event_id 
INNER JOIN [l_approach channel] lac WITH (nolock) ON lac.code = eve.class 
WHERE cmf.contact_id IS NOT NULL 
GROUP BY 
cmf.contact_id 
,cmf.type1 
,cmf.type2 
,lfu.description 
,lac.description 
WITH CUBE 
) 
INSERT INTO #category 
SELECT 
contact_id 
,donor_class 
,payment_type 
,donation_type 
,approach_channel 
,count1987 
,sum1987 
,count1988 
,sum1988 
,count1989 
,sum1989 
,count1990 
,sum1990 
,count1991 
,sum1991 
,count1992 
,sum1992 
,count1993 
,sum1993 
,count1994 
,sum1994 
,count1995 
,sum1995 
,count1996 
,sum1996 
,count1997 
,sum1997 
,count1998 
,sum1998 
,count1999 
,sum1999 
,count2000 
,sum2000 
,count2001 
,sum2001 
,count2002 
,sum2002 
,count2003 
,sum2003 
,count2004 
,sum2004 
,count2005 
,sum2005 
,count2006 
,sum2006 
,count2007 
,sum2007 
,count2008 
,sum2008 
,count2009 
,sum2009 
,count2010 
,sum2010 
,CASE WHEN count2005 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004 = 0 
      THEN 'New donor' 
     WHEN count2005 > 0 AND count2004 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003 > 0 
      THEN 'Reactivated donor' 
     WHEN count2005 > 0 AND count2004 > 0 AND count2003 > 0 
      AND count2002 > 0 AND count2001 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2005 > 0 AND count2004 > 0 AND count2003 > 0 
      AND count2002 > 0 
      THEN '4yrs consecutive' 
     WHEN count2005 > 0 AND count2004 > 0 AND count2003 > 0 
      THEN '3yrs consecutive' 
     WHEN count2005 > 0 AND count2004 > 0 
      THEN '2yrs consecutive' 
     WHEN count2005 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005 = 0 
      THEN 'Non donor' 
END AS category05 
,CASE WHEN count2006 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005 = 0 
      THEN 'New donor' 
     WHEN count2006 > 0 AND count2005 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004 > 0 
      THEN 'Reactivated donor' 
     WHEN count2006 > 0 AND count2005 > 0 AND count2004 > 0 
      AND count2003 > 0 AND count2002 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2006 > 0 AND count2005 > 0 AND count2004 > 0 
      AND count2003 > 0 
      THEN '4yrs consecutive' 
     WHEN count2006 > 0 AND count2005 > 0 AND count2004 > 0 
      THEN '3yrs consecutive' 
     WHEN count2006 > 0 AND count2005 > 0 
      THEN '2yrs consecutive' 
     WHEN count2006 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006 = 0 
      THEN 'Non donor' 
END AS category06 
,CASE WHEN count2007 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006 = 0 
      THEN 'New donor' 
     WHEN count2007 > 0 AND count2006 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005 > 0 
      THEN 'Reactivated donor' 
     WHEN count2007 > 0 AND count2006 > 0 AND count2005 > 0 
      AND count2004 > 0 AND count2003 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2007 > 0 AND count2006 > 0 AND count2005 > 0 
      AND count2004 > 0 
      THEN '4yrs consecutive' 
     WHEN count2007 > 0 AND count2006 > 0 AND count2005 > 0 
      THEN '3yrs consecutive' 
     WHEN count2007 > 0 AND count2006 > 0 
      THEN '2yrs consecutive' 
     WHEN count2007 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007 = 0 
      THEN 'Non donor' 
END AS category07 
,CASE WHEN count2008 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007 = 0 
      THEN 'New donor' 
     WHEN count2008 > 0 AND count2007 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006 > 0 
      THEN 'Reactivated donor' 
     WHEN count2008 > 0 AND count2007 > 0 AND count2006 > 0 
      AND count2005 > 0 AND count2004 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2008 > 0 AND count2007 > 0 AND count2006 > 0 
      AND count2005 > 0 
      THEN '4yrs consecutive' 
     WHEN count2008 > 0 AND count2007 > 0 AND count2006 > 0 
      THEN '3yrs consecutive' 
     WHEN count2008 > 0 AND count2007 > 0 
      THEN '2yrs consecutive' 
     WHEN count2008 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008 = 0 
      THEN 'Non donor' 
END AS category08 
,CASE WHEN count2009 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008 = 0 
      THEN 'New donor' 
     WHEN count2009 > 0 AND count2008 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007 > 0 
      THEN 'Reactivated donor' 
     WHEN count2009 > 0 AND count2008 > 0 AND count2007 > 0 
      AND count2006 > 0 AND count2005 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2009 > 0 AND count2008 > 0 AND count2007 > 0 
      AND count2006 > 0 
      THEN '4yrs consecutive' 
     WHEN count2009 > 0 AND count2008 > 0 AND count2007 > 0 
      THEN '3yrs consecutive' 
     WHEN count2009 > 0 AND count2008 > 0 
      THEN '2yrs consecutive' 
     WHEN count2009 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008+count2009 = 0 
      THEN 'Non donor' 
END AS category09 
,CASE WHEN count2010 > 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008+count2009 = 0 
      THEN 'New donor' 
     WHEN count2010 > 0 AND count2009 = 0 AND 
      count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008 > 0 
      THEN 'Reactivated donor' 
     WHEN count2010 > 0 AND count2009 > 0 AND count2008 > 0 
      AND count2007 > 0 AND count2006 > 0 
      THEN '5+yrs consecutive' 
     WHEN count2010 > 0 AND count2009 > 0 AND count2008 > 0 
      AND count2007 > 0 
      THEN '4yrs consecutive' 
     WHEN count2010 > 0 AND count2009 > 0 AND count2008 > 0 
      THEN '3yrs consecutive' 
     WHEN count2010 > 0 AND count2009 > 0 
      THEN '2yrs consecutive' 
     WHEN count2010 = 0 AND count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008+count2009 > 0 
      THEN 'Lapsed Donor' 
     WHEN count1987+count1988+count1989+count1990+count1991 
      +count1992+count1993+count1994+count1995+count1996 
      +count1997+count1998+count1999+count2000+count2001 
      +count2002+count2003+count2004+count2005+count2006+count2007+count2008+count2009+count2010 = 0 
      THEN 'Non donor' 
END AS category10 
,CASE WHEN count1987 > 0 THEN 1987 END AS [1987] 
,CASE WHEN count1988 > 0 THEN 1988 END AS [1988] 
,CASE WHEN count1989 > 0 THEN 1989 END AS [1989] 
,CASE WHEN count1990 > 0 THEN 1990 END AS [1990] 
,CASE WHEN count1991 > 0 THEN 1991 END AS [1991] 
,CASE WHEN count1992 > 0 THEN 1992 END AS [1992] 
,CASE WHEN count1993 > 0 THEN 1993 END AS [1993] 
,CASE WHEN count1994 > 0 THEN 1994 END AS [1994] 
,CASE WHEN count1995 > 0 THEN 1995 END AS [1995] 
,CASE WHEN count1996 > 0 THEN 1996 END AS [1996] 
,CASE WHEN count1997 > 0 THEN 1997 END AS [1997] 
,CASE WHEN count1998 > 0 THEN 1998 END AS [1998] 
,CASE WHEN count1999 > 0 THEN 1999 END AS [1999] 
,CASE WHEN count2000 > 0 THEN 2000 END AS [2000] 
,CASE WHEN count2001 > 0 THEN 2001 END AS [2001] 
,CASE WHEN count2002 > 0 THEN 2002 END AS [2002] 
,CASE WHEN count2003 > 0 THEN 2003 END AS [2003] 
,CASE WHEN count2004 > 0 THEN 2004 END AS [2004] 
,CASE WHEN count2005 > 0 THEN 2005 END AS [2005] 
,CASE WHEN count2006 > 0 THEN 2006 END AS [2006] 
,CASE WHEN count2007 > 0 THEN 2007 END AS [2007] 
,CASE WHEN count2008 > 0 THEN 2008 END AS [2008] 
,CASE WHEN count2009 > 0 THEN 2009 END AS [2009] 
,CASE WHEN count2010 > 0 THEN 2010 END AS [2010] 
FROM category 
WHERE 
contact_id IS NOT NULL 
AND donor_class IS NOT NULL 
ORDER BY contact_id 
+3

是的,請將完整查詢與執行計劃「text」和or XML一起發佈。 – 2010-11-30 11:02:47

+0

如果從查詢中刪除表格插入,會發生什麼情況 - 只需選擇數據?是否有任何錯誤的查詢計劃估計 - 即估計行數和實際行數差別很大的點? – 2010-11-30 11:05:12

+0

@刪除插入到#table子句時,查詢花費大約30分鐘的時間運行,並且在壞的查詢計劃方面有幾個(最大的一個是我的主表上的初始聚集索引掃描(捐贈) ,其中實際行數= 12,491,509,估計值爲193,940 – Dibstar 2010-11-30 11:16:05

回答

0

樣本數據沒有問題!

主要表信息:

Create table #donation 
(
contact_id NVARCHAR(255) 
,id UNIQUEIDENTIFIER 
,date_received DATETIME 
,event_id NVARCHAR(255) 
,value_gross DECIMAL(18,3) 
,value_net DECIMAL(18,3) 
,giftaid_status NVARCHAR(50) 
,payment_method NVARCHAR(50) -- 3 = regular, 1 = single 
) 

INSERT INTO #donation VALUES 
('18EC3CD2-3065-4FF4-BE40-000004228590', 'CEDB5C00-DB78-4D70-B222-892DCAD361E6', '2008-02-12 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', '45FB0841-BD82-47F2-B66F-473CE196B9E0', '2010-09-08 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', 'A27451B9-7542-4F61-A38F-EB202AD24CC5', '2010-03-09 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', '8F9E143C-4E35-4C9B-A385-A86F20D8EDA4', '2008-06-06 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', '47E91479-DAC6-4485-9C66-5970EA236CB0', '2009-07-10 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', '5A8DED80-C7A7-477D-9E3C-5C4E147B89D3', '2009-04-08 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', 'CD45A6B2-1204-495C-8C4F-27DFECF689CB', '2009-05-08 00:00:00.000', 'AA9237FC-E9E0-48D4-BF2C-62DDCDA7E874', 15.00, 15.00, 1, 3) 
('18EC3CD2-3065-4FF4-BE40-000004228590', 'E9C8C820-ECB7-4649-B27A-B84CD6029111', '2005-06-08 00:00:00.000', '34AE7B04-279F-476E-B040-78BA727E4370', 10.00, 12.82, 4, 3) 

CREATE TABLE #gifts 
(
contact_id NVARCHAR(255) 
,bank_date DATETIME 
,id UNIQUEIDENTIFIER 
,event_id NVARCHAR(255) 
,giftaid_status INT 
,amount_exc_giftaid DECIMAL(18,3) 
,amount_inc_giftaid DECIMAL(18,3) 
) 

INSERT INTO #gifts VALUES 
('18EC3CD2-3065-4FF4-BE40-000004228590','2006-05-31 00:00:00.000','A000BBCA-2C1D-4C53-8724-0E65498A7B21','97DAEE33-260A-46ED-8A14-201924BD057D',1,10.00,12.82) 
('18EC3CD2-3065-4FF4-BE40-000004228590','2002-05-31 00:00:00.000','F81A2690-C096-4BE9-8DD5-1670896323FF','F9F06373-38BC-4567-9F8A-EEB4434524BA',1,10.00,12.82) 

的附加表: - 數據不很重要的位置,但這些都鏈接到#donation表,以確定是否捐贈是緊急一個或沒有(在一切所述#gift表假設不被緊急)

CREATE TABLE #FUND 
(
donation_id NVARCHAR(255) 
,fund_id NVARCHAR(255) 
,finance_code NVARCHAR(10) 
,finance_code2 NVARCHAR(10) 
) 

CREATE TABLE #audit 
(
entity_name NVARCHAR(255) 
,changes NVARCHAR(5000) 
) 

CREATE TABLE #donation_fund (donation_id NVARCHAR(255) ,fund_id NVARCHAR(255) )

#fund表用於確定#donation表中的捐贈是否爲緊急情況 - 基金ID是否爲緊急情況由#audit表中的數據確定。

要確定#donation是緊急剪斷以下腳本用於:

CASE WHEN #donation_fund.fund_id IN --all emergency funds 
    (
    select f.id 
    FROM _audit a 
    INNER JOIN _fund f ON a.article_id = f.id 
    WHERE a.entity_name = 'Fund' 
    AND a.Changes LIKE 
    '%finance_code2%OldValue>3%' 
    UNION 
    select 
    id 
    FROM _fund 
    WHERE (finance_code2 LIKE '3%' 
    OR finance_code2 LIKE '9%') 
    AND finance_code2 IS NOT NULL 
    ) THEN 'Emergency' else 'Non-emergency' end as type2 

預期輸出(看起來像這樣,實際數字會隨着不同的結果下面設置包括額外的值):

contact_id donor_class payment_type donation_type count2002 sum2002 count2003 sum2003 count2004 sum2004 count2005 sum2005 count2006 sum2006 count2007 sum2007 count2008 sum2008 count2009 sum2009 count2010 sum2010 category05 category06 category07 category08 category09 category10 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted gift Non-emergency 1 10 0 0 0 0 0 0 1 10 0 0 0 0 0 0 0 0 Lapsed Donor Reactivated donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2002 NULL NULL NULL 2006 NULL NULL NULL NULL 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted regular Non-emergency 0 0 1 12.82 2 25.64 12 151.022 12 224.359 12 180 12 180 12 180 8 120 3yrs consecutive 4yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2003 2004 2005 2006 2007 2008 2009 2010 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted single Non-emergency 0 0 0 0 0 0 1 19.231 0 0 0 0 0 0 0 0 0 0 New donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2005 NULL NULL NULL NULL NULL 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted Total Non-emergency 1 10 1 12.82 2 25.64 13 170.253 13 234.359 12 180 12 180 12 180 8 120 4yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2002 2003 2004 2005 2006 2007 2008 2009 2010 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted gift Total 1 10 0 0 0 0 0 0 1 10 0 0 0 0 0 0 0 0 Lapsed Donor Reactivated donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2002 NULL NULL NULL 2006 NULL NULL NULL NULL 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted regular Total 0 0 1 12.82 2 25.64 12 151.022 12 224.359 12 180 12 180 12 180 8 120 3yrs consecutive 4yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2003 2004 2005 2006 2007 2008 2009 2010 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted single Total 0 0 0 0 0 0 1 19.231 0 0 0 0 0 0 0 0 0 0 New donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor Lapsed Donor NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2005 NULL NULL NULL NULL NULL 
18EC3CD2-3065-4FF4-BE40-000004228590 Unrestricted Total Total 1 10 1 12.82 2 25.64 13 170.253 13 234.359 12 180 12 180 12 180 8 120 4yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive 5+yrs consecutive NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL 2002 2003 2004 2005 2006 2007 2008 2009 2010 
1

我會調查使用交叉表查詢(PIVOT command in Sql Server)重寫您的查詢。這就像一個GROUP BY語句,不同之處在於它將聚合值放入列而不是行中,這就是你正在做的。這可能需要一點時間才能讓你的頭腦變得圓滿,但這些努力是值得的,你不會擁有所有的複製和粘貼代碼,並且它應該運行得更快。

2

同時有一個小建議,您可能會發現在將數據插入到臨時表中後,您最好在臨時表上創建索引,因爲這樣做會提高插入速度。手動創建它意味着必須修改您插入到臨時表中的每條記錄的索引。