sql - 在 Oracle SQL 表中插入值的有效方法
问题描述
我有两张桌子
create table nodes_tbl as (
select 'a' as nodeid, 'some string' as dummy_string, 0 as subnetid from dual union all
select 'b', 'qwe', 0 from dual union all
select 'c', 'asd', 0 from dual union all
select 'd', 'zxc', 0 from dual union all
select 'e', 'rty', 0 from dual);
和
create table subnets as (
select 'a' as nodeid, 1 as subnetid from dual union all
select 'b', 2 from dual union all
select 'c', 2 from dual union all
select 'd', 3 from dual union all
select 'e', 4 as nodeid from dual);
拥有数百万条记录的连接可以快速运行。
select n.NODEID, n.DUMMY_STRING, s.subnetid
from nodes_tbl n, subnets s where s.nodeid=n.nodeid
写入速度也很快
create table test_tbl as n.NODEID, s.subnetid
from nodes_tbl n, subnets s where s.nodeid=n.nodeid --10M records in 2s.
但是,当我尝试更新表并向列添加值时,查询非常慢
UPDATE nodes_tbl n
SET subnetid = (SELECT subnetid
FROM subnets s
WHERE s.nodeid = n.nodeid)
WHERE EXISTS (
SELECT subnetid FROM subnets s
WHERE s.nodeid = n.nodeid) --8 minutes for 100K records
为什么 insert 比create table
fromselect
语句慢得多?执行此插入的最有效方法是什么?
我知道创建视图选项,但想避免它。
解决方案
请尝试MERGE
:
merge into nodes_tbl n
using (select s.subnetid, s.nodeid
from subnets s
) x
on (x.nodeid = n.nodeid)
when matched then update set
n.subnetid = x.subnetid;
有什么改善吗?
顺便说一句,您是否NODEID
在两个表中的列上都创建了索引?
推荐阅读
- python - Python从输入文件中绘制数据
- python - SciPy stats lognormal distribution 获得具有给定对数正态分布均值和对数正态分布几何标准差的pdf
- json - 如何将 json 参数变量编码为一个休息尖锐的请求体
- gatling - 在 Gatling Scala 中将提取的数据从会话写入 CSV
- powershell - powershell:倒数计时器
- javascript - 为什么我在未构建时出现错误,而在构建时却没有?
- c - 在 C 中,对 void* 变量进行数学运算的最有效方法是什么
- c++ - 如何在 Mediapipe 中更改或设置“配置选项”?
- swift - 迅速。您如何计算从 tvOS 遥控器滑动的速度?
- javascript - Immer Produce 不会导致 Redux 重新渲染。在 Reducer 中修改拔模和基本状态