Postgresql生成大量测试数据

在PostgreSQL中如何用简单的几条SQL语句生成大量的测试数据呢?

 

此处,我简单的写一个例子,经过测试的:

(1)准备知识

 

针对 Postgres生成数据方法

<1>生成序列====》           SELECT * FROM generate_series(1,5);

<2>生成date====》    SELECT date(generate_series(now(), now() + '1 week', '1 day'));

 

<3>生成integer 随机数=》 SELECT (random()*(2*10^9))::integer;

<4>生成numeric 随机数=》select (random()*100)::numeric(4,2);

<5>生成字符串==》    select substr('abcdefghijklmnopqrstuvwxyz',1,(random()*26)::integer);

<6>生成重复串==>    select repeat('1',(random()*40)::integer);

举例:

SELECT generate_series(1,10) as key,(random()*100.)::numeric(4,2),repeat('1',(random()*25)::integer) ORDER BY random();

 key | numeric |          repeat          
-----+---------+--------------------------
   8 |   26.04 | 111
  10 |   83.44 | 1
   9 |   46.72 |
   3 |   57.84 | 1111111111111
   4 |   29.61 | 1111111111111111111
   5 |   11.32 | 1111111111111
   7 |   69.69 |
   2 |   42.23 | 11111111111111111
   6 |   12.32 | 111111111111111111111111
   1 |   84.92 | 111111

二、

如果您想知道执行该sql的时间,请在执行上述命令前设置:
postgres=# \timing on
Timing is on.

(1)测试参考SQL,可以把生成的随机值改的大一些;

#生成新表===>

select i,'text:'||i as text into test from generate_series(1,10) as i;

#在新表中插入测试数据===>

insert into test(i,text) select i,'text:'||i from generate_series(1,10) as i;

(2)查看表test占用的存储空间

若查看其中的index的空间或整个relation的空间,请参考:

http://www.postgresql.org/docs/9.1/static/functions-admin.html

          或:http://www.postgresql.org/docs/9.1/static/functions-admin.html

(3)查看整个数据库占用的硬盘空间:

 

 

(4)如果想用delete清空该表,然后真正清空硬盘空间
 从上面的步骤可以看出,vacuumdb后空间回收数据又减少到最初的6.9M的空间了。
参考自:http://blog.csdn.net/cool_cr/article/details/31745145

 

 

 

 

 

posted @ 2016-01-21 18:16  arun_yh  阅读(8604)  评论(0编辑  收藏  举报