请问这条sql可以怎样优化,效率更高?

[复制链接]
查看11 | 回复10 | 2021-1-27 06:27:37 | 显示全部楼层 |阅读模式
请问如果数据量大的话,这条sql可以怎样优化效率高?
deletefromtable1wherepidnotin(selectidfromtable2)

select*fromtable1wherepidnotin(selectidfromtable2)
分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
select*fromtable1awherenoteists(select1fromtable2bwherea.pid=b.id)
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
select*fromtable1awherenotexists(select1fromtable2bwherea.pid=b.id)

回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
deletemytable
wherepidin(
selectidfromt2
minus
selectpidfrommytable

PS:一定要多重方法反复测试。
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层

创建临时表,走索引
createtabletemp_1as
select/*+parallel(a,20)*/
a.id
fromtable2a;
createindexidx_idontemp_1(id)parallel20;
select/*+index(b)*/
*
fromtable1a
wherea.pidnotexists(select1fromtemp_1bwherea.pid=b.id)
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
数据量比较大的话用notexists效率会比较高哦
select*fromtable1awherenotexists(select1fromtable2bwherea.pid=b.id)

回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
两张表的总数据量,还有TABLE1中符合notinTABLE2这个条件的数据量有多少?
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
select*fromtable1awherenotexists(select1fromtable2bwherea.pid=b.id)
1.把*替换为表中的字段名

回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
使用左连接,检索速度非常快。
selectt1.*
fromtable1t1
leftjointable2t2
ont2.id=t1.pid
wheret2.idisnull
回复

使用道具 举报

千问 | 2021-1-27 06:27:37 | 显示全部楼层
关键看具体的场景,搞清楚当时慢在了什么地方。
具体包括当时的数据环境、还有当时的执行计划等。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行