是否有更好的查询/代码,插入这些数据所需的时间更少?

时间:2015-08-23 17:49:13

标签: sql oracle query-optimization sql-insert

我正在尝试将数百万行插入表中,如下面的代码所示。我想插入前300万行然后3000万,3亿等等。可能停在3亿。

我这样做的原因是我想测试表的分区。我想在分区和未分区时在select或other语句上测试表。我做了第一次插入(300万行),这花了超过30分钟。

是否有一种简单的方法可以插入需要较少时间的数据?

declare
  c_id  number := 0 ;
begin        
  while (c_id <= 3000000)
  loop
    INSERT all

      when c_id <= 500000 then
      into city
      values(c_id,'City' || c_id || sysdate,'NY',c_id + 500 || sysdate)

      when c_id between 500001 and 1000000 then
      into city
      values(c_id,'City' || c_id || sysdate,'CA',c_id + 500 || sysdate)

      when  c_id between 1000001 and 1500000 then 
      into city
      values(c_id,'City' || c_id || sysdate,'TX',c_id + 500 || sysdate)

      when c_id between 1500001 and 2000000 then 
      into city
      values(c_id,'City' || c_id || sysdate,'WA',c_id + 500 || sysdate)

      when c_id between 2000001 and 2500000 then
      into city
      values(c_id,'City' || c_id || sysdate,'NC',c_id + 500 || sysdate)

      when c_id > 2500000 then
      into city
      values(c_id,'City' || c_id || sysdate,'SC',c_id + 500 || sysdate)

    select 1 from dual;

      c_id := c_id + 1;
  end loop;
end;

1 个答案:

答案 0 :(得分:2)

以下是我将如何生成测试数据:

HUSQVIK@panel_management> CREATE TABLE CITIES (ID NUMBER NOT NULL, CITY VARCHAR2(100) NOT NULL, STATE VARCHAR2(2) NOT NULL, SOME_DATA VARCHAR2(255)) NOLOGGING;

Table created.

Elapsed: 00:00:00.01
HUSQVIK@panel_management> ALTER SESSION FORCE PARALLEL DML;

Session altered.

Elapsed: 00:00:00.00
HUSQVIK@panel_management> INSERT /*+ append */ INTO CITIES
  2  WITH STATES AS (
  3     SELECT 'NY' STATE, 1 ID_OFFSET FROM DUAL UNION ALL
  4     SELECT 'CA' STATE, 2 ID_OFFSET FROM DUAL UNION ALL
  5     SELECT 'TX' STATE, 3 ID_OFFSET FROM DUAL UNION ALL
  6     SELECT 'WA' STATE, 4 ID_OFFSET FROM DUAL UNION ALL
  7     SELECT 'NC' STATE, 5 ID_OFFSET FROM DUAL UNION ALL
  8     SELECT 'SC' STATE, 6 ID_OFFSET FROM DUAL
  9  ),
 10  GENERATOR AS (
 11     SELECT /*+ materialize cardinality(5000000) */ (LEVEL - 1) * 6 ID FROM DUAL CONNECT BY LEVEL <= 5000000
 12  )
 13  SELECT ID + ID_OFFSET, 'City' || (ID + ID_OFFSET) || SYSDATE, STATE, ID + 500 || SYSDATE
 14  FROM GENERATOR CROSS JOIN STATES;

30000000 rows created.

Elapsed: 00:00:13.08
HUSQVIK@panel_management>

在13秒内生成并插入3000万行。但这没有任何索引。即使/ * + APPEND * /也优化了维护,每个索引都会显着减慢插入速度。