12

大量のデータをテーブルに入れる方法がわかりません。データは繰り返さないでください

アドバイス、他の方法があるかもしれませんか?

create table COUNTRIES (
  COUNTRY_ID   VARCHAR2(7),
  COUNTRY_NAME VARCHAR2(40),
  constraint COUNTRY_C_ID_PK primary key (COUNTRY_ID)
);


Begin
For IDS in 1..1000000
Loop
INSERT INTO "SYSTEM"."COUNTRIES" (COUNTRY_ID, COUNTRY_NAME) VALUES (dbms_random.string('L', 7), dbms_random.string('L', 15));
Commit;
End loop;
End; 
4

2 に答える 2

29

データの量だけが必要で、コンテンツのランダム性を気にしない場合は、

 insert into countries select rownum, 'Name'||rownum from dual
   connect by rownum<=1000000;

トリックを行う必要があります。

于 2012-07-31T10:53:26.960 に答える
5

ランダムの非常に具体的な定義があり、重複を許可できない場合、例外処理は重複を回避するのに役立ちます。

この方法は非常に遅くなります。これを複数回行う必要がある場合、または大量のデータを処理する必要がある場合は、「ランダム」の定義を緩和して、Erichのようなソリューションを使用することをお勧めします。

--Create temporary unique constraint.  (Assuming you want each column to be unique?)
alter table countries add constraint countries_name_uq unique (country_name);

--Insert random data until it worked 1 million times.
Declare
    rows_inserted number := 0;
Begin
    Loop
        Begin
            INSERT INTO COUNTRIES(COUNTRY_ID, COUNTRY_NAME)
            VALUES(dbms_random.string('L', 7), dbms_random.string('L', 15));
            --Only increment counter when no duplicate exception
            rows_inserted := rows_inserted + 1;
        Exception When DUP_VAL_ON_INDEX Then Null;
        End;
        exit when rows_inserted = 1000000;
    End loop;
    commit;
End;
/

--Drop the temporary constraint
alter table countries drop constraint countries_name_uq;

--Double-check the count of distinct rows
select count(*) from
(
    select distinct country_id, country_name from countries
);

Result
------
1000000
于 2012-08-01T06:20:41.847 に答える