【发布时间】:2019-02-27 19:17:53
【问题描述】:
我有两张类似的桌子
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
为什么插入比select 语句中的create table 慢得多?
执行此插入的最有效方法是什么?
我知道创建视图选项,但想避免它。
【问题讨论】:
标签: sql oracle sql-insert