1

I have an object like the below but with a huge amount of data, we observed that it takes a long to be inserted into our SQL database as we were using normal foreach, the main idea is to insert each Department and get the generated identity number, then insert the nested employees assigned with that department ID.

<Sample>
    <Departments>
        <Department>
            <Name>HR</Name>
            <Employees>
                <Employee>
                    <Name>Marco</Name>
                </Employee>
                <Employee>
                    <Name>John</Name>
                </Employee>
                <Employee>
                    <Name>Sarah</Name>
                </Employee>
            </Employees>
        </Department>
        <Department>
            <Name>IT</Name>
            <Employees>
                <Employee>
                    <Name>Ali</Name>
                </Employee>
                <Employee>
                    <Name>Roberto</Name>
                </Employee>
                <Employee>
                    <Name>Franco</Name>
                </Employee>
            </Employees>
        </Department>
    </Departments>
</Sample>

We tried to use Parallel.ForEach to enhance the performance, and it does, but we got another issue with @@IDENTITY because there is an overlap between the running tasks in Parallel.ForEach as we found an employee is assigned to another department.

The need ... I need to speed the process up either I use Parallel.ForEach or foreach ... any ideas?

BTW ... we are calling a stored procedures which contains normal INSERT INTO command using classing ADO.NET

14
  • 4
    Parallel executions of SQL need their own connection and transaction. If you need to contain everything in one transaction you might look into TransactionScope as well. Commented Jun 22, 2018 at 21:14
  • 4
    Don't use @@IDENTITY. 99% of the time you wanted SCOPE_IDENTITY learn.microsoft.com/en-us/sql/t-sql/functions/… Commented Jun 22, 2018 at 21:16
  • 2
    And parallel foreach might not help you if your bottleneck is on the DB side. Instead it might even generate more overhead and slows you down. Commented Jun 22, 2018 at 21:37
  • 1
    @paparazzo, it's about the idea, not the code ... I pointed to simple code scenarios, I am just thinking about the idea and the approach which I should go through Commented Jun 22, 2018 at 21:49
  • 1
    Use SCOPE_IDENTITY(), if that doesn't solve your problem you will need to rewrite your stored procedure as then it must be inserting into multiple tables. But as others have said, you really should post some code. The idea is sound, the implementation might be flawed but we have no way of knowing this, so we're left with guessing. Commented Jun 23, 2018 at 13:53

1 Answer 1

1

You could use guid as primary key into your table. It would help you to avoid problem with @@IDENTITY. At first you should generate new guid-identity, thank insert the generated value into a row

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.