Nodejs: postgresql 무 작위 로 10 만 개의 데이터 테스트 삽입

5701 단어
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 ~ precise 1 - Ubuntu SMP 12 월 4 일 16: 18: 46 UTC 2013 x8664 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, function(err, 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','Q','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,price,count, shipping, payment,
                    consignee,gender, caddr, czipcode, cphone, cmessage, 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 분 정도 걸 렸 어 요.

좋은 웹페이지 즐겨찾기