RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1047941
Accepted
quaresma89
quaresma89
Asked:2020-11-19 17:54:17 +0000 UTC2020-11-19 17:54:17 +0000 UTC 2020-11-19 17:54:17 +0000 UTC

MySQL 等效 INSERT IGNORE

  • 772

我们设定了将项目从 MySQL 迁移到 Oracle 的任务。对 Orakl 不熟悉,我正在学习。

通过以下方式处理重复输入错误是一种好习惯:

EXCEPTION WHEN DUP_VAL_ON_INDEX 

或者使用它会更好MERGE吗?

sql
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. Best Answer
    0xdb
    2020-01-12T03:00:01Z2020-01-12T03:00:01Z

    使用 EXCEPTION WHEN DUP_VAL_ON_INDEX 处理重复记录错误是一种好习惯,还是使用 MERGE 更好?

    提前分析数据并据此做出决策是一种很好的做法。

    • 预计要插入多少行数据
    • 重复数与新行数之比
    • 第三方数据源和不可预测

    比较可能的解决方案

    EXCEPTION WHEN DUP_VAL_ON_INDEX使用in或 trigger捕获异常FOR ... LOOP显然不是比在插入之前显式检查重复数据更好的选择。因此,我不再考虑它。

    假设已经有 50 万行,一个数据源有 100 万行,其中一半是新记录。应该注意的是,改变这个比率会显着影响结果。还从@volkovirus 获取了替代答案以进行比较。

    create table t1 as
        select rownum id, 'memo '||rownum memo
        from dual connect by level <= 5e5;
    
    create or replace view source as 
        select rownum id, 'memo '||rownum memo
        from dual connect by level <= 1e6; 
    
    alter table t1 add constraint pk_t1 primary key (id);     
    exec dbms_errlog.create_error_log (dml_table_name => 't1')
    

    运行时间比较:

    declare 
        n number; 
    begin
        --#1 not exists
        n := dbms_utility.get_time; 
        insert into t1 select * from select * from source s
        where not exists (select 1 from t1 t where t.id = s.id);
        dbms_output.put_line ('not exists elapsed='||(dbms_utility.get_time - n)/100||' ('||sql%rowcount||' rows)');
        rollback;
    
        --#2 merge
        n := dbms_utility.get_time; 
        merge into t1 t using source s on (s.id = t.id)
        when not matched then insert values (s.id, s.memo);
        dbms_output.put_line ('merge      elapsed='||(dbms_utility.get_time - n)/100||' ('||sql%rowcount||' rows)');
        rollback;
    
        --#3 log errors 
        n := dbms_utility.get_time; 
        insert into t1 select * from source s
        log errors reject limit unlimited;
        dbms_output.put_line ('w/ errlog  elapsed='||(dbms_utility.get_time - n)/100||' ('||sql%rowcount||' rows)');
        rollback;
    end;
    /
    
    

    结果:

    Elapsed: 00:10:27.073
    
    not exists elapsed=7,97 (500000 rows)
    merge      elapsed=7,76 (500000 rows)
    w/ errlog  elapsed=609,96 (500000 rows)
    

    结论:

    • 如果不需要更新/删除旧记录,
      那么检查#1 not exists就足够了
    • 如果您需要更新/删除旧记录,
      那么肯定#2 MERGE
    • 如果您需要立即记录错误并对其执行任何操作,
      那么这#3 log errors将是一个合适的解决方案
    • 5
  2. volkovirus
    2020-01-11T19:50:28Z2020-01-11T19:50:28Z

    这一切都取决于你最终想要什么。MERGE在您的情况下,可以更新匹配字段上的记录。

    您也可以不生成еxception,而是将日志表中出现插入错误的所有行都丢弃,稍后您将对其进行分析并做出决定:

    为表创建一个错误日志表:

    begin
      dbms_errlog.create_error_log (dml_table_name => 'table1');
    end;
    

    接下来,我们插入自身table1:

    insert into table1
    select *
    from source_table
    log errors into err$_table1 ('INSERT') reject limit unlimited;
    
    • 2
  3. 0xdb
    2022-04-03T01:18:53Z2022-04-03T01:18:53Z

    您可以保留插入内容并添加IGNORE_ROW_ON_DUPKEY_INDEX提示。

    文档指出,此提示是一般规则的例外,以避免在生产环境中使用提示:

    注意:CHANGE_DUPKEY_ERROR_INDEX、和IGNORE_ROW_ON_DUPKEY_INDEX提示RETRY_ON_ROW_CHANGE与其他提示的不同之处在于它们具有语义效果。
    提示中解释的一般哲学不适用于这三个提示。

    create table tab (a number unique);
    /
    Table TAB created.
    
    insert into tab
    select 1 from dual union all
    select 1 from dual
    /
    ORA-00001: unique constraint (DB.SYS_C0012764) violated
    
    insert /*+ ignore_row_on_dupkey_index (tab (a)) */ into tab
    select 1 from dual union all
    select 1 from dual
    /
    
    1 row inserted.
    
    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5