万梅资源网 Design By www.ubjie.com

1.创建常规的企业信息表

create table t_centerprises(
 objectid bigint not null, /*唯一编号(6位行政区号+6位sn)*/
 divid uuid not null, /*行政区唯一代码*/
 name text not null, /*企业名称*/
 address text not null, /*企业地址*/
 post text, /*企业邮编*/
 contacts text, /*联系人*/
 tel text, /*联系电话*/
 fax text, /*传真*/
 describe text, /*企业备注*/ 
 date timestamp default now() not null, /*创建日期*/
 constraint pk_centerprisess_objectid primary key (objectid),
 constraint fk_centerprises_divid foreign key(divid) references ts_divisions(objectid) on delete cascade
);
create index idx_centerprises_divid on t_centerprises(divid);

2.需要使用的函数

/*转换16进制到字符*/
drop function if exists hex_to_string(text);
create or replace function hex_to_string( text) 
 returns text as 
$$
 declare
 result text;
 begin
 execute 'select U&''\' || $1 || '''' INTO result;
 return result;
 end;
$$ language plpgsql;
 
/*随机生成汉字
 汉字范围U+4E00..U+9FA5
*/
drop function if exists gen_random_zh(int,int);
create or replace function gen_random_zh(imin int,imax int) 
 returns text as 
$$
 declare
 vlen integer;
 result text;
 begin
 result := '';
 vlen = floor(random()*(imax-imin)+imin);
 for i in 1..vlen loop
  result := result || hex_to_string(to_hex(floor(random()*(42191-19968)+19968)::integer));
 end loop;
 return result;
 end;
$$ language plpgsql;

3.常规测试数据插入(5000000条)

insert into t_centerprises(objectid,divid,name,address,post,contacts,tel,fax,describe)
  select (vdivid|| lpad(id::text,6,'0'))::bigint as objectid,'110101',
  gen_random_zh(5,25) as name,gen_random_zh(10,50) as address,
  floor(random()*(699999-600000)+600000) as post,gen_random_zh(2,8) as contacts,
  floor(random()*(69999999-60000000)+60000000) as tel,floor(random()*(69999999-60000000)+60000000) as fax,
  gen_random_zh(32,128) as describe
 from generate_series(1,5000000) as id;

在普通pc机上插入,大概完成时间约8小时,过程不可监控,并且cpu/内存占用率高,磁盘基本满负荷动作,读写率基本上都是100%.

4.改进后的方法, 插入(10000000条)

do $$
 declare vStart bigint;
 declare vEnd bigint;
 declare MAXVALE bigint;
 declare INTERVAL bigint;
 declare vprovince integer;
 declare vprefecture integer;
 declare vcounty integer;
 declare vdivid text;
 declare vdividex uuid;
begin
 vprovince := 10;vprefecture := 1;vcounty := 1;
 
 MAXVALE := 1000000;
 INTERVAL := 1000; vStart := 1 ;vEnd := INTERVAL;
 vdivid := (lpad(vprovince::text,2,'0') || lpad(vprefecture::text,2,'0') || lpad(vcounty::text,2,'0'))::text;
 vdividex := (select objectid from ts_divisions where province=vprovince and prefecture=vprefecture and county=vcounty);
 loop 
 insert into t_centerprises(objectid,divid,name,address,post,contacts,tel,fax,describe)
  select (vdivid|| lpad(id::text,6,'0'))::bigint as objectid,vdividex as divid,
  gen_random_zh(5,25) as name,gen_random_zh(10,50) as address,
  floor(random()*(699999-600000)+600000) as post,gen_random_zh(2,8) as contacts,
  floor(random()*(69999999-60000000)+60000000) as tel,floor(random()*(69999999-60000000)+60000000) as fax,
  gen_random_zh(32,128) as describe
 from generate_series(vStart,vEnd) as id;
 
 raise notice '%', vEnd;
 vStart := vEnd + 1; vEnd := vEnd + INTERVAL;
 if( vEnd > MAXVALE ) then
  return;
 elsif(vEnd = MAXVALE) then
  vEnd := vEnd - 1;
 end if;
 end loop;
end$$;

因为运算原因, cpu/内存占用率仍然很高, 硬盘负荷较小,读写率也比较低,大概完成时间约1.5小时.

补充:postgreSQL数据库 向表中快速插入1000000条数据

不用创建函数,直接向表中快速插入1000000条数据

create table tbl_test (id int, info text, c_time timestamp);
insert into tbl_test select generate_series(1,100000),md5(random()::text),clock_timestamp();
select count(id) from tbl_test; --查看个数据条数

使用Postgresql 实现快速插入测试数据

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

万梅资源网 Design By www.ubjie.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
万梅资源网 Design By www.ubjie.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。