Skip to content

How to Suppress SELECT query after INSERT? #4651

Closed

Description

@nrcmkoh

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?

Activity

  1. liaozhongwu commented on Sep 6, 2019

    @liaozhongwu

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

  2. nrcmkoh commented on Sep 6, 2019

    @nrcmkoh
    Author

    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.

  3. nrcmkoh commented on Dec 17, 2019

    @nrcmkoh
    Author

    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.

  4. asecher commented on Jan 19, 2020

    @asecher

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

    entityManager
      .createQueryBuilder()
      .insert()
      .into("user")
      .values(users)
      .updateEntity(false)
      .execute();
  5. nrcmkoh commented on Jan 20, 2020

    @nrcmkoh
    Author

    @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.

  6. hypeofpipe commented on Nov 4, 2020

    @hypeofpipe

    You saved our costs! Great question and great answers!

  7. dangphu2412 commented on Feb 26, 2021

    @dangphu2412

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

  8. coyoteecd commented on Mar 4, 2021

    @coyoteecd
    Contributor

    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.

  9. imnotjames commented on Jul 10, 2021

    @imnotjames
    Contributor

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

  10. FrancoCorleone commented on Aug 5, 2022

    @FrancoCorleone

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions