当在我的本地计算机上通过 C# 代码运行 DocumentDB 查询时,一个简单的 DocumentDB 查询平均需要大约 0.5 秒。另一个例子,获取对文档集合的引用平均大约需要 0.7 秒。这是可以预料的吗?下面是我用于检查集合是否存在的代码,它非常简单 - 但有什么方法可以改善糟糕的性能吗?
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Get the database with the id=FamilyRegistry
var database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();
var stopWatch = new Stopwatch();
stopWatch.Start();
// Get the document collection with the id=FamilyCollection
var documentCollection = client.CreateDocumentCollectionQuery("dbs/"
+ database.Id).Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
var ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
var elapsedTime = String.Format("{0:00} seconds, {1:00} milliseconds",
ts.Seconds,
ts.Milliseconds );
Console.WriteLine("Time taken to get a document collection: " + elapsedTime);
Console.ReadKey();
本地计算机上的平均输出:
Time taken to get a document collection: 0 seconds, 752 milliseconds
在我的另一段代码中,我正在执行 20 个小文档更新,每个更新的 JSON 大小约为 400 字节,但总共仍需要 12 秒。我只是在我的开发环境中运行,但我期待更好的性能。
最佳答案
简而言之,使用 DocumentDB 可以在 ~9 毫秒内端到端完成此操作。我将在下面介绍所需的更改以及它们为何/如何影响结果。
DocumentDB 中的第一个查询总是需要更长的时间,因为它执行一些设置工作(获取 DocumentDB 分区的物理地址)。接下来的几个请求需要更长的时间来预热连接池。后续查询将与您的网络一样快(由于 SSD 存储,DocumentDB 中的读取延迟非常低)。
例如,如果您修改上面的代码来测量,例如 10 个读数,而不是仅第一个读数,如下所示:
using (DocumentClient client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey))
{
long totalRequests = 10;
var database = client.CreateDatabaseQuery().Where(db => db.Id == "FamilyRegistry").AsEnumerable().FirstOrDefault();
Stopwatch watch = new Stopwatch();
for (int i = 0; i < totalRequests; i++)
{
watch.Start();
var documentCollection = client.CreateDocumentCollectionQuery("dbs/"+ database.Id)
.Where(c => c.Id == "FamilyCollection").AsEnumerable().FirstOrDefault();
Console.WriteLine("Finished read {0} in {1}ms ", i, watch.ElapsedMilliseconds);
watch.Reset();
}
}
Console.ReadKey();
我从雷德蒙德的桌面上针对 Azure West US 数据中心运行得到以下结果,即大约50 毫秒。这些数字可能会有所不同,具体取决于网络连接以及客户端与托管 DocumentDB 的 Azure DC 的距离:
Finished read 0 in 217ms
Finished read 1 in 46ms
Finished read 2 in 51ms
Finished read 3 in 47ms
Finished read 4 in 46ms
Finished read 5 in 93ms
Finished read 6 in 48ms
Finished read 7 in 45ms
Finished read 8 in 45ms
Finished read 9 in 51ms
接下来,我从默认网关切换到 Direct/TCP 连接,以将延迟从两跳改进为一跳,即将初始化代码更改为:
using (DocumentClient client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey, new ConnectionPolicy { ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp }))
现在,通过 ID 查找集合的操作将在 23 毫秒内完成:
Finished read 0 in 197ms
Finished read 1 in 117ms
Finished read 2 in 23ms
Finished read 3 in 23ms
Finished read 4 in 25ms
Finished read 5 in 23ms
Finished read 6 in 31ms
Finished read 7 in 23ms
Finished read 8 in 23ms
Finished read 9 in 23ms
当您从也在同一 Azure DC 中运行的 Azure VM 或辅助角色运行相同结果时会怎样?相同的操作大约需要 9 毫秒完成!
Finished read 0 in 140ms
Finished read 1 in 10ms
Finished read 2 in 8ms
Finished read 3 in 9ms
Finished read 4 in 9ms
Finished read 5 in 9ms
Finished read 6 in 9ms
Finished read 7 in 9ms
Finished read 8 in 10ms
Finished read 9 in 8ms
Finished read 9 in 9ms
所以,总结一下:
- 对于性能测量,请允许使用一些测量示例来说明 DocumentDB 客户端的启动/初始化。
- 请使用 TCP/Direct 连接以获得最低延迟。
- 如果可能,请在同一 Azure 区域内运行。
- 如果您按照这些步骤操作,您可以获得出色的性能,并且您将能够通过 DocumentDB 获得最佳性能数据。
关于azure - DocumentDB 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33088927/