Home > OS >  Mongodb AsQueryable() Performance
Mongodb AsQueryable() Performance

Time:12-23

I have code like this where I want to query to MongoDB using Linq.

I get an AsQueryable from MongoDB collection.

public IEnumerable<IVideo> GetVideos()
{
    var collection = database.GetCollection<IVideo>("Videos");
    return collection.AsQueryable();
}

I call it like so,

var finalList = Filter2(Filter1(GetVideos())).Skip(2).Take(30);
foreach(var v in finalList)
{
    .... 
}

Functions with the queries.

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    var list = list.Where(q=>q.Categorized)
}


public IEnumerable<IVideo> Filter2(IEnumerable<IVideo> list)
{
    var query = from d in list
        where d.File == "string1" || d.File == "string2" 
                select d;
    return query;
}

My code works fine. I have my code hosted in an IIS and have around 50,000 records and the queries are a bit complex than the example. My worker process spikes to 17% and takes a few seconds to execute when the foreach is called. This is a ridiculous high for such a low date amount.

I have a couple of questions.

  1. Is the query being executed by .net or MongoDB? If it is executed by MongoDB why is my worker process taking such a hit?
  2. What are the steps I can take to improve the execution time to render the query and reduce the server load.

Thanks

CodePudding user response:

You're downloading all entries client-side by accident

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    var list = list.Where(q=>q.Categorized)
}

IEnumerable causes the queryable to execute and return results. Change the filter methods to accept and return IQueryable.

EDIT:

The code you posted:

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    var list = list.Where(q=>q.Categorized)
}

Does not compile.

Your code should look like this:

public IQueryable<IVideo> Filter1(IQueryable<IVideo> qVideos)
{
    return qVideos.Where(q => q.Categorized);
}

public IQueryable<IVideo> Filter2(IQueryable<IVideo> qVideos)
{
    return qVideos
        .Where(e => e.File == "string1" || e.File == "string2");
}

public DoSomething()
{

    // This is the query, in debug mode you can inspect the actual query generated under a property called 'DebugView'
    var qVideos = Filter2(Filter1(GetVideos()))
        .Skip(1)
        .Take(30);

    // This runs the actual query and loads the results client side.
    var videos = qVideos.ToList();

    // now iterated

    foreach (var video in videos)
    {
        
    }

}
  • Related