How to Suppress SELECT query after INSERT?
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?
I fixed it by using save and { reload: false }, Any idea about insert?
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.
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 You can use updateEntity(false) with an insert query builder like this:
entityManager
.createQueryBuilder()
.insert()
.into("user")
.values(users)
.updateEntity(false)
.execute();
@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.
You saved our costs! Great question and great answers!
You can easily turn it off in .save() method like the code below:
await user.save(user, { reload: false });
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.
This was answered. updateEntity(false) prevents the select.
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!