使用CONNECT BY LEVEL时查询速度非常慢

问题描述 投票:0回答:1

需求

我想在Oracle SQL中创建如下表:

COUNTRY  NAME  WMWHSE_ID   DATE
   US     CRD2     1      040620
   GR     WAZ      2      040620
   CN     KOL      3      040620
   FR     DEL      4      040620
                  ...      ...
   US     CRD2     1      030620
   GR     WAZ      2      030620
   CN     KOL      3      030620
   FR     DEL      4      030620
                  ...      ...

WMWHSE_ID中的每个仓库都将在今天的日期上打印一个DATE列,并且DATE =昨天,前天,前一天...等将重复此操作,直到1周前。我总共有124个仓库,所以总共124 * 7 = 868行。

===

查询块

以下是我的查询。它使用CONNECT BY LEVEL <=实现7个日期(理论上)

select 
SUBSTR(db_alias, 1, 2) AS COUNTRY, 
db_alias as NAME, 
To_Number(Regexp_Replace(Db_Logid, '[^0-9]', '')) As Wmwhse_Id, 
to_char(sysdate, 'yyyyMMdd')+1-level as ACTDATE 
from wmsadmin.pl_db, dual where db_alias not like '%BPV' and  db_alias not like 'PRDO%' and db_alias not like 'ENTERPRISE'
connect by level <=7
order by ACTDATE desc, WMWHSE_ID asc

((需要GROUP BY,因为没有它,表看起来像:)

COUNTRY  NAME  WMWHSE_ID   DATE
   US     CRD2     1      040620
   GR     WAZ      2      040620
   CN     KOL      3      040620
   FR     DEL      4      040620
                  ...      ...
   US     CRD2     1      030620
   US     CRD2     1      030620
   US     CRD2     1      030620
   US     CRD2     1      030620
                  ...      ...

===

问题

查询时间似乎在CONNECT BY LEVEL <= n中随着n呈指数增长。我进行了一些测试,并得到以下信息:

CONNECT BY LEVEL <= n      ROWS         SECONDS
         1                 124           2-6
         2                 248           10+?
         3                 372           110

n = 4以上的任何东西似乎都只是完全挂断了sqldeveloper。当n = 7时,我让计算机运行了30分钟以上,并且查询仍在运行。

是什么导致这种缓慢?有没有更好的方法来实现我的表?谢谢您的时间。

sql oracle
1个回答
0
投票

除了速度很慢之外,您的查询还返回了许多重复项。即使您选择不同的值,也不会更快。

但是,这样的事情可能会发生。我没有您的数据,因此我使用CTE制作了一个小样本集。

SQL> set timing on
SQL> with pl_db (country, db_alias, db_logid) as
  2    (select 'US', 'CRD2'      , 'AB1' from dual union all
  3     select 'GR', 'WAZ'       , 'CD2' from dual union all
  4     select 'CN', 'KOL'       , 'EF3' from dual union all
  5     select 'FR', 'DEL'       , 'GH4' from dual union all
  6     select 'HR', 'XBPV'      , 'IJ5' from dual union all
  7     select 'AT', 'PRDO'      , 'KL6' from dual union all
  8     select 'DE', 'ENTERPRISE', 'MN7' from dual
  9    )
 10  select
 11    substr(country, 1, 2) as country,
 12    db_alias              as name,
 13    to_number(regexp_replace(db_logid, '[^0-9]', '')) as wmwhse_id,
 14    to_char(sysdate + 1 - column_value, 'yyyymmdd') as actdate
 15  from pl_db cross join
 16    table(cast(multiset(select level from dual
 17                        connect by level <= 7
 18                       ) as sys.odcinumberlist))
 19  where db_alias not like '%BPV'
 20    and db_alias not like 'PRDO%'
 21    and db_alias not like 'ENTERPRISE'
 22  order by actdate desc, wmwhse_id asc;

COUNTRY  NAME        WMWHSE_ID ACTDATE
-------- ---------- ---------- --------
US       CRD2                1 20200604
GR       WAZ                 2 20200604
CN       KOL                 3 20200604
FR       DEL                 4 20200604
US       CRD2                1 20200603
GR       WAZ                 2 20200603
CN       KOL                 3 20200603
FR       DEL                 4 20200603
US       CRD2                1 20200602
GR       WAZ                 2 20200602
CN       KOL                 3 20200602
FR       DEL                 4 20200602
US       CRD2                1 20200601
GR       WAZ                 2 20200601
CN       KOL                 3 20200601
FR       DEL                 4 20200601
US       CRD2                1 20200531
GR       WAZ                 2 20200531
CN       KOL                 3 20200531
FR       DEL                 4 20200531
US       CRD2                1 20200530
GR       WAZ                 2 20200530
CN       KOL                 3 20200530
FR       DEL                 4 20200530
US       CRD2                1 20200529
GR       WAZ                 2 20200529
CN       KOL                 3 20200529
FR       DEL                 4 20200529

28 rows selected.

Elapsed: 00:00:00.09
SQL>

对我来说很快速;尝试一下,希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.