Typeorm: Why .save() gives better perf compare to QueryBuilder .insert() - bulk insertion

Created on 4 Apr 2018  路  1Comment  路  Source: typeorm/typeorm

Issue type:

[?] documentation issue

Database system/driver:

[*] postgres

TypeORM version:

[*] latest
Node version: v9.7.1
TypeORM version: 0.1.20

Code to reproduce the context of the question :

import "reflect-metadata";
import {createConnection} from "typeorm";
import { User } from "./entity/User";

createConnection().then(async connection => {

  const users: User[] = [];

  for (let i = 1; i <= 1000; i++) {
    const user = new User();
    user.firstName = `Jean`;
    user.lastName = `Perf`;
    user.age = 1;
    users.push(user);
  }

  console.time("queryCreation");
  const query = connection.createQueryBuilder()
    .insert()
    .into(User)
    .values(users);
  console.timeEnd("queryCreation");

  console.time("queryExecInsert");
  query.execute();
  console.timeEnd("queryExecInsert");

  console.time("queryExecSave");
  connection.manager.save(users)
  console.timeEnd("queryExecSave");

}).catch(error => console.log(error));

queryCreation: 1.153ms
queryExecInsert: 2152.322ms
queryExecSave: 0.357ms

queryCreation: 1.371ms
queryExecInsert: 2325.678ms
queryExecSave: 0.450ms

queryCreation: 0.999ms
queryExecInsert: 1989.534ms
queryExecSave: 0.315ms
#
It gets similar result as https://github.com/typeorm/typeorm/blob/master/test/benchmark/bulk-save/bulk-save.ts.
From the doc (http://typeorm.io/#/insert-query-builder) it says that QueryBuilder insert "is the most efficient way in terms of performance to insert rows into your database".
But given result above .save seems to be a lot quicker, so I don't understand. Any guess ?

Most helpful comment

Try it on @next. Save mechanizm was completely refactored and now save is ultra fast and insert is super-ultra fast and a bit faster then save. npm i typeorm@next.

>All comments

Try it on @next. Save mechanizm was completely refactored and now save is ultra fast and insert is super-ultra fast and a bit faster then save. npm i typeorm@next.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leixu2txtek picture leixu2txtek  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

laukaichung picture laukaichung  路  3Comments

guscastro picture guscastro  路  3Comments

Diluka picture Diluka  路  3Comments