zoukankan      html  css  js  c++  java
  • 一句Delete..In.. 删除语句的优化

    实验环境:

    # 类别 版本
    1 操作系统 Win10
    2 数据库 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    3 硬件环境 T440p
    4 内存 8G

    建表:

    CREATE TABLE tb_sc
    (
        id NUMBER not null primary key,
        studentid int not null,
        courseid int not null,
        score int not null
    )

    充值:

    Insert into tb_sc
    select rownum,dbms_random.value(0,10000),dbms_random.value(1,5),dbms_random.value(0,150) from dual
    connect by level<=10000
    order by dbms_random.random

    待优化的SQL,此sql在数据量大时如僵死一般:

    delete from tb_sc where (studentid,courseid,score) not in (select studentid,courseid,max(score) as score from tb_sc group by studentid,courseid);

    优化方案1:

    delete from tb_sc where id not in (select tb_sc.id from tb_sc,( select studentid,courseid,max(score) as score from tb_sc group by studentid,courseid ) tb_sc2
    where tb_sc.studentid=tb_sc2.studentid and tb_sc.courseid=tb_sc2.courseid and tb_sc.score=tb_sc2.score)

    优化方案2:

    delete from tb_sc where not exists (
    select null from tb_sc a,
                     (select studentid,courseid,max(score) as score from tb_sc group by studentid,courseid) b
    where a.studentid=b.studentid and a.courseid=b.courseid and a.score=b.score and tb_sc.id=a.id)

    优化方案3: 这种方案适用于delete语句太简单而删除数据较多的场合:

    每次删除一百条:

    delete from tb_sc where (studentid,courseid,score) not in (select studentid,courseid,max(score) as score from tb_sc group by studentid,courseid) and rownum<101

    放在循环里执行:

    while(剩余数量>0){
    
              删除符合条件的
    
    }


    优化方案四:将要保留的数据存入一张临时表,删除原表再倒回来,这种操作最大的优势在于降低表的水位线。

    相关帖子:

    https://www.cnblogs.com/xiandedanteng/p/12232822.html

    https://www.cnblogs.com/xiandedanteng/p/12232485.html

    https://www.cnblogs.com/xiandedanteng/p/12231995.html

    --2020年2月5日--

  • 相关阅读:
    ssh
    步进器&分栏控制器
    一些过期的整理前端代码
    进度条和滑动条
    UISwitcher
    定时器
    创建window
    iOS开发---UIButton 1 //创建一个可以显示图片的按钮。
    iOS开发---UILabel
    今日头条前端编程题
  • 原文地址:https://www.cnblogs.com/heyang78/p/12263253.html
Copyright © 2011-2022 走看看