Wednesday, December 5, 2007

SELECT FOR INSERT

Wouldn't it be nice if we had some like SELECT FOR INSERT in SQL?  I am not even sure if we don't but I have not been able to find anything like that. The problem I am trying to address is the classic problem of two transactions trying to insert a data if it is missing. Lets say T1 reads for record X and if it doesn't find it, X gets inserted. T2 is doing the same. T1 and T2 happily start together so T1 reads and there is no X, same happens to T2. T1 inserts X and commits, T2 inserts and commit and boom, Duplicate data. In the same fashion we have SELECT FOR UPDATE, it would be nice to have SELECT FOR (POSSIBLE) INSERT in SQL. This would mean looks for this data and if you don't find it lock the table because I am going to insert it. It has a bad effect on performance though. That's probably why we don't have it.

No comments: