Avoid field combination constraints when writing duplicate values in High-concurrency Databases

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.