在RavenDB中查询时间太长

时间:2018-12-07 13:29:05

标签: c# testing time ravendb

我有一个在query中测试RavenDB时间的示例。因此,我使用LinqqueryStopWatch来获取时间。

但是,当我运行代码时,计时似乎太长了(它是2401 ms),因此我尝试通过Ranven Studio进行查询,结果是,计时仅花费了1 ms。我不知道为什么它会有更多不同。

Ps:我的数据库有20万个文档,我当然有indexes

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Raven.Client.Documents;
using Raven.Client.Documents.BulkInsert;

namespace ConsoleApplication6
{
    public class Customer
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    public class SupportCall
    {
        public string Id { get; set; }
        public int Cost { get; set; }
        public string CustomerId { get; set; }
        public DateTime StartDay { get; set; }
        public DateTime EndDay { get; set; }
        public string Issue { get; set; }
    }

    internal class Program
    {
        private static char[] _buffer = new char[6];
        private static string RandomName(Random rand)
        {
            _buffer[0] = (char)rand.Next(65, 91);
            for (int i = 1; i < 6; i++)
            {
                _buffer[i] = (char)rand.Next(97, 123);
            }
            return new string(_buffer);
        }
        static void Main(string[] args)
        {
            using (var store = new DocumentStore
            {
                Urls = new[] { "http://localhost:8080" },
                Database = "Test"
            }.Initialize())
            { 
                using (var session = store.OpenSession())
                {
                    var sp = Stopwatch.StartNew();
                    SupportCall supportCall = session.Query<SupportCall>()
                        .Include<SupportCall>(s => s.CustomerId)
                        .Where(s => s.Cost == 21821).FirstOrDefault();
                    Customer customer = session.Load<Customer>(supportCall.CustomerId);
                    sp.Stop();
                    Console.WriteLine(sp.ElapsedMilliseconds);
                }
                Console.ReadKey();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您能否进行相同的查询(实际上,最好使用不同的参数,以避免缓存成本)并再次检查?

花费这么长时间的最常见原因是您要为首次连接和建立文档存储设置付费。 这里的奇怪之处在于,您是在本地主机上执行此操作的,因此,即使在首次调用时,我也希望这样做非常快。

您可以使用Fiddler(将URL更改为public function authenticated($request , $user){ if($user->rol=='Administrador'){ return redirect()->route('home') ; }elseif($user->rol=='Docente'){ return redirect()->route('balance') ; }else { return redirect()->route('profile'); } } ,以便捕获它)来查看网络的成本。

我看到这样的事情是由于防病毒和数据包检查工具而发生的。