Oracle - 使用 Left Join ON Where IS NULL 时性能不佳
Oracle - Bad performance when using Left Join ON Where IS NULL
作为我的标题。我有以下代码:
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 子句时可能会停止。
这个问题有什么解决办法吗?
非常感谢!
我们可以尝试将您的查询编写为直接左连接,而不求助于子查询:
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 在您的总查询中具体化中间结果。从性能的角度来看,这可能代价高昂,原因有很多。
作为我的标题。我有以下代码:
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 子句时可能会停止。 这个问题有什么解决办法吗? 非常感谢!
我们可以尝试将您的查询编写为直接左连接,而不求助于子查询:
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 在您的总查询中具体化中间结果。从性能的角度来看,这可能代价高昂,原因有很多。