Nodejs:postgresql随机插入十万条数据测试

前端之家收集整理的这篇文章主要介绍了Nodejs:postgresql随机插入十万条数据测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cpu:cpu family : 6
model : 42

model name : Intel(R) Core(TM) i5-2450M cpu @ 2.50GHz

系统信息:Linux sec 3.5.0-45-generic #68~precise1-Ubuntu SMP Wed Dec 4 16:18:46 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

node postgresql package:https://github.com/brianc/node-postgres

db/index.js部分:


  1. var pg = require('pg');
  2. var conString = "postgres://test:test@localhost/pgs";
  3. exports.pg_exec = function(prepared_obj,cb){
  4. pg.connect(conString,function(err,client,done) {
  5. if(err) {
  6. cb(err,{"rowCount":0})
  7. return;
  8. }
  9. client.query(prepared_obj,result) {
  10. done();
  11. cb(err,result);
  12. });
  13. });
  14. }



index.js部分

  1. /**
  2. * Created by ty4z2008 on 14-1-19.
  3. */
  4. var pg_exec = require("./db").pg_exec;
  5. (function(){
  6. console.log('start pg_insert');
  7. var rand_ship = new Array('韵达','EMS','顺丰','圆通','申通','韵达','UPS','中通','平邮'),rand_alp=new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y','z','2','3','4','5','6','7','8','9'),rand_number=new Array(1,2,3,4,5,6,7,8,9,0),rand_add=new Array('支付宝','网银','货到付款','财付通','微信支付','其他'),rand_gender=new Array('男','女'),rand_state=new Array('交易成功','交易失败','发货中','交易取消','运输中');
  8.  
  9. var rand_ship_length=rand_ship.length,rand_alp_length=rand_alp.length,rand_number_length=rand_number.length,rand_add_length=rand_add.length,rand_state_length=rand_state.length,rand_gender_length=rand_gender.length;
  10. for(var i = 0; i < 50000; i++) {
  11. var n=Math.floor(Math.random()*rand_number_length),a=Math.floor(Math.random()*rand_alp_length),e=Math.floor(Math.random()*rand_add_length),s=Math.floor(Math.random()*rand_ship_length),st=Math.floor(Math.random()*rand_state_length),g=Math.floor(Math.random()*rand_gender_length),price=Math.floor(Math.random()*100);
  12. total_price=i+rand_number[n]+i;
  13. count=rand_number[n]+rand_number[n]+2;
  14. czipcode = i+rand_number[n] + i;
  15. gender=rand_gender[g];
  16. shipping=rand_ship[s];
  17. payment=rand_add[e];
  18. consignee="consignee"+rand_alp[a]+i;
  19. caddr=rand_alp[a]+'caddr'+i;
  20. cphone='1'+i+rand_number[n];
  21. cmessage="cmessage"+rand_alp[a]+i;
  22. ostate=rand_state[st];
  23. pg_exec({
  24. name:"insert",text:" INSERT INTO " +
  25. "orderinfo(total_price,price,count,otime,shipping,payment," +
  26. " consignee,gender,caddr,czipcode,cphone,cmessage,ostate)" +
  27. "VALUES($1,$2,$3,current_timestamp(2),$4,$5,$6,$7,$8,$9,$10,$11,$12)",values:[total_price,consignee,ostate]},function(err){
  28. if(err){
  29. console.log(err);
  30. return;
  31. }
  32. });
  33. };
  34. })();




sql部分:
  1. CREATE TYPE sex_enum AS ENUM('男','女');


  1. -- Table: orderinfo
  2.  
  3. -- DROP TABLE orderinfo;
  4.  
  5. CREATE TABLE orderinfo
  6. (
  7. order_id serial NOT NULL,user_id serial NOT NULL,price character varying(10),-- 单价
  8. count character varying(10),-- 数量
  9. total_price numeric(9,2),-- 总价
  10. otime timestamp without time zone,-- 订单时间
  11. shipping character varying(20),-- 送货方式
  12. payment character varying(20),-- 支付方式
  13. consignee character varying(20),-- 收货人
  14. gender sex_enum,-- 性别-gender
  15. caddr character varying(100),czipcode character varying(10),-- 邮编
  16. cphone character varying(12),-- 联系号码
  17. cmessage character varying(100),ostate character varying(20),-- 状态
  18. CONSTRAINT orderinfo_pkey PRIMARY KEY (order_id),CONSTRAINT orderinfo_user_id_fkey FOREIGN KEY (user_id)
  19. REFERENCES users (user_id) MATCH SIMPLE
  20. ON UPDATE NO ACTION ON DELETE NO ACTION
  21. )
  22. WITH (
  23. OIDS=FALSE
  24. );
  25. ALTER TABLE orderinfo
  26. OWNER TO z71;
  27. COMMENT ON COLUMN orderinfo.price IS '单价';
  28. COMMENT ON COLUMN orderinfo.count IS '数量';
  29. COMMENT ON COLUMN orderinfo.total_price IS ' 总价
  30. ';
  31. COMMENT ON COLUMN orderinfo.otime IS '订单时间';
  32. COMMENT ON COLUMN orderinfo.shipping IS '送货方式
  33. ';
  34. COMMENT ON COLUMN orderinfo.payment IS '支付方式
  35. ';
  36. COMMENT ON COLUMN orderinfo.consignee IS '收货人
  37. ';
  38. COMMENT ON COLUMN orderinfo.gender IS '性别-gender
  39. ';
  40. COMMENT ON COLUMN orderinfo.czipcode IS '邮编';
  41. COMMENT ON COLUMN orderinfo.cphone IS '联系号码';
  42. COMMENT ON COLUMN orderinfo.ostate IS '状态';




完成执行时间大概花了1分钟左右

猜你在找的Postgre SQL相关文章