1. 程式人生 > >Postgresql 生成大量資料的方法

Postgresql 生成大量資料的方法

1、順序數
例子,生成一批順序值。

postgres=# select id from generate_series(1,10) t(id);  
id 
----  
  1  
  2  
  3  
  4  
  5  
  6  
  7  
  8  
  9  
 10  
(10 rows)  

2、隨機數

random()
例子,生成一批隨機整型

postgres=# select (random()*100)::int from generate_series(1,10);  
 int4   
------  
   14  
   82  
   25  
   75  
    4  
   75  
   26  
   87  
   84  
   22  
(10 rows)  

3、隨機字串

md5(random()::text)
例子,生成一批隨機字串

postgres=# select md5(random()::text) from generate_series(1,10);  
               md5                  
----------------------------------  
 ba1f4f4b0073f61145a821c14437230d  
 a76b09292c1449ebdccad39bcb5864c0  
 d58f5ebe43f631e7b5b82e070a05e929  
 0c0d3971205dc6bd355e9a60b29a4c6d  
 bd437e87fd904ed6ecc80ed782abac7d  
 71aea571d8c0cd536de53fd2be8dd461  
 e32e105db58f9d39245e3e2b27680812  
 174f491a2ec7a3498cab45d3ce8a4277  
 563a7c389722f746378987b9c4d9bede  
 6e8231c4b7d9a5cfaae2a3e0cef22f24  
(10 rows)  

4、重複字串

repeat(‘abc’, 10)
例子,生成重複2次的隨機字串

postgres=# select repeat(md5(random()::text),2) from generate_series(1,10);  
                              repeat                                
------------------------------------------------------------------  
 616d0a07a2b61cd923a14cb3bef06252616d0a07a2b61cd923a14cb3bef06252  
 73
bc0d516a46182b484530f5e153085e73bc0d516a46182b484530f5e153085e e745a65dbe0b4ef0d2a063487bbbe3d6e745a65dbe0b4ef0d2a063487bbbe3d6 90f9b8b18b3eb095f412e3651f0a946c90f9b8b18b3eb095f412e3651f0a946c b300f78b20ac9a9534a46e9dfd488761b300f78b20ac9a9534a46e9dfd488761 a3d55c275f1e0f828c4e6863d4751d06a3d55c275f1e0f828c4e6863d4751d06 40e609dbe208fc66372b1c829018097140e609dbe208fc66372b1c8290180971 f661298e28403bc3005ac3aebae49e16f661298e28403bc3005ac3aebae49e16 10d0641e40164a238224d2e16a28764710d0641e40164a238224d2e16a287647 450e599890935df576e20c457691c421450e599890935df576e20c457691c421 (10 rows)

5、隨機中文

create or replace function gen_hanzi(int) returns text as $$    
declare    
  res text;    
begin    
  if $1 >=1 then    
    select string_agg(chr(19968+(random()*20901)::int), '') into res from generate_series(1,$1);    
    return res;    
  end if;    
  return null;    
end;    
$$ language plpgsql strict;   
postgres=# select gen_hanzi(10) from generate_series(1,10);  
      gen_hanzi         
----------------------  
 騾歵癮崪圚禎驟氾準賠  
 縬寱癱辦戾薶竅爉充環  
 鷊趕輪肸蒹焷尮稟漽湯  
 庰槖諍蜞礀鏈懼珿憗膃  
 憭釃輪訞陡切瀰煈瘐獵  
 韸琵慆蠑啈響夐捶燚積  
 菥芉阣瀤樂潾敾糩镽礕  
 廂壠欳事鎤懯劑搯薔窡  
 覤綊伱鳪散噹鐨灳毯杸  
 鯷倯鰂鏨牓晟挗覷鎛壯  
(10 rows)  

6、隨機陣列

create or replace function gen_rand_arr(int,int) returns int[] as $$    
  select array_agg((random()*$1)::int) from generate_series(1,$2);    
$$ language sql strict;    
postgres=# select gen_rand_arr(100,10) from generate_series(1,10);  
          gen_rand_arr             
---------------------------------  
 {69,11,12,70,7,41,81,95,83,17}  
 {26,79,20,21,64,64,51,90,38,38}  
 {3,64,46,28,26,55,39,12,69,76}  
 {66,38,87,78,8,94,18,88,89,1}  
 {6,14,81,26,36,45,90,87,35,28}  
 {25,38,91,71,67,17,26,5,29,95}  
 {82,94,32,69,72,40,63,90,29,51}  
 {91,34,66,72,60,1,17,50,88,51}  
 {77,13,89,69,84,56,86,10,61,14}  
 {5,43,8,38,11,80,78,74,70,6}  
(10 rows)  

7、連線符

postgres=# select concat('a', ' ', 'b');  
 concat   
--------  
 a b  
(1 row)  

8、自定義函式

通過自定義函式,可以生成很多有趣的資料。

隨機身份證號

create or replace function gen_id(    
  a date,    
  b date    
)     
returns text as $$    
select lpad((random()*99)::int::text, 2, '0') ||     
       lpad((random()*99)::int::text, 2, '0') ||     
       lpad((random()*99)::int::text, 2, '0') ||     
       to_char(a + (random()*(b-a))::int, 'yyyymmdd') ||     
       lpad((random()*99)::int::text, 2, '0') ||     
       random()::int ||     
       (case when random()*10 >9 then 'X' else (random()*9)::int::text end ) ;    
$$ language sql strict;    
postgres=# select gen_id('1900-01-01', '2017-10-16') from generate_series(1,10);  
       gen_id         
--------------------  
 25614020061108330X  
 49507919010403271X  
 96764619970119860X  
 915005193407306113  
 551360192005045415  
 430005192611170108  
 299138191310237806  
 95149919670723980X  
 542053198501097403  
 482334198309182411  
(10 rows)  

By 天蠍座