Oracle - 使用 EXISTS 时关系运算符无效

Oracle - Invalid relational operator when I'm using EXISTS

我收到了以下查询,当满足条件时,返回的记录从 table et_fact_reclam_ter_his_misc 中删除:

DELETE FROM et_fact_reclam_ter_his_misc
 WHERE num_siniest || num_exped IN
 (SELECT DISTINCT num_siniest || num_exped
          FROM et_fact_reclam_ter_his_misc t1
         WHERE NOT EXISTS (SELECT *
                  FROM et_fact_reclam_ter_misc t2
                 WHERE t1.num_siniest = t2.num_siniest));

我一直在搜索,发现使用 EXISTS 而不是 IN 会提高查询性能。但是我 运行 有 2 个错误。第一个是当我将 IN 运算符替换为 EXISTS:

时出现的错误消息
DELETE FROM et_fact_reclam_ter_his_misc
 WHERE num_siniest || num_exped EXISTS
 (SELECT DISTINCT num_siniest || num_exped
          FROM et_fact_reclam_ter_his_misc t1
         WHERE NOT EXISTS (SELECT *
                  FROM et_fact_reclam_ter_misc t2
                 WHERE t1.num_siniest = t2.num_siniest));


ORA-00920: invalid relational operator

第二个是当我尝试将 SELECT 嵌套在 DELETE 中时,但返回的行数不同:

DELETE FROM et_fact_reclam_ter_his_misc
 WHERE EXISTS
 (SELECT FROM et_fact_reclam_ter_his_misc
         WHERE num_siniest || num_exped IN
               (SELECT DISTINCT num_siniest || num_exped
                  FROM et_fact_reclam_ter_his_misc t1
                 WHERE NOT EXISTS
                 (SELECT *
                          FROM et_fact_reclam_ter_misc t2
                         WHERE t1.num_siniest = t2.num_siniest)));

感谢任何帮助提高查询性能的帮助

你能试试下面的方法吗,这不是连接字段的最佳方法,

DELETE FROM et_fact_reclam_ter_his_misc A
 WHERE EXISTS
 (  SELECT 1 FROM 
         (SELECT DISTINCT num_siniest || num_exped
          FROM et_fact_reclam_ter_his_misc t1) B
          WHERE A.num_siniest|| A.num_exped = B.num_siniest || B.num_exped 
         AND NOT EXISTS (SELECT 1
                  FROM et_fact_reclam_ter_misc t2
                 WHERE B.num_siniest = t2.num_siniest));

似乎连接的列是数字,它会强制 oracle 将其转换为 char,这可能会阻碍 performance.Below 如果您处理大量记录,应该更快。

DELETE FROM et_fact_reclam_ter_his_misc A
 WHERE EXISTS
 (  SELECT 1 FROM 
         (SELECT DISTINCT num_siniest, num_exped
          FROM et_fact_reclam_ter_his_misc t1) B
          WHERE A.num_siniest = B.num_siniest
          AND  A.num_exped = B.num_exped 
         AND NOT EXISTS (SELECT 1
                  FROM et_fact_reclam_ter_misc t2
                 WHERE B.num_siniest = t2.num_siniest));

DISTINCT 会矫枉过正,用 group by 代替肯定会好得多,所以最好的是:

DELETE FROM et_fact_reclam_ter_his_misc A
 WHERE EXISTS
 (  SELECT 1 FROM 
         (SELECT num_siniest, num_exped,count(1)
          FROM et_fact_reclam_ter_his_misc t1
          group by num_siniest, num_exped) B
          WHERE A.num_siniest = B.num_siniest
          AND  A.num_exped = B.num_exped 
         AND NOT EXISTS (SELECT 1
                  FROM et_fact_reclam_ter_misc t2
                 WHERE B.num_siniest = t2.num_siniest));

看起来可以这样做:

delete et_fact_reclam_ter_his_misc t1
where  not exists
       ( select * from et_fact_reclam_ter_misc t2
         where  t2.num_siniest = t1.num_siniest
       );

在原来的版本中,一级子查询好像查询的是同一个被删除的table,只是为了构造一个not exists子查询。我怀疑您想要做的就是删除 his 行,其中 num_siniest 在主要 table 中不存在。一些示例数据和预期结果会使这一点更加清晰。