Oracle SQL CREATE TABLE 花费太长时间或无法创建(没有 CREATE TABLE 工作)
Oracle SQL CREATE TABLE takes too long time or not able to create (without CREATE TABLE works)
任何人都可以在这里提供一些意见。
我正在使用此结构创建 table、
create table user.sales_fill as
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info )a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
当我只使用 SELECT 部分时,显示结果只需要 7.5 秒。
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info
)a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
但是如果我在 select 代码之上添加 'create table'。我永远无法创建 table。
如果我使用以下 table 创建的内容(但不是正确的内容)
,我有权创建 table
create table user.sales_fill as
select
gender
,age
,sales
, 1 as logic
from sales
;
有什么建议吗?谢谢!
尝试重写子查询,使其不需要分解内爆的行(JOIN ON DISTINCT):
select
*
from
cust_info
join
sales
on
cust_info.age = sales.age
and cust_info.gender = sales.gender
union
select
name,
age,
gender,
null
from
cust_info
where
not exists(
select
*
from
sales
where
sales.age=cust_info.age and
sales.gender=cust_info.gender
)
这样就不会混淆优化器。
还要注意:create table as
可能会很慢,因为它会从扫描的表中复制索引。您可以尝试 create view
(这也具有不采用 space 和自动更新更新表的优点),或者您可以尝试不带索引的显式 create table
和 as...
然后才 insert into ... select ...
.
通过在查询末尾添加 where rownum = 0
来预先创建 table。然后做单独的插入:
CREATE TABLE misery
AS
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1)
WHERE a.ROWNUM < 1;
INSERT INTO misery( col1
, col2
, col3
, col4 )
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1);
INSERT INTO misery( col1, col2, col3 )
SELECT col1, col2, col3
FROM andhow
WHERE NOT EXISTS
(SELECT NULL
FROM andhow
WHERE andhow.col1 = misery.col1
AND andhow.col2 = misery.col2
AND andhow.col3 = misery.col3)
任何人都可以在这里提供一些意见。
我正在使用此结构创建 table、
create table user.sales_fill as
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info )a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
当我只使用 SELECT 部分时,显示结果只需要 7.5 秒。
select
a.name
,gender
,a.age
,b.sales
, 1 as logic
from
(select distinct
name, age, gender
from cust_info
)a
left join
sales b
on
a.age = b.age
and a.gender = b.gender
;
但是如果我在 select 代码之上添加 'create table'。我永远无法创建 table。
如果我使用以下 table 创建的内容(但不是正确的内容)
,我有权创建 tablecreate table user.sales_fill as
select
gender
,age
,sales
, 1 as logic
from sales
;
有什么建议吗?谢谢!
尝试重写子查询,使其不需要分解内爆的行(JOIN ON DISTINCT):
select
*
from
cust_info
join
sales
on
cust_info.age = sales.age
and cust_info.gender = sales.gender
union
select
name,
age,
gender,
null
from
cust_info
where
not exists(
select
*
from
sales
where
sales.age=cust_info.age and
sales.gender=cust_info.gender
)
这样就不会混淆优化器。
还要注意:create table as
可能会很慢,因为它会从扫描的表中复制索引。您可以尝试 create view
(这也具有不采用 space 和自动更新更新表的优点),或者您可以尝试不带索引的显式 create table
和 as...
然后才 insert into ... select ...
.
通过在查询末尾添加 where rownum = 0
来预先创建 table。然后做单独的插入:
CREATE TABLE misery
AS
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1)
WHERE a.ROWNUM < 1;
INSERT INTO misery( col1
, col2
, col3
, col4 )
SELECT a.col1
, a.col2
, a.col3
, b.col4
FROM loves a
INNER JOIN company b ON (a.col1 = b.col1);
INSERT INTO misery( col1, col2, col3 )
SELECT col1, col2, col3
FROM andhow
WHERE NOT EXISTS
(SELECT NULL
FROM andhow
WHERE andhow.col1 = misery.col1
AND andhow.col2 = misery.col2
AND andhow.col3 = misery.col3)