typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

How to Suppress SELECT query after INSERT?

Open nrcmkoh opened this issue 7 years ago • 10 comments

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [x] mssql [ ] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.9 (or put your version here)

I am executing the following line of code in a transaction using entity manager

await entityManager.insert(
                      CriteriaClause,
                      {
                        customerId: clause.customerId,
                        criteriaStatementId: clause.criteriaStatementId,
                        criteriaPhraseNum: clause.criteriaPhraseNum,
                        fieldName: clause.fieldName,
                        lowValue: clause.lowValue,
                        highValue: clause.highValue,
                        criteriaOperatorId: clause.criteriaOperatorId,
                        criteriaTableId: clause.criteriaTableId,
                        modifiedBy: clause.modifiedBy
                      },
                      { reload: false }
                    );

When this code executes, it's extremely slow because of the select query after the insert. My understanding is that setting the reload option to false should suppress the query. If this is not the way, how would I do that?

nrcmkoh avatar Aug 28 '19 05:08 nrcmkoh

I fixed it by using save and { reload: false }, Any idea about insert?

liaozhongwu avatar Sep 06 '19 12:09 liaozhongwu

It sounds like this option only works for save, which is unfortunate because save was not cascading the relationships for me. I switched over to explicitly inserting and updating all the entities in my entity graph.

One detail I forgot to mention earlier is that the query after the insert was slow due to setting the orderBy option on the entity. I resolved the issue by removing the option and performing the order by where I needed it.

nrcmkoh avatar Sep 06 '19 13:09 nrcmkoh

Any plans to implement fix resolve this? I'm hitting up against this again, and I don't want to have to write raw SQL every time I need fast inserts. I feel like the reload negates the performance benefits of using insert, especially since it's creating a select statement for each record.

nrcmkoh avatar Dec 17 '19 17:12 nrcmkoh

@nrcmkoh You can use updateEntity(false) with an insert query builder like this:

entityManager
  .createQueryBuilder()
  .insert()
  .into("user")
  .values(users)
  .updateEntity(false)
  .execute();

asecher avatar Jan 19 '20 10:01 asecher

@nrcmkoh You can use updateEntity(false) with an insert query builder like this:

entityManager
  .createQueryBuilder()
  .insert()
  .into("user")
  .values(users)
  .updateEntity(false)
  .execute();

I've already converted the code, and I don't want to change it now, but thank you for showing me this! I'll use it the next time I need to prevent reload.

nrcmkoh avatar Jan 20 '20 17:01 nrcmkoh

You saved our costs! Great question and great answers!

hypeofpipe avatar Nov 04 '20 11:11 hypeofpipe

You can easily turn it off in .save() method like the code below: await user.save(user, { reload: false });

dangphu2412 avatar Feb 26 '21 12:02 dangphu2412

For whoever ends up here, keep in mind that using updateEntity(false) causes your inserted identities not to be copied back into the object being inserted, and neither in the InsertResult.identifiers array. The workaround is to manually copy them from the raw result, which is a bit meh, especially for bulk inserts.

Relates to #5348, I added a proposal to address this here.

coyoteecd avatar Mar 04 '21 15:03 coyoteecd

This was answered. updateEntity(false) prevents the select.

imnotjames avatar Jul 10 '21 05:07 imnotjames

Th provided workaround doesn't work. It should be possible to do reload: false on insert operation as well. Save operation updates the row if entity already exist. It definitely should be possible to do well performing insert without selecting first. We should be fine with simply catching a duplicate error and resolve it!

FrancoCorleone avatar Aug 05 '22 20:08 FrancoCorleone