This isn't a question where I'm too lazy to look up the answer myself. Here's a sample script:
Code:
If Exists (Select A_Small_Field From MyTable Where Record_ID = ?)
Update MyTable Set X = ?, Y = ?, Z = ? Where Record_ID = ?
Else
Insert Into MyTable Values ( ?, ?, ?, ?, ? )
But no matter what happens, you run a very similar query twice. If the system isn't hammered, and the record exists, SQL should cache the row when you run the exists subquery. So it should at least update against memory instead of against the disc. Still, it will have to find that row twice.
Is this really the best way? I mean in terms of performance, I don't mind if it means extra code.
I was thinking I could use a unique constraint with ignore_dup_keys set to on, except that I don't need to just verify a record exists, I actually need to change some of its values.
|