常见的SQL等价改写
表结构
SQL> desc scott.emp
Name Type Nullable Default Comments
-------- ------------ -------- ------- --------
EMPNO NUMBER(4) Y
ENAME VARCHAR2(10) Y
JOB VARCHAR2(9) Y
MGR NUMBER(4) Y
HIREDATE DATE Y
SAL NUMBER(7,2) Y
COMM NUMBER(7,2) Y
DEPTNO NUMBER(2) Y
SQL> desc scott.dept
Name Type Nullable Default Comments
------ ------------ -------- ------- --------
DEPTNO NUMBER(2)
DNAME VARCHAR2(14) Y
LOC VARCHAR2(13) Y
半连接
select * from scott.emp a
where deptno in(select deptno from scott.dept b where a.deptno = b.deptno)
等价
select * from scott.emp a
where exists(select 1 from scott.dept b where a.deptno = b.deptno)
等价
select * from scott.emp a,(select deptno from scott.dept ) b
where a.deptno = b.deptno
反连接
select * from scott.emp a
where deptno not in (select deptno from scott.dept b where a.deptno = b.deptno)
等价
select * from scott.emp a
where not exists(select 1 from scott.dept b where a.deptno = b.deptno)
等价
select * from scott.emp a left join scott.dept b
on a.deptno = b.deptno
where b.deptno is null
标量子查询
select ename, (select dname from scott.dept b where a.deptno = b.deptno)
from scott.emp a
where empno = 7499
等价
select ename, dname
from scott.emp a left join scott.dept
on a.deptno = b.deptno
where empno = 7499
表反复查询
select ename from scott.emp
where empno in (select max(empno) from scott.emp group by deptno)
等价
select ename from (
select ename ,
empno,
max(empno)over(partition by deptno) max_empno
from scott.emp
) where empno=max_empno
select * from (
select count(*) from scott.emp where deptno=10),
(
select count(*) from scott.emp where deptno=20)
等价
select
sum(case when deptno=10 then 1 end),
sum(case when deptno=20 then 1 end)
from scott.emp
with可以改写sql中反复出现的查询体
在一些特定场景中(子查询加层次查询,rownum等),sql的某种写法,会导致执行计划有问题,走的是cost较高的执行计划。
所以需要sql改写,来让sql优化器能找出正确的执行计划。
还有就是sql的逻辑,多次查询同一个表与查询一次就能获得结果集,那个效率高很明显赛。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY