請問如果資料量大的話,這條sql可以怎樣優化效率高?
delete from table1 where pid not in(select id from table2)
或
select * from table1 where pid not in(select id from table2)
uj5u.com熱心網友回復:
select * from table1 a where not eists (select 1 from table2 b where a.pid=b.id)uj5u.com熱心網友回復:
select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id)uj5u.com熱心網友回復:
delete mytablewhere pid in(
select id from t2
minus
select pid from mytable
)
PS:一定要多重方法反復測驗。
uj5u.com熱心網友回復:
創建臨時表,走索引
create table temp_1 as
select /*+ parallel(a,20)*/
a.id
from table2 a;
create index idx_id on temp_1(id) parallel 20;
select /*+ index (b)*/
*
from table1 a
where a.pid not exists (select 1 from temp_1 b where a.pid = b.id)
uj5u.com熱心網友回復:
資料量比較大的話用not exists效率會比較高哦select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id)
uj5u.com熱心網友回復:
兩張表的總資料量,還有TABLE1中符合not in TABLE2這個條件的資料量有多少?uj5u.com熱心網友回復:
select * from table1 a where not exists (select 1 from table2 b where a.pid=b.id)1.把 * 替換為表中的欄位名
uj5u.com熱心網友回復:
使用左連接,檢索速度非常快。select t1.*
from table1 t1
left join table2 t2
on t2.id = t1.pid
where t2.id is null
uj5u.com熱心網友回復:
關鍵看具體的場景,搞清楚當時慢在了什么地方。具體包括當時的資料環境、還有當時的執行計劃等。
uj5u.com熱心網友回復:
對于rowid洗掉資料的,如果資料量比較大,1.建議把其他表的索引洗掉或者無效
2.通過rowid洗掉
3.如數資料量特別大的時候可以考慮直接獲取create table 然后rname成原來的表名
delete from table1 t where rowid not in (select t2.rowid from table2 t1,table1 t2 where t1.pid = t2.pid )
或
select * from table1 where pid not in(select id from table2)
如果table2沒有重復資料的話,直接外連接就好了
select t.* from table1 t,table2 t1 where t.pid = t2.id(+) and t2.id is null;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/81486.html
標籤:基礎和管理
上一篇:Linux下透明網關和Oracle裝在同一臺服務器上怎么做(求詳細安裝程序)
下一篇:Oracle查詢思路-陳述句分析
