一句Delete In 刪除語句的優化

2022-01-28 17:55:05 字數 2003 閱讀 4509

實驗環境:#類別

版本1作業系統

win10

2資料庫

oracle database 11g enterprise edition release 11.2.0.1.0 - 64bit production

3硬體環境

t440p4記憶體

8g建表:

create

table

tb_sc

( id

number

notnull

primary

key,

studentid

intnot

null

, courseid

intnot

null

, score

intnot

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

bylevel

<=

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

bystudentid,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

notexists

(select

null

from

tb_sc a,

(select studentid,courseid,max(score) as score from tb_sc group

bystudentid,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)

優化方案四:將要保留的資料存入一張臨時表,刪除原表再倒回來,這種操作最大的優勢在於降低表的水位線。

相關帖子:

--2023年2月5日--

python第一句 第一句python

內部執行過程 python內部執行過程如下 內容編碼 python直譯器在載入.py檔案中的 時,會對內容進行編碼 預設ascill ascii american standard code for information interchange,美國標準資訊交換 是基於拉丁字母的一套電腦編碼系統,...

每日一句 2014 8 26

when life gets hard and you want to give up,remember that life is full of ups and downs,and without the downs,the ups would mean nothing 當生活很艱難,你想要放棄的...

每日一句 2014 9 1

people with goals succeed because they know where they re going 有目標的人能夠成功,因為他們知道他們要去哪 people with goals succeed because they know where they re going ...