1

I'm having a problem where the below query is taking around 700ms to execute. It's in a loop and gets called 100+ times so it's taking forever.

Model:

public class ReleaseDates
{
    public int Id { get; set; }
    public string MovieName { get; set; }
    public string Country { get; set; }
    public DateTime ReleaseDate { get; set; }
    public string AlternateSource { get; set; }
}

Query:

public async Task<List<ReleaseDates>> GetReleaseDatesAsync(string movieName)
{
    return await Db.ReleaseDates.Where(x => x.MovieName == movieName && string.IsNullOrEmpty(x.AlternateSource)).ToListAsync();
}

Any suggestions how to speed this up?

6
  • Take it out of a loop and change it so there is only one query to get everything? You can keep appending where conditions without enumerating it Commented Jun 25, 2015 at 13:20
  • The table is very large 10,000's of data. So grabbing everything I would have thought to be a bad idea? Commented Jun 25, 2015 at 13:22
  • Maybe you should use a stored procedure to do the join and get the data you need the return instead of looping and passing in movieName in 1 by 1 Commented Jun 25, 2015 at 13:25
  • How about pagination? Commented Jun 25, 2015 at 13:26
  • Use results caching (also known as "second-level caching"), you keep the results of queries in a local cache. When issuing a query, you first see if the results are available locally before you query against the store. Commented Jun 25, 2015 at 13:30

2 Answers 2

3

Get rid of the loop. That's the problem. You're sending a lot of queries to database.

Store all the movie names that you're searching for in a list and do a contains there.

public async Task<List<ReleaseDates>> GetReleaseDatesAsync(List<string> movieNames)
{
    //movie names that you're searching for - movieNames

    return await Db.ReleaseDates.Where(x => movieNames.Contains(x.MovieName) && string.IsNullOrEmpty(x.AlternateSource)).ToListAsync();

}

If you want to get the release dates for "Ant Moon", "GodZippa", "SuperWan", your list will contain these strings and this is it.

Some further reading: http://blogs.msdn.com/b/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx

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

1 Comment

Yeah stupid of me, when trying to work something out I always end up with tunnel vision. Thanks.
2

Create a index on your movie name field. Also, catch the query through profiler and exec it in Sql Management Studio with Show Plan enabled. It will give you some insights to optimize the query performance.

4 Comments

sorry, new to EF, how would i create an index?
Do it directly on Sql Server Management Studio.
I'm using a code first approach, Can it not be done via the model or any EF configuration?
I believe you can create indices if you're using EF 6.1. If you're using a version lower than 6.1, you'll have to do it manually. msdn.microsoft.com/en-us/data/jj591583.aspx#Index

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.