Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In ms sql you can do an update query, check the rowcount and if it's 0, then do an insert.

That's how I got an improved query performance for syncing data.



I think you need a unique index if you're doing something like this in MS-SQL, Postgres, or MySQL. Otherwise, the default isolation level won't prevent this data race:

  writer A: begin xact, update, see 0 rows updated
  writer B: begin xact, insert same thing, end xact
  writer A: insert, end xact
One reason I use ON CONFLICT in Postgres is for explicitness. You're specifically telling the DBMS to upsert, and it'll throw an error if you don't have a unique index to support this.


Also mentioned above, ON CONFLICT DO UPDATE would be able to update the concurrent writer's row instead of throwing a uniqueness violation.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: