数据库环境:SQL SERVER 2005

今天看到一条SQL,返回10条数据,执行了50多S。刚好有空,就对它进行了优化,优化后1S出结果。

先看下原始SQL

SELECT  t1.line_no ,
        MAX(sat100.confrim_date) confrim_date ,
        sam63_lq.company_name ,
        sat04.c_code ,
        ctlm23.corr_name ,
        MAX(sat04.l_date_d) l_date_d ,
        SUM(sat05.qty_d_order) qty_d_order ,
        sat100.tran_no_row
FROM    sat100
        INNER JOIN sat101 ON sat100.com_id = sat101.com_id
                             AND sat100.tran_no = sat101.tran_no
                             AND sat100.tran_row = sat101.tran_row
        LEFT JOIN sat05 ON sat101.com_id = sat05.com_id
                           AND sat101.p_g_order_no = sat05.p_g_order_no
                           AND sat101.p_g_order_line = sat05.p_g_order_line
        LEFT JOIN sat04 ON sat04.com_id = sat05.com_id
                           AND sat04.p_g_order_no = sat05.p_g_order_no
        LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
                            AND sat05.item_no = ctlm22.item_no
        LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
                              AND sat100.company_id = sam63_lq.company_id
        LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
                              AND sat05.cx_item_no = sam60_lq.cx_item_no
        LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no
                              AND sat100.company_id = sam65_lq.company_id
        LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
                            AND sat05.sa_unit = ctlm17.unit
        LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
                            AND sat04.c_code = salm02.client_id
        LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
                            AND sat04.c_code = ctlm23.corr_id
        LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
        LEFT JOIN ( SELECT  ROW_NUMBER() OVER ( ORDER BY t2.tran_no_row ) line_no ,
                            t2.tran_no_row
                    FROM    ( SELECT  DISTINCT
                                        sat100.tran_no_row
                              FROM      sat100
                                        INNER JOIN sat101 ON sat100.com_id = sat101.com_id
                                                             AND sat100.tran_no = sat101.tran_no
                                                             AND sat100.tran_row = sat101.tran_row
                                        LEFT JOIN sat05 ON sat101.com_id = sat05.com_id
                                                           AND sat101.p_g_order_no = sat05.p_g_order_no
                                                           AND sat101.p_g_order_line = sat05.p_g_order_line
                                        LEFT JOIN sat04 ON sat04.com_id = sat05.com_id
                                                           AND sat04.p_g_order_no = sat05.p_g_order_no
                                        LEFT JOIN ctlm22 ON sat05.com_id = ctlm22.com_id
                                                            AND sat05.item_no = ctlm22.item_no
                                        LEFT JOIN sam63_lq ON sat100.com_id = sam63_lq.com_id
                                                              AND sat100.company_id = sam63_lq.company_id
                                        LEFT JOIN sam60_lq ON sat05.com_id = sam60_lq.com_id
                                                              AND sat05.cx_item_no = sam60_lq.cx_item_no
                                        LEFT JOIN ctlm17 ON sat05.com_id = ctlm17.com_id
                                                            AND sat05.sa_unit = ctlm17.unit
                                        LEFT JOIN salm02 ON sat04.com_id = salm02.com_id
                                                            AND sat04.c_code = salm02.client_id
                                        LEFT JOIN ctlm23 ON sat04.com_id = ctlm23.com_id
                                                            AND sat04.c_code = ctlm23.corr_id
                                        LEFT JOIN ctlm08 ON salm02.dept_id = ctlm08.dept_id
                              WHERE     salm02.dept_id LIKE '%'
                                        AND sat100.company_id = '107'
                                        AND sat100.corr_id LIKE 'A010131%'
                                        AND sat04.l_date_d >= '2015/06/01 00:00:00'
                                        AND sat04.l_date_d <= '2015/06/30 23:59:59'
                            ) t2
                  ) t1 ON sat100.tran_no_row = t1.tran_no_row
WHERE   salm02.dept_id LIKE '%'
        AND sat100.company_id = '107'
        AND sat100.corr_id LIKE 'A010131%'
        AND sat04.l_date_d >= '2015/06/01 00:00:00'
        AND sat04.l_date_d <= '2015/06/30 23:59:59'
GROUP BY t1.line_no ,
        sam63_lq.company_name ,
        sat04.c_code ,
        ctlm23.corr_name ,
        sat100.tran_no_row
ORDER BY t1.line_no ,
        sat100.tran_no_row
View Code

相关文章:

  • 2022-12-23
  • 2022-01-07
  • 2022-02-06
  • 2021-07-26
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-11-07
猜你喜欢
  • 2021-06-17
  • 2022-02-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
  • 2021-05-20
相关资源
相似解决方案