10 threads operate simultaneously, and the same data is inserted frequently. Although data is inserted using:
Insert inti tablename (fields ....) select @ t1, @ t2, @ t3 from tablename where not exists (select id from tablename where t1 = @ t1, t2 = @ t2, t3 = @ t3)
It was still invalid in the case of high concurrency. This statement is also included in the stored procedure. (Previously, I tried to determine whether there are records and whether to write data. This is invalid ).
Therefore, such situations still need to be addressed from the root of the database, that is, constraints. Otherwise, the database's atomic operations will not be as detailed as needed.
There are not many people using command lines to add constraints. Every time I look for SQL statements on the internet, I am exhausted. I 'd better write them down.
The key is field combination constraint uniqueness.
Alter table tablename add CONSTRAINT NewUniqueName Unique (t1, t2, t3)
This ensures that the three fields are not duplicated.
The adjustment to the database in the production system is really imperative ......
There is no good solution for repeated database read operations, that is, some entries in the database are read at the same time to change a field of these entries to 1, and other processes will not read them again. However, in the case of multithreading, even if I use the latest features of SQL SERVER 2005, it is similar to the update... output into to the temporary table method:
Update tablename set OnCheck = 1, LastLockTime = getdate (), LastChecktime = getdate ()
Output deleted. ID into @ newtb
Where ID in
(Select id from tablename where Oncheck = 0)
It will still cause repeated reads. Is there no better way?
If you have a better method, you can send it out.