优化Oracle查询,使其只包含一条语句,并加快脚本的运行速度

c9qzyr3d  于 2023-03-22  发布在  Oracle
关注(0)|答案(1)|浏览(150)

我有这个续集,我列出了我所有的交易,我只对那些不是由接口预订的交易感兴趣,其中源接口= TBG。
我想添加另一个语句,再次我只查看未通过Interface预订的交易,但这次对另一个名为System Booking ID的字段感兴趣,该字段应始终包含条目。这两个字段应作为OR条件而不是AND条件进行测试。
我在下面列出了最初的声明,然后修改了它,创建了另一个满足“系统预订ID”为NOT NULL的条件的声明。
如果我一起运行它们,它们似乎可以工作。然而,脚本现在需要更长的时间才能完成。有没有办法将这些附加条件添加到第一条语句中,而不是创建一个全新的语句?我想这会使它运行得更快。
----初始语句

SELECT ab.deal_num
                FROM history_table abh
                INNER JOIN deal_table ab ON (
                        abh.tran_num = ab.tran_num
                        AND ab.tran_type = 0
                        )
                INNER JOIN ins i ON (
                        ab.ins_type = i.id_number
                        AND i.id_number IN (
                            605
                            ,610
                            )
                        )
                INNER JOIN personnel_table p ON (
                        abh.personnel_id = p.id_number
                        AND UPPER(p.name) NOT LIKE 'Interface%'
                        )
                INNER JOIN tran_info_types tit ON ('Source Interface' = tit.type_name)
                LEFT OUTER JOIN ab_tran_info ati ON (
                        tit.type_id = ati.type_id
                        AND ab.tran_num = ati.tran_num
                        )
                WHERE 1 = 1
                    AND abh.row_creation > TO_DATE('16-Mar-23 22:56:16', 'DD-MM-YY HH24:MI:SS') - 90
                    AND ati.value = 'TBG'
                    AND abh.version_number = 1

----附加声明

SELECT ab.deal_num
                    FROM history_table abh
                    INNER JOIN deal_table ab ON (
                            abh.tran_num = ab.tran_num
                            AND ab.tran_type = 0
                            )
                    INNER JOIN ins i ON (
                            ab.ins_type = i.id_number
                            AND i.id_number IN (
                                605
                                ,610
                                )
                            )
                    INNER JOIN personnel_table p ON (
                            abh.personnel_id = p.id_number
                            AND UPPER(p.name) NOT LIKE 'Interface%'
                            )
                    INNER JOIN tran_info_types tit ON ('System Booking ID' = tit.type_name)
                    LEFT OUTER JOIN ab_tran_info ati ON (
                            tit.type_id = ati.type_id
                            AND ab.tran_num = ati.tran_num
                            )
                    WHERE 1 = 1
                        AND abh.row_creation > TO_DATE('16-Mar-23 22:56:16', 'DD-MM-YY HH24:MI:SS') - 90
                        AND ati.value IS NOT NULL
                        AND abh.version_number = 1

即使我只运行续集的这一部分,它仍然返回与'INTERFACE'预订的交易

SELECT ab.deal_num
            FROM history_table abh
            INNER JOIN deal_table ab ON (
                    abh.tran_num = ab.tran_num
                    AND ab.tran_type = 0
                    )
            INNER JOIN ins i ON (
                    ab.ins_type = i.id_number
                    AND i.id_number IN (
                        605
                        ,610
                        )
                    )
            INNER JOIN personnel_table p ON (
                    abh.personnel_id = p.id_number
                    AND UPPER(p.name) NOT LIKE 'INTERFACE%'
                    )
            LEFT OUTER JOIN ab_tran_info ati ON (
                    ab.tran_num = ati.tran_num
                    )
            LEFT OUTER JOIN tran_info_types tit ON (
                    tit.type_id = ati.type_id
                    )
            WHERE rownum <1000

我想我确定了它停止排除处理'界面'的那一行。它必须是添加这两个字段的东西,续集不喜欢。

SELECT ab.deal_num
            FROM history_table abh
            INNER JOIN deal_table ab ON (
                    abh.tran_num = ab.tran_num
                    AND ab.tran_type = 0
                    )
            INNER JOIN ins i ON (
                    ab.ins_type = i.id_number
                    AND i.id_number IN (
                        605
                        ,610
                        )
                    )
            INNER JOIN personnel_table p ON (
                    abh.personnel_id = p.id_number
                    AND UPPER(p.name) NOT LIKE 'INTERFACE%'
                    )
            LEFT OUTER JOIN ab_tran_info ati ON (
                    ab.tran_num = ati.tran_num
                    )
            LEFT OUTER JOIN tran_info_types tit ON (
                    tit.type_id = ati.type_id
                    )
            WHERE 1 = 1
                AND abh.row_creation > TO_DATE('16-Mar-23 22:56:16', 'DD-MM-YY HH24:MI:SS') - 90
                AND abh.version_number = 1
xbp102n0

xbp102n01#

对我来说,似乎这一行:

INNER JOIN tran_info_types tit ON ('System Booking ID' = tit.type_name)

应该被移到WHERE子句中(这只是一个条件,它过滤type_name上的行),而JOIN本身看起来也不同。类似于:

SELECT ab.deal_num
                FROM history_table abh
                INNER JOIN deal_table ab ON (
                        abh.tran_num = ab.tran_num
                        AND ab.tran_type = 0
                        )
                INNER JOIN ins i ON (
                        ab.ins_type = i.id_number
                        AND i.id_number IN (
                            605
                            ,610
                            )
                        )
                INNER JOIN personnel_table p ON (
                        abh.personnel_id = p.id_number
                        AND UPPER(p.name) NOT LIKE 'Interface%'
                        )
                LEFT OUTER JOIN ab_tran_info ati ON (
                        ab.tran_num = ati.tran_num
                        )
                LEFT OUTER JOIN tran_info_types tit ON (
                        tit.type_id = ati.type_id
                        )
                WHERE 1 = 1
                    AND abh.row_creation > TO_DATE('16-Mar-23 22:56:16', 'DD-MM-YY HH24:MI:SS') - 90
                    AND abh.version_number = 1
                    AND ( (     tit.type_name = 'System Booking ID' 
                            AND ati.value IS NOT NULL
                           )
                        OR (     tit.type_name = 'System Source Interface' 
                            AND ati.value = 'TBG'
                           )

截至性能:首先,我会确保连接和过滤器中使用的列被索引,表和索引被分析(收集统计数据),并让优化器做它能做的事情。

相关问题