每个会话的最大允许请求异常 - 解决方法

时间:2012-01-11 20:32:57

标签: c# for-loop nosql ravendb

我收到异常“达到每个会话允许的最大请求数(30)”等。 我试图在“for循环”中运行RavenDB查询,我想知道如何绕过这样做。代码如下。

  using (var session = store.OpenSession())
        {          
            var MovieList = session.Query<Movies>()                                    
                                 .ToList();         

            foreach (var movie in MovieList)
            {
               int NumByState = session.Query<Theaters>()
                                .Where(x => x.State == movie.State)
                                .Count();

               string MovieName = movie.MovieName;
             }                     

        }               

查询中的记录是100s,我需要在for循环中运行计数查询,我无法看到在循环外运行查询的另一种方式..因为我需要为每个查询运行此查询列表中的项目。谢谢你的帮助。

3 个答案:

答案 0 :(得分:4)

ZVenue,这是Multi Maps / Reduce索引的完美示例:

public class MoviesWithTheatersCount : AbstractMultiMapIndexCreationTask<MoviesWithTheatersCount.ReduceResult>
{
    public class ReduceResult
    {
        public string State { get; set; }
        public string MovieName { get; set; }
        public int TheaterCount { get; set; }
    }

    public MoviesWithTheatersCount()
    {
        AddMap<Movie>(movies => from movie in movies
                                select new
                                           {
                                               State = movie.State,
                                               MovieName = movie.Name,
                                               TheaterCount = 0
                                           });

        AddMap<Theater>(theaters => from theater in theaters
                                    select new
                                               {
                                                   State = theater.State,
                                                   MovieName = (string)null,
                                                   TheaterCount = 1
                                               });

        Reduce = results => from result in results
                            group result by result.State
                            into g
                            select new
                                       {
                                           State = g.Key,
                                           MovieName = g.Select(x => x.MovieName != null).First(),
                                           TheaterCount = g.Sum(x => x.TheaterCount)
                                       };
    }
}

作为旁注:您不应使用无界结果集进行查询 - 请改用.Take()

答案 1 :(得分:0)

这可以解决您的问题:

using (var session = store.OpenSession())
{          
    var MovieList = session.Query<Movies>()                                    
        .ToList();
    var states = session.Query<Theaters>()
        .ToList()
        .GroupBy(x => x.State)
        .ToDictionary(x => x.Key, x => x.Count());         

    foreach (var movie in MovieList)
    {
        int NumByState = states.ContainsKey(movie.State)
            ? states[movie.State]
            : 0;

        string MovieName = movie.MovieName;
    }
} 

这比数据库查询数百次要快得多 - 特别是如果通过网络访问数据库。

答案 2 :(得分:0)

正如@Tobias建议的更好的方法,但您可以更改最大请求数。这是通过设置MaxNumberOfRequestPerSession属性来完成的。这可以在session.Advanced / MaxNumberOfRequestPerSession中设置。

这里的想法和细节:http://ravendb.net/documentation/safe-by-default