dense_rank()+hash提示改写优化SQL

9/1/2015来源:SQL技巧人气:982

dense_rank()+hash提示改写优化SQL

数据库环境: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_rowFROM    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_rowWHERE   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_roWordER BY t1.line_no ,        sat100.tran_no_row
View Code

下面,我来说下我的优化思路:

   1.检查SQL的写法是否有问题

   先看下子查询部分,发现和外部访问的表及过滤的条件都差不多,用BeyondCompare工具检查外部查询和子查询的差别,相对于外部查询,

子查询少访问了一个表sam65_lq,即少了这部分内容“LEFT JOIN sam65_lq ON sat100.car_no = sam65_lq.veh_no AND sat100.company_id

= sam65_lq.company_id”,恰巧字段veh_no和字段company_id是sam65_lq的联合主键,因此,这部分并没有影响查询的数据。

   再看下子查询,它要实现的功能就是根据不重复的tran_no_row生成一个序号,因此,可以用dense_rank()替代子查询实现相同的功能。同时,

sat04有过滤条件,因而可以将left join sat04改成inner join sat04。

   改写后的SQL如下:

SELECT  line_no ,        MAX(confrim_date) confrim_date ,        company_name ,        c_code ,        corr_name ,        MAX(l_date_d) l_date_d ,        SUM(qty_d_order) qty_d_order ,        tran_no_rowFROM    ( SELECT    DENSE_RANK() OVER ( ORDER BY sat100.tran_no_row ) AS line_no ,                    sat100.confrim_date ,                    sam63_lq.company_name ,                    sat04.c_code ,                    ctlm23.corr_name ,                    sat04.l_date_d ,                    sat05.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                    INNER 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 sat0