0

我有一个数据库(RavenDB),它需要能够每 10 秒处理 300 个查询(全文搜索)。为了提高性能,我拆分了数据库,所以我有多个 documentStores 我的代码:

            var watch = Stopwatch.StartNew();
        int taskcnt = 0;
        int sum = 0;


        for (int i = 0; i < 11; i++)
        {
            Parallel.For(0, 7, new Action<int>((x) =>
            {
                for(int docomentStore = 0;docomentStore < 5; docomentStore++)
                {
                    var stopWatch = Stopwatch.StartNew();
                    Task<IList<eBayItem>> task = new Task<IList<eBayItem>>(Database.ExecuteQuery, new Filter()
                    {
                        Store = "test" + docomentStore,
                        MaxPrice = 600,
                        MinPrice = 200,
                        BIN = true,
                        Keywords = new List<string>() { "Canon", "MP", "Black" },
                        ExcludedKeywords = new List<string>() { "G1", "T3" }
                    });
                    task.ContinueWith((list) => {
                        stopWatch.Stop();
                        sum += stopWatch.Elapsed.Milliseconds;
                        taskcnt++;
                        if (taskcnt == 300)
                        {
                            watch.Stop();
                            Console.WriteLine("Average time: " + (sum / (float)300).ToString());
                            Console.WriteLine("Total time: " + watch.Elapsed.ToString() + "ms");

                        }

                    });
                    task.Start();
                }

            }));
            Thread.Sleep(1000);

        }
  • 平均查询时间:514,13 ms
  • 总时间:00:01:29.9108016

我查询 ravenDB 的代码:

        public static IList<eBayItem> ExecuteQuery(object Filter)
    {
        IList<eBayItem> items;
        Filter filter = (Filter)Filter;

        if (int.Parse(filter.Store.ToCharArray().Last().ToString()) > 4)
        {
            Console.WriteLine(filter.Store); return null;
        }
        using (var session = Shards[filter.Store].OpenSession())
        {
            var query = session.Query<eBayItem, eBayItemIndexer>().Where(y => y.Price <= filter.MaxPrice && y.Price >= filter.MinPrice);

            query = filter.Keywords.ToArray()
            .Aggregate(query, (q, term) =>
                q.Search(xx => xx.Title, term, options: SearchOptions.And));
            if (filter.ExcludedKeywords.Count > 0)
            {
                query = filter.ExcludedKeywords.ToArray().Aggregate(query, (q, exterm) =>
                q.Search(it => it.Title, exterm, options: SearchOptions.Not));
            }
            items = query.ToList<eBayItem>();
        }
        return items;
    }

以及 RavenDB 的初始化:

        static Dictionary<string, EmbeddableDocumentStore> Shards = new Dictionary<string, EmbeddableDocumentStore>();
    public static void Connect()
    {
        Shards.Add("test0", new EmbeddableDocumentStore() { DataDirectory = "test.db" });
        Shards.Add("test1", new EmbeddableDocumentStore() { DataDirectory = "test1.db" });
        Shards.Add("test2", new EmbeddableDocumentStore() { DataDirectory = "test2.db" });
        Shards.Add("test3", new EmbeddableDocumentStore() { DataDirectory = "test3.db" });
        Shards.Add("test4", new EmbeddableDocumentStore() { DataDirectory = "test4.db" });
        foreach (string  key in Shards.Keys)
        {
            EmbeddableDocumentStore store = Shards[key];
            store.Initialize();
            IndexCreation.CreateIndexes(typeof(eBayItemIndexer).Assembly, store);
        }
    }

如何优化我的代码,使我的总时间更短?把我的数据库分成 5 个不同的数据库好不好?

编辑:该程序只有 1 个 documentStore 而不是 5 个。(正如Ayende Rahien所建议的)这也是它自己的查询:

Price_Range:[* TO Dx600] AND Price_Range:[Dx200 TO NULL] AND Title:(Canon) AND Title:(MP) AND Title:(Black) -Title:(G1) -Title:(T3)
4

2 回答 2

2

不,这不好。使用单个嵌入式 RavenDB。如果你需要分片,这涉及到多台机器。

通常,每个 RavenDB 查询都在几毫秒内。您需要显示查询的样子(您可以在它们上调用 ToString() 来查看)。

以这种方式拥有 RavenDB 的分片意味着它们都在争夺 CPU 和 IO

于 2013-04-02T04:05:23.627 回答
0

我知道这是一篇旧帖子,但这是我得到的最高搜索结果。

我遇到了同样的问题,我的查询需要 500 毫秒。现在通过应用以下搜索实践需要 100 毫秒:http ://ravendb.net/docs/article-page/2.5/csharp/client-api/querying/static-indexes/searching

于 2015-01-21T11:02:21.913 回答