当前位置:网站首页>Solve the problem of multi-thread calling sql stored procedure
Solve the problem of multi-thread calling sql stored procedure
2022-08-11 04:26:00 【A rookie is a great god】
Scenario:
Our program is now multi-threaded, and I need to insert the data in the temporary table into TABLE_M, but at this time other threads may be inserting, so I can't use our previous solution (select max(oid) from Tuning.TABLE_M..., then to maintain the relationship between the main and sub-tables, change the self-increment attribute of the OID of TABLE_M, insert, and then add the self-increment attribute...)
I want to do this now:
1,) Insert the temporary data into TABLE_M (OID is self-incrementing) (Insert Into ....select ...), then I get select Scope_Identity() as @MaxTABLE_MOID, and then modify the temporary data according to this @MaxTABLE_MOIDTABLE_MOID in the table.
1.1,) (1,) Will this scheme have the following problems:
A thread, 1-10 total 10 records,
B thread, 1-10 total 10 records
When thread A is inserted,
Thread B is also reinserting
A thread and B thread are interactive insertion, A thread inserts one, B thread inserts one
It works like this:
For example, the OID of TABLE_M at this time is 100
The OID of the first TABLE_M inserted by thread A becomes 101
The OID of the first TABLE_M inserted by thread B becomes 102
When I finished inserting thread A, the OID of the last record inserted by thread A is 120
At this time, my Temp will go to this 120 to modify the word table relationship
TABLE_M (OID=120) is no problem when A is inserted
TABLE_M(OID=119) is inserted by B, but it is associated with the ninth record of the temporary table corresponding to thread A
Sad!!!
1.2,) Are we going to use With(lock) when inserting?
2,) I also have an idea, that is, after the insertion is completed, the generated OIDs of the batch of data inserted into TABLE_M are queried according to the conditions, and then these OIDs are matched with temp (matching in order, because the insertion of temp must haveorder insertion), and then modify the TABLE_MOID in temp, so that the child data can be inserted into TABLE_M_LteCell, TABLE_M_GsmCell, TABLE_M_TdsCell.
DBA provides solutions
We can make a separate table for OID auto-increment maintenance, which can be temporarily called the MAX_OID table.The easiest way is to just record: the current largest OID.
So we follow the following steps when doing multi-threaded operations
1. Get the value in the MAX_OID table, so you can get the self-incrementing starting serial number
2. Get the maximum number of rows in the temporary table at the time of this insertion.Do not use select count(1) from table, use the following statement for maximum performance.
sql statement to directly get the number of table rows:
select sum(row_count)
from sys.dm_db_partition_stats
where index_id<1 and object_id=object_id('table name')
3. Using max_oid + the number of rows in the temporary table, you can get the maximum MAXOID number of the main table in the future after the temporary table is inserted into the main table
4. Update the MAX_OID table.Change the data in the table to: max_oid+the number of rows in the temporary table
Special note: In order to ensure transaction consistency, the above steps need to be completed in one transaction, and the following statements are required
SET XACT_ABORT ON
begin tran
Execute the sql statement (that is, the logical operations of 1-4 above)
commit
Although the above four steps are verbose in text, the actual operation will be very fast.All system tables are used to read, which must be at the millisecond level and will not affect performance.
In this way, each thread reads the value in the MAX_OID table, and there will be no conflict.
Such as the following example:
The current maximum value of MAX_OID is 100.
The temporary table of thread one is 30 rows
The temporary table of thread two is 40 rows
Thread one:
1. Read the MAX_OID table.Get 100
2. Get the number of temporary table rows: 30
3. The predicted maxoid is 100+30=130
4. Change MAX_OID to 130.
Thread two:
1. Read the MAX_OID table.get 130
2. Get the number of temporary table rows: 40
3. The predicted maxoid is 130+40=170
4. Change MAX_OID to 170.
Note: The MAX_OID obtained by thread two must be 130, not 100. Because thread one has updated this value.And the update process is locked.Thread two can read less than 100.
This avoids conflicts.
边栏推荐
- Common layout effect realization scheme
- 监听U盘插入 拔出 消息,获得U盘盘符
- Power Cabinet Data Monitoring RTU
- AVH 动手实践 (二) | 在 Arm 虚拟硬件上部署 PP-OCR 模型
- 一文读懂 高性能可预期数据中心网络
- CTO said that the number of rows in a MySQL table should not exceed 2000w, why?
- [FPGA] day19- binary to decimal (BCD code)
- 0基础转行软件测试,自学3个月,浅拿12k*13薪offer
- LeetCode刷题第11天字符串系列之《 58最后一个单词长度》
- 【FPGA】day20-I2C读写EEPROM
猜你喜欢

The principle, architecture, implementation, practice of "transfer" and "search", no need to be afraid of interviews

【FPGA】名词缩写

What is machine learning?Explain machine learning concepts in detail

"3 Longest Substring Without Repeating Characters" on the 17th day of LeetCode brushing

无线电射频能量的收集

Object Creation and Display Transformation

Interchangeability and Measurement Technology—Surface Roughness Selection and Marking Method

Power Cabinet Data Monitoring RTU

Differences and connections between distributed and clustered

LeetCode刷题第16天之《239滑动窗口最大值》
随机推荐
redis按照正则批量删除key
洛谷P1763 埃及分数
简历里写了会代码,却依然过不了面试这一关
监听U盘插入 拔出 消息,获得U盘盘符
Get the length of the linked list
Harvesting of radio frequency energy
【力扣】22.括号生成
【FPGA】day21- moving average filter
快速使用UE4制作”大场景游戏“
LeetCode Brush Questions Day 11 String Series "58 Last Word Length"
Jetson Orin平台4-16路 GMSL2/GSML1相机采集套件推荐
leetcode刷题第13天二叉树系列之《98 BST及其验证》
Use jackson to parse json data in detail
LeetCode814 Math Question Day 15 Binary Tree Series Value "814 Binary Tree Pruning"
How to learn machine learning?machine learning process
C# 一周入门高级编程之《C#-LINQ》Day Four
es-head plugin insert query and conditional query (5)
[Likou] 22. Bracket generation
这些云自动化测试工具值得拥有
利用Navicat Premium导出数据库表结构信息至Excel