Can parallel insert statements in sql lead to dead

2019-07-08 08:18发布

I am having a SP that is doing a select union of many tables with no lock. This will do lot of calculations with many groupings and sumation and so will take about 2 min for its run. I need to run this for about 500 different set of data. So It totally takes about 1000 min.

To reduce this I need to run this calculation in parallel. So if i run for 4 set of data in parallel I should be done in about 250 min.

But the problem is that once i am done with this calculations I get a set of data that i need to store into a table which has some primary key constraints on 4 of its columns. So when running in parallel I am expecting some dead lock issues. So I was thinking of writing this data to a temporary table that will have the same columns but with no primary key or any constraints.

So with this I am expecting there should be no dead lock. Please advice in this regards and let me know if my understanding is right. Thank you.

1条回答
Lonely孤独者°
2楼-- · 2019-07-08 08:39

INSERT cannot deadlock against another INSERT on the same table, because two INSERT statement on the same table will always acquire the locks in the same order. That being said, I must comment on your statement:

if i run for 4 set of data in parallel I should be done in about 250 min.

You have no reason to expect this. First and foremost such a claim denotes you ignore Amdahl's law. Second a SQL workload is already parallelism internally, the queries in your procedure use parallel plans whenever possible, see Parallel Query Processing, especially if it contains many 'groupings and summations'.

Ultimately what you're facing is an optimization task. Approach it like any other optimization task, identify the bottlenecks first. Waits and Queues is a must read as you embark this journey.

查看更多
登录 后发表回答