相关疑难解决方法(0)

如何从我的查询中消除这种昂贵的索引查找操作?

我在 Sql Server 2008 中有两个表,看起来像这样:

Transaction_Details:
    Dataset,
    Department,
    Charge_ID,
    Transaction_Type,
    dos_month,
    post_month,
    amount


Charge_Summary
    Dataset,
    Department,
    Charge_ID,
    dos_month,
    Ins1_Category
    
Run Code Online (Sandbox Code Playgroud)

我正在寻找的输出看起来像这样:Dataset、Department、Ins1_Category、dos_month、Post_Month、Total_Payments、Total_Charges、Previous_Balance -- 定义为所有匹配交易的总金额,与当前月之前的 post_month

我在下面使用的 SQL 给出了预期的输出,但执行计划中有一个索引查找占用了 90% 以上的成本。这是在数据仓库操作中,因此除了此查询之外,服务器上没有其他活动,无需担心其他读取或写入。大约 30 分钟的执行时间对于它的用途来说是可以接受的,但如果有更好的方法来做到这一点,我仍然需要一些建议。

SELECT
    t1.Dataset,
    t1.Department,
    c1.Ins1_Category,
    t1.dos_month,
    t1.post_month,
    SUM(case
            when t1.transaction_type = 'payment' THEN t1.amount
            ELSE 0
        END
        ) AS total_payments,
    SUM(case
            when t1.transaction_type = 'adjustment' THEN t1.amount
            ELSE 0
        END
        ) AS total_adjustments,
    SUM(case
            when t1.transaction_type = 'charge' THEN t1.amount
            ELSE 0
        END
        ) AS total_charges,
    (
        SELECT
            SUM(t2.amount)
        FROM
            Transaction_Details …
Run Code Online (Sandbox Code Playgroud)

performance sql-server index-tuning query-performance

4
推荐指数
1
解决办法
423
查看次数