There is a task: to insert N elements into the table, but before that make sure that are not added if these items.
I.e. first do something like:
SELECT COUNT(*) FROM xxx WHERE x IN (x1,x2,x3,x4,x5,x6...... x1000);
If the result is 0, it means that you can do the same bulk INSERT statement.
But the problem is how to do it secure with multithreading?
That is, for example, how to avoid the situation that obtained at the same time the thread 2 and the procedure is the same:
P1: SELECT COUNT(*) — receives a "0"
P2: SELECT COUNT(*) — receives a "0"
P1: does the INSERT
P2: because I get "zero" in the previous select, INSERT, too, making duplicate records
Is there a solution to this problem?
The idea is to set some global flag seems to be very crooked and stupid.
Hranici do not offer, because again- they will not save you from the simultaneity. Generally such things are done?
Sometimes it is assumed to insert large amounts of data on tens of thousands, so it is likely that queries will run too quickly and is likely to catch a bug with simultaneous insert.