1

I have a JSON file that contains about 20k lines of code that has to be read, sorted and saved into a database. I've written code for it and it works the way it's suppose to but my issue is that it takes about 10 minutes. Therefor I wonder if someone has any ideas what can be done to enhance the performance?

Json:

{
    "Number": 123456,
    "Area": "NE01"
},
{
    "Number": 123457,
    "Area": "NE01"
},

and so forth....

C#:

dynamic json = JsonConvert.DeserializeObject(File.ReadAllText(path, Encoding.UTF8));

foreach (var obj in json)
{
    if (obj.Area == "NE01")
    {
        var o = new object
        {
            Number = obj.Number,
        };
        db.Entity.Add(obj);
        continue;
    }

    if (obj.Area == "NE02")
    {
        var o = new object
        {
            Number = obj.Number,
        };
        db.Entity.Add(obj);
        continue;
    }

    if (obj.Area == "NE03")
    {
        var o= new object
        {
            Number = obj.Number,
        };
        db.Entity.Add(obj);
        continue;
    }

    if ( obj.Area== "NE04")
    {
        var o = new object
        {
            Number = obj.Number
        };
        db.Entity.Add(obj);
        continue;
    }
}

db.SaveChanges();

To make it clearer, area has four different values. Depending on the value the number will have a foreign key that points to the area. Unfortunately I'm not allowed to change anything in the underlying database. Let me know if I have to provide further information.

7
  • 2
    Have you added timings to see where the time is going? there are 4 places to time, as a start: File.ReadAllText, JsonConvert.DeserializeObject, the foreach loop, and finally SaveChanges. Until you know where the time is being spent: you can't start to make it faster. Commented Feb 14, 2019 at 10:05
  • might take a look at the bulk operations - EF do not support it. In case SaveChanges is slow Commented Feb 14, 2019 at 10:06
  • That's not valid Json. Commented Feb 14, 2019 at 10:08
  • @SaniSinghHuttunen I think we can infer that it is an extract from the JSON, presumably from a JSON array; I'm not sure that's something we need to get hung up on... Commented Feb 14, 2019 at 10:09
  • 1
    to echo @Vladimir: if all you're doing here is insert, then SqlBulkCopy may be your best option. There's interesting ways of doing that, but perhaps the most convenient (if you have a List<T> of a T that looks like the table) is to use FastMember.ObjectReader look for bcp here: github.com/mgravell/fast-member Commented Feb 14, 2019 at 10:10

2 Answers 2

1

Using EntityFramework.Utilities you can use Bulk Insert which should speed up insertions.

Something like:

public class Data
{
  public int Number { get; set; }
  public string Area { get; set; }
}

var objects = JsonConvert.DeserializeObject<List<Data>>(File.ReadAllText(path, Encoding.UTF8))
  .Select(d => new object { Number = d.Number })
  .ToList();

EFBatchOperation.For(db, db.Entity).InsertAll(objects);

Disclaimer: Code not tested.

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

Comments

0

Only by using AddRange I was able to decrease the time to just over one minute. Which is good enough for my purpose.

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.