0

任何人都可以在这里提供一些意见。

我正在使用这个结构来创建表格,

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
     ;

但是如果我在此选择代码之上添加“创建表”。我永远无法创建表。

如果我使用以下创建的表(但不是正确的内容),我有权创建表

create table user.sales_fill as  
select

                gender
                ,age
                ,sales
                , 1 as logic
        from  sales 
         ;

有什么建议吗?谢谢!

4

2 回答 2

0

尝试重写子查询,使其不需要分解内爆行(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(这也具有不占用空间并使用更新的表自动更新的优点),或者您可以尝试显式create table没有索引as...并且只有 then insert into ... select ...

于 2019-03-18T14:58:55.093 回答
0

where rownum = 0通过添加到查询的末尾来预先创建表。然后做单独的插入:

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)
于 2019-03-19T02:12:30.283 回答