52人参与 • 2026-02-04 • Oracle
在 oracle 中,连接查询(join) 的连接类型及其性能对比
返回两个表中满足连接条件的记录。
select * from table1 t1 join table2 t2 on t1.id = t2.id;
特点:
, 和 where),默认是内连接。返回左表的全部记录,如果右表中没有匹配的记录,则右表列为 null。
select * from table1 t1 left join table2 t2 on t1.id = t2.id;
返回右表的全部记录,左表没有匹配记录的列为 null。
select * from table1 t1 right join table2 t2 on t1.id = t2.id;
返回两个表中的全部记录,没有匹配的部分列为 null。
select * from table1 t1 full outer join table2 t2 on t1.id = t2.id;
同一个表连接自身。
select a.name, b.name from employee a join employee b on a.manager_id = b.id;
返回两个表的笛卡尔积(每个表1条记录,两表组合1×1条记录)。
select * from table1 cross join table2;
select * from table1 t1, table2 t2 where t1.id = t2.id(+); -- 等价于 left join
| 连接类型 | 行数大小关系 | 匹配情况 | 性能 | 优化建议 |
|---|---|---|---|---|
| inner join | 常见,性能最好 | 有匹配 | 高效 | 使用索引字段连接 |
| left join | 左大右小 | 不一定匹配 | 中等 | 尽量加过滤条件 |
| right join | 左小右大 | 不一定匹配 | 中等 | 同上,尽量避免 |
| full outer join | 大数据慎用 | 不一定匹配 | 低效 | 避免在大数据量上使用 |
| cross join | 小表可用,谨慎使用 | 无条件连接 | 最低 | 通常不推荐 |
| self join | 中等 | 有匹配 | 中等 | 注意表别名 |
inner join 优于 outer joinouter join 多用于有缺失数据容忍时建议使用如下命令查看连接执行方式:
explain plan for select ... from ... where ...; select * from table(dbms_xplan.display);
| 算法 | 特点 | 适用情况 |
|---|---|---|
| nested loop join | 一条一条查找 | 小表驱动大表,有索引时最佳 |
| hash join | 建立哈希表进行匹配 | 大表连接,大量数据无索引时 |
| merge join | 对两个表排序再合并 | 连接列已排序或排序开销可接受 |
select o.order_id, c.customer_name, p.product_name from orders o join customers c on o.customer_id = c.customer_id join products p on o.product_id = p.product_id where o.order_date >= sysdate - 30;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论