0

エンド ユーザーが 1 つまたは複数を選択してレポートを実行できるアプリケーションがあります。

  • お客様
  • 期間
  • 指標

アプリケーション/レポート プラットフォームの制約により、ユーザーが選択した組み合わせごとに 1 つの行を含むクエリを返す必要があります。その組み合わせのデータがない場合でも同様です。

クエリを最適化する方法があるかどうかを確認したいのですが、構築しているマトリックスを実際の財務データに結合して、データセットを構築するのにかなりの時間がかかることがわかったからです戻ってきた。

以下のようにサンプルをまとめました(SQL Server 2008 R2):

--Average 12 metrics, max 50
declare @Metrics table (MetricID int)
insert into @Metrics values (1000)
insert into @Metrics values (2000)
insert into @Metrics values (3000)

--Average 12 periods, max 24
declare @Periods table (PeriodID int)
insert into @Periods values (201301)
insert into @Periods values (201302)
insert into @Periods values (201303)

--Between 200 and 500 customers
declare @Customers table (CustomerID int)
insert into @Customers values (1)
insert into @Customers values (2)
insert into @Customers values (3)

--Create combined table
declare @IDMatrix table (CustomerID int, PeriodID int, MetricID int)

INSERT INTO @IDMatrix
SELECT x1.CustomerID, 
        x1.PeriodID, 
        m.MetricID
FROM (SELECT c.CustomerID, p.PeriodID
        FROM @Customers c, @Periods p) x1, @Metrics m
ORDER BY x1.customerid, x1.periodid, m.MetricID


declare @Financials table (CustomerID int, MetricID int, PeriodID int, Value float)
insert into @Financials values (1,1000,201301,984.2)
insert into @Financials values (1,1000,201302,245.1)
insert into @Financials values (1,1000,201303,789.1)

SELECT
    i.CustomerID,
    i.PeriodID,
    i.MetricID,
    sum(isnull(f.Value,0))
FROM @IDMatrix i
LEFT JOIN @Financials f ON i.CustomerID = f.CustomerID and i.MetricID = f.MetricID and i.PeriodID = f.PeriodID
GROUP BY
    i.CustomerID,
    i.PeriodID,
    i.MetricID
4

1 に答える 1

1

マトリックスのテーブル変数の代わりに一時テーブルを試してから、インデックスを作成することをお勧めします。

于 2013-04-08T18:18:17.490 に答える