【问题标题】:How to clear table in oracleoracle如何清空表
【发布时间】:2012-12-21 00:46:54
【问题描述】:

我在 Oracle 8,1 中有数据表。大约有一百万行。但是很多行被相同的列重复。我需要知道清除这些数据的最快方法。 例如我有:

id name surname date
21 'john' 'smith' '2012 12 12'; 
21 'john' 'smith' '2012 12 13';
21 'john' 'smith' '2012 12 14';
....

现在我需要删除前两行,因为它们与前三列重复,并保留最新日期的行。

【问题讨论】:

标签: oracle duplicates delete-row oracle8i


【解决方案1】:
delete from table t where
exists (select * from table where id=t.id and name=t.name and surname=t.surname
        and date > t.date)

这有多快取决于您的 Oracle 参数。并且索引 (id,name,surname) 可能会有所帮助。

【讨论】:

    【解决方案2】:

    如果可能,我会选择 CTAS(创建表作为选择),截断原始表,然后将数据复制回来:

    -- create the temp table (it contains only the latest values for a given (id, name, surname) triple
    CREATE TABLE tmp as 
    SELECT id, name, surname, date1 from 
    (select 
      t1.*, 
      row_number() over (partition by id, name, surname order by date1 desc) rn
    from mytab t1)
    where rn = 1;
    
    -- clear the original table
    TRUNCATE TABLE mytab;
    
    -- copy the data back
    INSERT /* +APPEND */ INTO mytab(id,name,surname,date1) 
      (SELECT id,name,surname,date1 from tmp);
    

    【讨论】:

      【解决方案3】:

      如果确实有很多重复项,我建议只使用干净的数据重新创建表:

      CREATE TABLE tmp AS 
      SELECT id, name, surname, max(d) as d
         FROM t
        GROUP BY id, name, surname;
      

      然后用原表替换原表:

      RENAME your_table TO old_table;
      RENAME tmp_table TO your_table;
      

      不要忘记移动索引、约束和权限...

      【讨论】:

      • 我会向您提出与向 OP 提出的相同要求:将“lots”定义为表中所有行的百分比。
      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多