Mongodb AsQueryable() 性能

Mongodb AsQueryable() Performance

我有这样的代码,我想使用 Linq 查询 MongoDB。

我从 MongoDB 集合中得到一个 AsQueryable。

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

我是这样称呼它的,

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

查询函数。

public IEnumerable<IVideo> Filter1(IEnumerable<IVideo> list)
{
    return 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;
}

我的代码工作正常。我将我的代码托管在 IIS 中,并且有大约 50,000 条记录,查询比示例有点复杂。我的工作进程峰值达到 17%,并且在调用 foreach 时需要几秒钟的时间来执行。对于如此低的日期数量,这是一个荒谬的高。

我有几个问题。

  1. 查询是由 .net 还是 MongoDB 执行的?如果它是由 MongoDB 执行的,为什么我的工作进程会受到这样的打击?
  2. 我可以采取哪些步骤来缩短呈现查询的执行时间并减少服务器负载。

谢谢

您在客户端不小心下载了所有条目

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

IEnumerable 导致可查询执行和 return 结果。将过滤方法更改为接受和 return IQueryable.

编辑:

您发布的代码:

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

不编译。

您的代码应如下所示:

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)
    {
        
    }

}