I am starting a transaction and I need to check that a record exists, I don't need any values, but I need it to exist for the duration of the transaction for everything to remain valid.
It's VERY unlikely of course that someone will delete at more or less the same time someone else tries to do what this transaction happens, but as with all things concurrent 'very unlikely' isn't good enough.
I don't want to experiment either - I want to know for certain it'll work. Will something like:
SELECT EXISTS(SELECT * FROM tbl1 WHERE ... LOCK IN SHARE MODE);
Work as I expect it to?
For
I would imagine that LOCK IN SHARE MODE
is simply implemented as a flag at the parsing stage, telling MySQL to acquire certain locks, thus this isn't a special case.
Against
MySQL treats EXISTS
differently to a normal SELECT
for obvious reasons. As per it's query optimiser it often reads constants before it starts running the query. An over-eager optimisation could defeat this.
I'm leaning towards 'for' but I want to be certain.
SELECT ... FOR UPDATE
.