0

このクエリには平均 4 秒かかります。これはストアド プロシージャのサブクエリになり、1 秒未満で実行する必要があります。クエリは次のとおりです。

(select customercampaignname + ' $' + convert(varchar, cast(amount as numeric(36,2) ) ) As 'Check_Stub_Comment2' from (
    select ROW_NUMBER() OVER (ORDER BY amount desc) as rownumber, customercampaignname, amount from (
        select * from (
               select distinct d.customercampaignname
                   ,sum(d.mastercurrencyamount) As amount

                from bb02_donation d
                      JOIN bb02_donationline dl on d.donationid = dl.donationid
                      JOIN bb02_fundraiserrevenuestream frs on dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid and frs.fundraiserid = 1869

                where d.customercampaignname is not null 
                      and d.customercampaignname != '' 
                group by d.CustomerCampaignName
        ) as x
) as sub ) as y where rownumber = 1)
4

3 に答える 3

0

これが違いを生むかどうかはわかりません

;WITH cte AS 
(
    select *, ROW_NUMBER() OVER (ORDER BY amount desc) as rownumber
    from (
        select distinct d.customercampaignname, sum(d.mastercurrencyamount) As amount
        from bb02_donation d
        INNER JOIN bb02_donationline dl on d.donationid = dl.donationid
        INNER JOIN bb02_fundraiserrevenuestream frs on dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid and frs.fundraiserid = 1869
        where d.customercampaignname is not null 
                and d.customercampaignname != '' 
        group by d.CustomerCampaignName
    ) as x
)
SELECT TOP 1 *
FROM cte 
ORDER BY RowNumber

たとえば、bb02_fundraiserrevenuestream.fundraiserid = 1869 のみを選択して、残りのクエリを実行できます。わかりませんが、実行計画を試してみることになるかもしれません。

;WITH Frs AS 
( SELECT dl.fundraiserrevenuestreamid 
  FROM bb02_fundraiserrevenuestream
  WHERE fundraiserid = 1869
)
, cte AS (
    select d.customercampaignname, sum(d.mastercurrencyamount) As amount
    from Frs
    INNER JOIN bb02_donationline dl  ON dl.fundraiserrevenuestreamid = frs.fundraiserrevenuestreamid
    INNER JOIN bb02_donation d on d.donationid = dl.donationid
    where d.customercampaignname is not null 
            and d.customercampaignname != '' 
    group by d.CustomerCampaignName
)
SELECT TOP 1  customercampaignname + ' $' + convert(varchar, cast(amount as numeric(36,2) ) ) As 'Check_Stub_Comment2' 
FROM cte
ORDER BY sum(d.mastercurrencyamount)

いつものINDEXESように、これらのパフォーマンスの問題の解決策になる可能性があります。

于 2013-07-22T18:58:57.987 に答える