oracle SQL-如何查找在其他表中跳过的行

时间:2018-10-29 17:43:18

标签: sql oracle select

我有下表:

CREATE TABLE tbl_part_1
(
  LOGID NUMBER 
, TIMESTAMP DATE 
, TASK VARCHAR2(200 CHAR)
, TERMINALIP VARCHAR2(256 CHAR) 
) 

CREATE TABLE tbl_part_2
(
  LOGID NUMBER 
, MREC_ID VARCHAR2(40 CHAR) NOT NULL 
, SUBTASK VARCHAR2(200 CHAR) 
, USER VARCHAR2(50 CHAR) 
, TRANSACTIONID VARCHAR2(100 CHAR) 
) 

CREATE TABLE tbl_full_record 
(
  REC_ID VARCHAR2(32 CHAR) NOT NULL 
, TMSTAMP DATE 
, USER VARCHAR2(250 CHAR) 
, META_RECORD VARCHAR2(2000 CHAR)   
, DATA_RECORD CLOB 
) 

在“ META_RECORD”列中,我得到了一个XML:

<Meta_Record>
    <TimeStamp>(DATE TIME HERE)</TimeStamp>
    <User>(USER NAME HERE)</User> <!-- USER from tbl_part_2 -->
    <TerminalIP>(USER IP HERE)</TerminalIP>  <!-- TERMINALIP from tbl_part_1 -->
    <Task>(USER TASK HERE)</Task> <!-- TASK from tbl_part_1 -->
    <SubTask>(USER SUBTASK HERE)</SubTask>  <!-- SUBTASK from tbl_part_2 -->
    <MetaRecordID>(MREC_ID HERE)</MetaRecordID>  <!-- MREC_ID from tbl_part_2 -->
    <TransactionID>(TRANSACTIONID HERE)</TransactionID>   <!-- TRANSACTIONID from tbl_part_2 -->
</Meta_Record>

表“ tbl_full_record”保存的数据与“ tbl_part_1”和“ tbl_part_2”相同。 但是目前我在“ tbl_full_record”中缺少一行(或者在“ tbl_part_1”中使用“ tbl_part_2”获得了很多)。
注意:有多个“ tbl_full_record”(一年中的每个月一个),但只有一次“ tbl_part_1 + 2” 映射必须通过“ tbl_full_record”的“ META_RECORD”字段完成

我想知道如何找到丢失/过多的行? 我将如何设置查询来实现这一点,假设“ tbl_part_1 + 2”的行数约为20.000.000行,而“ tbl_full_record”的行数约为1.500.000,该查询的性能还是很好的?

1 个答案:

答案 0 :(得分:1)

假设log_id和rec_id相同。

tbl_part_1,tbl_part_2中的额外行:

select tp1.*, tp2.* from tbl_part_1 tp1, tbl_part_2 tp2
      where tp1.logid = tp2.logid
   --add necessary join conditions
  and not exists ( select * from tbl_full_record tf
                    where tp1.logid = tf.rec_id
                    --add any correlated join conditions
                );