Postgres快速创建大量测试数据

DROP TABLE  IF EXISTS color;
CREATE TABLE color(
    cname VARCHAR(20),
    remark VARCHAR(20)
);
insert into color select round(random()*100),round(random()*1000) from generate_series(1,10000000);

插入10000000数据之后,用时51秒

对于generate_series函数,还可以生成时间:

david=# select generate_series(now(), now() + '7 days', '1 day');
        generate_series        
-------------------------------
 2013-04-03 14:22:26.391852+08
 2013-04-04 14:22:26.391852+08
 2013-04-05 14:22:26.391852+08
 2013-04-06 14:22:26.391852+08
 2013-04-07 14:22:26.391852+08
 2013-04-08 14:22:26.391852+08
 2013-04-09 14:22:26.391852+08
 2013-04-10 14:22:26.391852+08
(8 rows)

david=# select generate_series(to_date('20130403','yyyymmdd'), to_date('20130404','yyyymmdd'), '3 hours');  
    generate_series     
------------------------
 2013-04-03 00:00:00+08
 2013-04-03 03:00:00+08
 2013-04-03 06:00:00+08
 2013-04-03 09:00:00+08
 2013-04-03 12:00:00+08
 2013-04-03 15:00:00+08
 2013-04-03 18:00:00+08
 2013-04-03 21:00:00+08
 2013-04-04 00:00:00+08
(9 rows)

 

posted @ 2017-02-06 16:19  人生如若初见  阅读(1218)  评论(0编辑  收藏  举报