GaussDB(DWS)批量生成数据的方法
【摘要】 GaussDB(DWS)提供了非常多的函数用来造数,用于常规测试。
GaussDB(DWS)提供了非常多的函数用来造数,用于常规测试。
相关函数
日期,时间,间隔,时间计算等
- now()
- current_date
- current_time
- curtime() 带时区
- curtime()::time without time zone 不带时区
- to_timestamp(sec)
- date_part(‘field’, source)
- INTERVAL ‘1 day 10 second’
- TIMESTAMP ‘2001-02-16 20:38:40’
- age(timestamp, timestamp)
- adddate(date, interval | int)
- addtime(timestamp | time | text, interval | text)
- subdate(date, interval | int)
uuid生成
- uuid()
- uuid_generate_v1()
- sys_guid()
随机数生成
- setseed(double precision) 为随后的随机设置种子
- random()
- rand()
随机字符串
- md5(random()::text)
批量生成数字
- range(1,100)
- generate_series(1,100)
- row_number()
- rank()
批量生成时间
- generate_series(now(), now()+ interval ‘1 day’, interval ‘1 second’)
取模
- abs(mod(100,2))
生成多维随机数组
SELECT array_fill(random(), ARRAY[1024]);
生成固定长度随机字符
postgres=> SELECT string_agg(chr(33 + floor(random() * 94)::int), '')
postgres-> FROM generate_series(1, 1000);
string_agg
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,<:5{YjWv}qRWX8ncxi2<ub]B9Oa]!9Uz<:9C2h0'7s?k0&3z5;G5|}#D,<o,Z%R-RpH24C{\G}m79+F{`,!o)&;cNX~%6Ai[;W`Y2p0wvj-=3Z<V.^Grm[@c7'U~Xndi;G;EbMigFeIJy;)NidV$|p+@?0n{QxP|ZlY{qc]b+H&<W};M6%\n19}T<z?q-)9dqn'ZhtsnKR<;x@\UT@)8a&c\\GlOF![;crZfeDn\aIG}N'zjF!=/D[^jw2K:8{EmIEDT<nB>'Xuqh3pI7p'
3Xu[@XYHv5(%HA9}+Fo;@\<5()soh&]*9!I#LZhM>k7L}-oxix\,,-B?TA)3Rn*/\8)L</B3W'+*|_EN0+#r_iA`@3V/+Ci{6wZn#;1i}hZGBCS6bc!^kk#e{hNW$9a12>5L/DD&hpV(H$+0JPGv0Y^[:wJco[;0ndeo'*c"u21&g{+iuU+aZ1n~5wk9We`TX4&(~nQ5Vs`OZgm7d`lOyV#5A~nA2Lb37,H!S/6^pA")efvn(`$j"N!njz-?_e\i15S4=Y',axu/ihhw+(AqO
DTrlnXGytDp)c%^N{U-e|RQ2nPuhj^m/s@Oy<SLs^E,33F2}nuN!}x\YZ2x?8ni\u)-9$La.:)<=)/N>C@GT}(#x[OXxrIBY=]5'&D=418`zu.de=^c%'NJGf=dTDaORr2?.<$r6nszhZ,1E,eIbK_.mD%zE&0.vzpdqhPX||v4f3@P7=@1+-hF:P<lyiv!y~i!8QL']"kA0*Mv/{\fQKXM1iO-M8Tu|$-lnPHkjd[0!a/M2'JlTa]k1+_NpaxU:&wp*W4~o]Mt4ln,,]M<UY
!t_"5hH;"-y%nL8KR@vTh;#x^mv-)s).N^$G%=x7ReN$g,6S41'xDA4'`-Ef{ZHemIik=!/pbf![~Wr{NBx>HmAO{wMTdfW?X;(j%%,aTeW:?f.:,'75U85"{vor0+teW.`|DSv*h^|k0g<fDLrZ{X\df7L(t5VZ(vP_FP>aHB
(1 row)
postgres=> SELECT left(string_agg(md5(random()::text), ''), 1000)
postgres-> FROM generate_series(1, 32);
left
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
449ec8472f6741d8b06f665c70b1cd7d90f466946624a2a4dafe44ca0760766067cc02650352e742aded7e425577d842548123e8caa70afd80e31c157d376df2c5a5972da440945c0b18e4ee3bbaf1c789dccc98ab7aa2a078a9b78de622d188865543ee4404ad045c10a475871eeaf58a6c253e8b6d4c8182ad7b34755b77db61d0e38b428a07fdc0f9
8620d7d878aa44ba2047fb9bd9a5747e917de7f69905974466413e78c030c9a12f91cc6d2840ea671cb8cf2cc48d2c53a19ddcf69f61dd128bc8202bfd72429b092ae60a31d44b5300267d3f93c500626e9618d62e20afe4c492606db3228650d2800fa0362afa6d75f91089e63dfb5d10e33b63fe80213cf790b80d5d0ef8c59911b90edb0f638a3e2e9
07be3905806674308365d0232a5cf799bd14e067814d0c50324f28ee4bc53dad36fe908178202efe3ec0066b81caadb201f25a1988d2536f7be177466f79570dbcb56fa769c8f3e687b6d43a8cfeeb0a8d68c9245eeafea8c30055fc153ba9218673095f975a5002ffb70c88b81656da0937ad763c186856aec30a7f365967aa95f00a55234f8dfcb34fc
6ef9b3d73d2540e2cd39941ac380fdaf16b59e943dc87ef1241a0450966514832a54499a707bf2cfb1221868c6166cf96ac5c137e36e708f7097fa4068e0b31bc43addc2e7e37cbbcb9a9512b74c02110a14ed8576
(1 row)
postgres=>
postgres=> SELECT array_fill(random(), ARRAY[10]) from generate_series(1,10);
array_fill
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014,0.280953919515014}
{0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791,0.357142751570791}
{0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798,0.367632256820798}
{0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972,0.748980359639972}
{0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159,0.811735593248159}
{0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422,0.360405946150422}
{0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513,0.0137217403389513}
{0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348,0.802638200577348}
{0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747,0.165215156506747}
{0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678,0.094106154050678}
(10 rows)
postgres=>
用例
create table test(id int, now_time timestamp, name varchar, age float) with(orientation=column) distribute by hash(id);
insert into test select 1, now();
insert into test select 2, current_timestamp::TIMESTAMP(0);
insert into test select 3, to_timestamp(2);
insert into test select 4, TIMESTAMP '2001-02-16 20:38:40';
insert into test select 5, now()+ INTERVAL '10 day 10 second';
insert into test select 6, now(), date_part('year', now())::text;
insert into test select 7, now(), uuid();
insert into test select 8, now(), uuid_generate_v1();
insert into test select 9, now(), sys_guid();
insert into test select 10, now(), uuid(),random();
insert into test select 11, now(), generate_series(now(), now()+ interval '1 month', interval '1 day');
insert into test select generate_series(12,100), generate_series(now(), now()+ interval '1 month', interval '1 day'),md5(random()::text) ;
postgres=# select * from test order by 1;
id | now_time | name | age
-----+----------------------------+--------------------------------------+----------------
1 | 2024-03-04 12:03:33.820218 | |
2 | 2024-03-04 12:03:34 | |
3 | 1970-01-01 08:00:02 | |
4 | 2001-02-16 20:38:40 | |
5 | 2024-03-14 12:03:43.910831 | |
6 | 2024-03-04 12:03:33.93528 | 2024 |
7 | 2024-03-04 12:03:33.966333 | 65e54815-c1b9-0e00-1276-b77ac6d0fffe |
8 | 2024-03-04 12:03:33.999056 | 65e54816-03bf-0000-1277-b77ac6d0fffe |
9 | 2024-03-04 12:03:34.011676 | 65E54816321C00001278B77AC6D0FFFE |
10 | 2024-03-04 12:03:34.02812 | 65e54816-76ac-0000-1279-b77ac6d0fffe | .4850455801934
11 | 2024-03-04 12:03:35.182978 | 2024-03-04 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-15 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-19 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-05 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-16 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-18 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-06 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-20 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-21 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-07 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-22 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-23 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-08 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-24 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-25 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-09 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-26 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-10 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-27 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-28 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-11 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-29 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-30 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-12 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-31 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-04-01 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-13 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-04-02 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-04-03 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-14 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-03-17 12:03:35.182978+08 |
11 | 2024-03-04 12:03:35.182978 | 2024-04-04 12:03:35.182978+08 |
12 | 2024-03-12 12:03:51.39628 | 80654ac7f1191271e6c8ca75fe92b276 |
12 | 2024-03-22 12:03:51.39628 | 06dafef5d02701b9fc792abc5753c7d3 |
12 | 2024-03-20 12:03:51.39628 | 248fd8b916d2993e8d9bd634e30afe25 |
12 | 2024-03-28 12:03:51.39628 | 57757baf520a40b065b8003e8daee597 |
12 | 2024-03-24 12:03:51.39628 | 743da9190b457a692f5939d02acd1eec |
12 | 2024-03-19 12:03:51.39628 | b93818c49f3142c631de885d9b5d3dbc |
12 | 2024-04-02 12:03:51.39628 | 315c285e87ab131e7d85e0bdf2b4ec0a |
12 | 2024-03-27 12:03:51.39628 | 328e35ad88a0d380041537156755d115 |
12 | 2024-03-17 12:03:51.39628 | 2f15525e52f79dfad662c727af923f0c |
12 | 2024-03-25 12:03:51.39628 | 68b4c285b45960281c7ef4d1c5cf92fb |
12 | 2024-03-06 12:03:51.39628 | b03843547cc25a94353227bceadcfaeb |
12 | 2024-04-04 12:03:51.39628 | 09357ca099a02615cce4aaa4eb6535d2 |
12 | 2024-03-10 12:03:51.39628 | 16f3b6b35ae050eca7fcde328186ca76 |
12 | 2024-04-03 12:03:51.39628 | fd4d67aafff38d1a5586acb8b7083753 |
12 | 2024-03-26 12:03:51.39628 | 5e4e01d7c193b83b7f5e8f75705780d5 |
12 | 2024-03-18 12:03:51.39628 | 63db17942c18dc7882b67a79cd8a4555 |
12 | 2024-03-16 12:03:51.39628 | 764eddbddb00e934bdb23a513ddbf890 |
12 | 2024-03-30 12:03:51.39628 | d911b89436e70c25f395f2bee1adf147 |
12 | 2024-03-14 12:03:51.39628 | 81265074d2f6e3f1b6ca18a6f99f5ea5 |
12 | 2024-03-15 12:03:51.39628 | a5aa4135c61059a265d2175f176c399d |
12 | 2024-03-29 12:03:51.39628 | af40a343683d630073b621041418d823 |
12 | 2024-03-09 12:03:51.39628 | 9f232a88da6f2a2dc83c0d51f1df8f9d |
12 | 2024-03-11 12:03:51.39628 | 190d9b9d68b086bb43f53c56509172e2 |
12 | 2024-03-13 12:03:51.39628 | 4bf1f42a3188b649b9a74c5e9beae532 |
12 | 2024-03-07 12:03:51.39628 | c89ffd9bfa3b29eb73662326e91dbb8b |
12 | 2024-03-05 12:03:51.39628 | 6354da38ea3f6f1852b6990fdcc23dc4 |
12 | 2024-03-21 12:03:51.39628 | 73a3b26be0dc9b1d35308d2ec5c66e6d |
12 | 2024-03-23 12:03:51.39628 | 5f02735a0504e64e4d3ee175db00c0d2 |
12 | 2024-04-01 12:03:51.39628 | 3e33f7d2c1d85bc8c6ce2954aad62998 |
12 | 2024-03-08 12:03:51.39628 | f8bca07d9cf605b731a20cbbc1aff3ac |
12 | 2024-03-31 12:03:51.39628 | 01bdd7c0d74f5b38157851a736c8988a |
12 | 2024-03-04 12:03:51.39628 | df7455fcc0ee854a02e3bf7769b0d16b |
13 | 2024-03-16 12:03:51.39628 | 7f7ab8cb379000953a244f191d29ce83 |
13 | 2024-03-17 12:03:51.39628 | 552d726339450083550cec47637716d0 |
13 | 2024-03-15 12:03:51.39628 | ed1038e799e3b425c4592f27eb33508e |
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)