gpt4 book ai didi

azure-cosmosdb - CosmosDb 首次连接可能需要很多秒

转载 作者:行者123 更新时间:2023-12-02 16:56:15 24 4
gpt4 key购买 nike

我正在测试 CosmosDb。我发现初始连接通常需要很多秒。我编写了一个小型 .net core 2.2 控制台应用程序来演示该问题。

   static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");
string url = "https://docdb.documents.azure.com:443/";
string key = "myKey";
DocumentClient docClient = new DocumentClient(new Uri(url), key);
Stopwatch sw = new Stopwatch();
while (true)
{
sw.Start();
var res = await docClient.ReadDocumentAsync(UriFactory.CreateDocumentUri("ct", "ops", "xxx"),
new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey("test") });
sw.Stop();
Console.WriteLine($"Query took {sw.ElapsedMilliseconds}ms");
sw.Reset();
await Task.Delay(1000);
}
}

这是我的典型结果:

Hello World!
Query took 48530ms
Query took 36ms
Query took 26ms
Query took 15ms

第一个请求需要 48 秒!我尝试将 docClient 构造修改为:

 DocumentClient docClient = new DocumentClient(new Uri(url), key,new 
ConnectionPolicy {ConnectionMode = ConnectionMode.Direct,ConnectionProtocol
= Protocol.Tcp });

查看一些典型结果是否更好:

 Hello World!
Query took 20536ms
Query took 104ms
Query took 37ms
Query took 71ms
Query took 13ms
Query took 88ms
Query took 14ms

第一次查询还有 20 秒。

我的数据库是

130Mb     
Avg Throughput /s* 3.58 RU/s

我有

 400 RU's 

我能做些什么来减轻第一次连接的延迟吗?

我在 Cosmosdb 上的文档是:

 {
"id": "xxx",
"fid": "test",
"_rid": "VX8TAPKGDqNeWwEAAAAAAA==",
"_self": "dbs/VX8TAA==/colls/VX8TAPKGDqM=/docs/VX8TAPKGDqNeWwEAAAAAAA==/",
"_etag": "\"0000d4a2-0000-1100-0000-5ce801ef0000\"",
"_attachments": "attachments/",
"_ts": 1558708719
}

最佳答案

这是预期的,请参阅 https://learn.microsoft.com/en-us/azure/cosmos-db/performance-tips#networking 中的第 2 点

By default, the first request has a higher latency because it has to fetch the address routing table. To avoid this startup latency on the first request, you should call OpenAsync() once during initialization as follows.

static async System.Threading.Tasks.Task Main(string[] args)
{
Console.WriteLine("Hello World!");
string url = "https://docdb.documents.azure.com:443/";
string key = "myKey";
DocumentClient docClient = new DocumentClient(new Uri(url), key);
await docClient.OpenAsync();
Stopwatch sw = new Stopwatch();
while (true)
{
sw.Start();
var res = await docClient.ReadDocumentAsync(UriFactory.CreateDocumentUri("ct", "ops", "xxx"),
new RequestOptions { PartitionKey = new Microsoft.Azure.Documents.PartitionKey("test") });
sw.Stop();
Console.WriteLine($"Query took {sw.ElapsedMilliseconds}ms");
sw.Reset();
await Task.Delay(1000);
}
}

关于azure-cosmosdb - CosmosDb 首次连接可能需要很多秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56295719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com