Create table contains 10 million records shortly

How to create huge table which contains 10 million records shortly

If you want to take performance test or sql optimization, you should need huge tables.

But if there is no data, how do you create huge table in a short term?

Yes,you can use batch insert grammar provided by oracle,just like this:

INSERT ALL
INTO firsttb(NAME, age,createdtime) values('E1','22',sysdate)
INTO firsttb(NAME, age,createdtime) values('E2','32',sysdate)
INTO firsttb(NAME, age,createdtime) values('E3','42',sysdate)
...
INTO firsttb(NAME, age,createdtime) values('E250','42',sysdate)
select * from dual

By Java program,you can write many "into..." statements between "Insert all" and "select * from dual" ,and push all sentences to DB one time。

But, this method is limited,it depends on your computer's performance. On my poor T440p, I only can write at most 250 "INTO..." sentences between "Insert all" and "select * from dual".

So I need call Oracle for 4 times to submit 1,000 records and it will take ten minutes to submit 1,000,000 records, further more, it will take more than one hour to submit 10 million records.

After I finish filling three tables, I have nothing to do but go to the bed because it is too late...

It is too long and miserable , we have to look for new method.

Several days later, I figure out the following steps:

1.Create a table whose structure is same as the huge table :

CREATE TABLE HY_million
(
id NUMBER not null primary key,
name NVARCHAR2(60) not null,
score NUMBER(4,0) NOT NULL,
createtime TIMESTAMP (6) not null
)

2.Using following statements to insert 2,000,000 records into HY_million:

*Insert into HY_million
select rownum,dbms_random.string('
',dbms_random.value(6,20)),dbms_random.value(0,20),sysdate from dual
connect by level<=2000000
order by dbms_random.random**

Attention: record count:2,000,000 depends on your machine, maybe you can hence it to 10,000,000 if your computer permits, otherwise you have to follow the steps below:

3.Commit firstly, and then create the target huge table:

*create table hy_million2 as select from HY_million**

check how many records are there in the table hy_million2:

*select count() from hy_million2**

You will find the count is 2,000,000. Now hy_million2 has the same structure as hy_million and there is no constraints, which is convenient to fill records.

4.Next, run the following statements for four times:

*insert into hy_million2 select from HY_million**

After completion, there should be 10,000,000 records in hy_million2

5.And run the sql below to format the id column regularly:

update hy_million2 set id=rownum where 1=1

Let's check the format result:

select count(distinct id) from hy_million2

If no mistake, the count should be 10,000,000.

And then commit again.

6.At last, set the primary key to table:'hy_million2 '

ALTER TABLE hy_million2 ADD CONSTRAINT constraint_name PRIMARY KEY (id);

Now you can use and enjoy the huge table.

Appendix: my operation logs:
SQL> CREATE TABLE HY_million
2 (
3 id NUMBER not null primary key,
4 name NVARCHAR2(60) not null,
5 score NUMBER(4,0) NOT NULL,
6 createtime TIMESTAMP (6) not null
7 );

表已创建。

SQL> Insert into HY_million
2 select rownum,dbms_random.string('*',dbms_random.value(6,20)),dbms_random.value(0,20),sysdate from dual
3 connect by level<=2000000
4 order by dbms_random.random;

已创建2000000行。

SQL> commit;

提交完成。

SQL> create table hy_million2 as select * from HY_million;

表已创建。

SQL> select count(*) from hy_million2;

COUNT(*)

2000000

SQL> insert into hy_million2 select * from HY_million;

已创建2000000行。

SQL> insert into hy_million2 select * from HY_million;

已创建2000000行。

SQL> insert into hy_million2 select * from HY_million;

已创建2000000行。

SQL> insert into hy_million2 select * from HY_million;

已创建2000000行。

SQL> update hy_million2 set id=rownum where 1=1;

已更新10000000行。

SQL> select count(distinct id) from hy_million2;

COUNT(DISTINCTID)

10000000

SQL>

SQL> ALTER TABLE hy_million2 ADD CONSTRAINT constraint_name PRIMARY KEY (id);

表已更改。

SQL> commit;

提交完成。

猜你喜欢

转载自blog.51cto.com/7726611/2467156