Recently read an article, said that PostgreSQL also supports NOSQL, defined the exclusive JSONB data type, and faster than MongoDB, and today verified the next, the same data, postgre insertion speed is only MONGO of three points. may be caused by their respective drivers.
data structure: type Stockdaystruct{Date int32 O, H, L, C, V, a Float32}mongo used the MGO package, the insertion code is as follows: Func READHQ (conn net. Conn, C*mgo. Collection, Codestring) { varBUF [ -]byte varx Stockvary kline_day forI: =0; ; i++{_, Err:= Conn. Read (buf[0: -]) ifErr = =io. EOF {fmt. Println ("end of this file transfer") Break } ifErr! =Nil {fmt. PRINTLN (ERR)return} b_buf:= bytes. Newbuffer (buf[0: -]) binary. Read (b_buf, Binary. Littleendian,&X)//binary. Littleendian is the concept of in-memory byte-order, which is to put low-byte into the back. Network transmission is generally used Bigendian, memory byte sequence and CPU, programming to convert. Y.code =Code y.a=x.a y.c=x.c y.date=x.date Y.H=X.H Y.L=X.L y.o=X.O y.v=X.V//FMT. Println (y)Err = C.insert (&y)ifErr! =Nil {panic (err)}}return} #postgresql Table structure CREATE TABLE json_test (ID serial not NULL, data jsonb, CONSTRAINT json_test_pkey PRIMARY KEY (ID) ) used the"GITHUB.COM/LIB/PQ"This package: Insert code: FUNC READHQ (conn net. Conn, DB*sql. DB, Codestring) { varBUF [ -]byte varx Stockday for{_, Err:= Conn. Read (buf[0: -]) ifErr = =io. EOF {fmt. Println ("end of this file transfer") Break } ifErr! =Nil {fmt. PRINTLN (ERR)return} b_buf:= bytes. Newbuffer (buf[0: -]) binary. Read (b_buf, Binary. Littleendian,&X)//binary. Littleendian is the concept of in-memory byte-order, which is to put low-byte into the back. Network transmission is generally used Bigendian, memory byte sequence and CPU, programming to convert. //FMT. Println (y)//err = C.insert (&y)BUF, err: = json. Marshal (&x)//Inserting Datastmt, err: = db. Prepare ("INSERT into Json_test (data) VALUES ($) Returning ID") Checkerr (Err) _, Err= stmt. Exec (string(BUF)) Checkerr (err)}return}
Test of the insertion speed of MongoDB and PostgreSQL under Golang