Oracle-在IS NULL处使用Left Join ON时性能不佳

时间:2018-11-02 05:08:00

标签: oracle left-join isnull

作为我的头衔。我有以下代码:

SELECT * FROM
(SELECT "Student_ID", "ID_Card", "First_Name", "Last_Name" FROM "STUDENT@DBLA
 WHERE "YEAR" = 2018 AND "SEX" = 'FEMALE') T
LEFT JOIN (SELECT Student_ID, ID_Card, First_Name, Last_Name FROM schema.STUDENTS
           WHERE YEAR = 2018 AND SEX = 'FEMALE') D
ON D.ID_Card = T."ID_Card" WHERE D.ID IS NULL;

此查询运行得很低,可能在后面添加Where is null子句时停止。 这个问题有解决方案吗? 非常感谢!

1 个答案:

答案 0 :(得分:0)

我们可以尝试将查询写为直接左联接,而无需借助子查询:

SELECT
    t1.Student_ID,
    t1.ID_Card,
    t1.First_Name,
    t1.Last_Name,
    t2.Student_ID AS Student_ID_2
    t2.ID_Card AS ID_Card_2,
    t2.First_Name AS First_Name_2,
    t2.Last_Name AS Last_Name_2
FROM "STUDENT@DBLA" t1
LEFT JOIN schema.STUDENTS t2
    ON t1.ID_Card = t2.IDCard AND
       t2.YEAR = 2018 AND t2.SEX = 'FEMALE' AND t2.ID IS NULL
WHERE
    t1.YEAR = 2018 AND t1.SEX = 'FEMALE';

您可能要避免在此处使用子查询的原因是,它可以强制Oracle在您的总查询中实现中间结果。从性能的角度来看,这可能是昂贵的,原因有很多。