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部分:


var pg = require('pg');
var conString = "postgres://test:test@localhost/pgs";
exports.pg_exec = function(prepared_obj,cb){
    pg.connect(conString,function(err,client,done) {
        if(err) {
            cb(err,{"rowCount":0})
            return;
        }
        client.query(prepared_obj,result) {
            done();
            cb(err,result);
        });
    });
}



index.js部分

/**
 * Created by ty4z2008 on 14-1-19.
 */
var pg_exec = require("./db").pg_exec;
(function(){
    console.log('start pg_insert');
    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('交易成功','交易失败','发货中','交易取消','运输中');

        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;
    for(var i = 0; i < 50000; i++) {
        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);
        total_price=i+rand_number[n]+i;
        count=rand_number[n]+rand_number[n]+2;
        czipcode = i+rand_number[n] + i;
        gender=rand_gender[g];
        shipping=rand_ship[s];
        payment=rand_add[e];
        consignee="consignee"+rand_alp[a]+i;
        caddr=rand_alp[a]+'caddr'+i;
        cphone='1'+i+rand_number[n];
        cmessage="cmessage"+rand_alp[a]+i;
        ostate=rand_state[st];
        pg_exec({
                name:"insert",text:" INSERT INTO " +
                    "orderinfo(total_price,price,count,otime,shipping,payment," +
                    " consignee,gender,caddr,czipcode,cphone,cmessage,ostate)" +
                    "VALUES($1,$2,$3,current_timestamp(2),$4,$5,$6,$7,$8,$9,$10,$11,$12)",values:[total_price,consignee,ostate]},function(err){
                if(err){
                    console.log(err);
                    return;
                }
        });
    };
})();




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


-- Table: orderinfo

-- DROP TABLE orderinfo;

CREATE TABLE orderinfo
(
  order_id serial NOT NULL,user_id serial NOT NULL,price character varying(10),-- 单价
  count character varying(10),-- 数量
  total_price numeric(9,2),-- 总价
  otime timestamp without time zone,-- 订单时间
  shipping character varying(20),-- 送货方式
  payment character varying(20),-- 支付方式
  consignee character varying(20),-- 收货人
  gender sex_enum,-- 性别-gender
  caddr character varying(100),czipcode character varying(10),-- 邮编
  cphone character varying(12),-- 联系号码
  cmessage character varying(100),ostate character varying(20),-- 状态
  CONSTRAINT orderinfo_pkey PRIMARY KEY (order_id),CONSTRAINT orderinfo_user_id_fkey FOREIGN KEY (user_id)
      REFERENCES users (user_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE orderinfo
  OWNER TO z71;
COMMENT ON COLUMN orderinfo.price IS '单价';
COMMENT ON COLUMN orderinfo.count IS '数量';
COMMENT ON COLUMN orderinfo.total_price IS ' 总价
';
COMMENT ON COLUMN orderinfo.otime IS '订单时间';
COMMENT ON COLUMN orderinfo.shipping IS '送货方式
';
COMMENT ON COLUMN orderinfo.payment IS '支付方式
';
COMMENT ON COLUMN orderinfo.consignee IS '收货人
';
COMMENT ON COLUMN orderinfo.gender IS '性别-gender
';
COMMENT ON COLUMN orderinfo.czipcode IS '邮编';
COMMENT ON COLUMN orderinfo.cphone IS '联系号码';
COMMENT ON COLUMN orderinfo.ostate IS '状态';




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

原文链接:https://www.f2er.com/postgresql/195900.html

猜你在找的Postgre SQL相关文章