MSSQL:以下代码的替代方法,这需要太多时间才能获得结果

MSSQL: Alternative Approach to the below code, this is taking too much time to get results

我想知道如何简单地编写下面的代码,同时我也有兴趣避免代码重复。有没有别的办法。要做的是

  1. 无论 [invoices_global] table 的字段发生什么变化,这些变化都会以 old_value、[=37 的格式作为新记录插入=]、field_changed 在更改日志中 table。

  2. 单个字段可能会被许多用户更新,所以应该是最新的,这就是为什么我在代码中提到 last_updated_datetime desc。

  3. 需要避免重复。

  4. 性能提升,因为我为单个 ap_work_id 多次调用相同的 table。获取结果需要 18 秒 - 40000 个中有 2000 个。

  5. 仅供参考,system_id 和 ap_work_id 相同。

         SELECT
             api.ap_work_id,
             api.invoice_status,
             api.po_number,
             api.po_value,
             api.region,
             api.invoice_value,
             api.invoice_value_currency,
             em3.employee_full_name AS invoice_owner,
             (
                 **SELECT TOP 1
                     old_value
                 FROM    change_log cl
                 WHERE  field_changed = 'ARIBA INVOICE ID' AND api.ap_work_id = cl.
                 system_id AND system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_ariba_id_value,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'ARIBA INVOICE ID' AND api.ap_work_id = cl.
                 system_id AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_ariba_id_value,
             (
                 **SELECT TOP 1
                     old_value
                 FROM   change_log cl
                 WHERE  field_changed = 'PO NUMBER' AND api.ap_work_id = cl.system_id AND
                 cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_po_number,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'PO NUMBER' AND api.ap_work_id = cl.system_id AND
                 cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_po_number,
             (
                 **SELECT TOP 1
                     old_value
                 FROM   change_log cl
                 WHERE  field_changed = 'VENDOR NAME' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_vendor_name,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'VENDOR NAME' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_vendor_name,
             (
                 **SELECT TOP 1
                     old_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE VALUE' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_invoice_value,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE VALUE' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_invoice_value,
             (
                 **SELECT TOP 1
                     old_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE CURRENCY' AND api.ap_work_id = cl.
                 system_id AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_invoice_currency,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE CURRENCY' AND api.ap_work_id = cl.
                 system_id AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_invoice_currency,
             (
                 **SELECT TOP 1
                     old_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE DATE' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS bot_invoice_date,
             (
                 **SELECT TOP 1
                     new_value
                 FROM   change_log cl
                 WHERE  field_changed = 'INVOICE DATE' AND api.ap_work_id = cl.system_id
                 AND cl.system_type = 'INVOICE'
                 ORDER BY
                         last_updated_datetime DESC**
             ) AS processor_invoice_date
     FROM invoices_global api
         OUTER APPLY
     (
         SELECT TOP 1
             *
         FROM   transactions t
         WHERE  t.po_number = api.po_number AND t.po_number <> ''
         ORDER BY
                 t.last_updated_datetime DESC
     ) AS t
         LEFT JOIN employee_master em3
         ON em3.ldap = api.invoice_owner_ldap
         LEFT JOIN vendor v
         ON v.vendor_number = api.vendor_number
         LEFT JOIN cost_center cc
         ON cc.cost_ctr = api.cost_center
     WHERE DATEDIFF(DAY,api.approved_date,GETDATE()) <= 30
     GROUP BY
             api.ap_work_id,
             api.category,
             api.invoice_status,
             api.po_number,
             api.po_value,
             api.region,
             em3.employee_full_name,
             api.invoice_value,
             api.invoice_value_currency,
             api.invoice_date,
             api.due_date,
             api.company_code,
             api.vendor_number,
             api.approved_date,
             t.company_code,
             api.cost_center,
             v.vendor_name,
             v.vendor_number,
             cc.company;
    

这是一种方法,您

  • 为 system_id 和 field_changed
  • 的每个组合创建一个具有最新值的派生 table
  • 将派生的 tabled 加入原始 api table 并将值放入各个字段

它目前使用 MAX(CASE()),但您可以将其用作 PIVOT。

此处仅包含 SQL 的顶部(与问题相关)- 您需要在底部进行其他连接。我也没有测试过它(没有 table 结构等)所以我不能保证没有错别字。

; WITH LatestCL AS
        (SELECT * 
            FROM    (SELECT system_id AS ap_work_id, field_changed, old_value, new_value, 
                        ROW_NUMBER () OVER (PARTITION BY system_id, field_changed ORDER BY last_updated_datetime DESC) AS rn
                    FROM change_log
                    WHERE system_type = 'INVOICE'
                    ) cl
            WHERE cl.rn = 1
        )
SELECT  api.ap_work_id,
        api.invoice_status,
        api.po_number,
        api.po_value,
        api.region,
        api.invoice_value,
        api.invoice_value_currency,
        MAX(CASE WHEN field_changed = 'ARIBA INVOICE ID' THEN old_value ELSE NULL END) AS bot_ariba_id_value,
        MAX(CASE WHEN field_changed = 'ARIBA INVOICE ID' THEN new_value ELSE NULL END) AS processor_ariba_id_value,
        MAX(CASE WHEN field_changed = 'PO NUMBER' THEN old_value ELSE NULL END) AS bot_po_number,
        MAX(CASE WHEN field_changed = 'PO NUMBER' THEN new_value ELSE NULL END) AS processor_po_number,
        MAX(CASE WHEN field_changed = 'VENDOR NAME' THEN old_value ELSE NULL END) AS bot_vendor_name,
        MAX(CASE WHEN field_changed = 'VENDOR NAME' THEN new_value ELSE NULL END) AS processor_vendor_name,
        MAX(CASE WHEN field_changed = 'INVOICE VALUE' THEN old_value ELSE NULL END) AS bot_invoice_value,
        MAX(CASE WHEN field_changed = 'INVOICE VALUE' THEN new_value ELSE NULL END) AS processor_invoice_value,
        MAX(CASE WHEN field_changed = 'INVOICE CURRENCY' THEN old_value ELSE NULL END) AS bot_invoice_currency,
        MAX(CASE WHEN field_changed = 'INVOICE CURRENCY' THEN new_value ELSE NULL END) AS processor_invoice_currency,
        MAX(CASE WHEN field_changed = 'INVOICE DATE' THEN old_value ELSE NULL END) AS bot_invoice_date,
        MAX(CASE WHEN field_changed = 'INVOICE DATE' THEN new_value ELSE NULL END) AS processor_invoice_date
FROM    invoices_global api
        INNER JOIN LatestCL ON api.ap_work_id = LatestCL.ap_work_id