DocumentDB 性能问题
DocumentDB performance issues
当 运行 从本地计算机上的 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 个小型文档更新,每个更新约 400 个字节,JSON 大小,总共仍需要 12 秒。我的开发环境只有 运行,但我期待更好的性能。
简而言之,这可以通过 DocumentDB 在 ~9 毫秒 内完成。我将介绍所需的更改,why/how 它们会影响下面的结果。
在 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 数据中心得到以下结果 运行,即大约 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 VM 或辅助角色的相同结果也在同一 Azure DC 中 运行 时怎么样?相同的操作完成大约需要 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 客户端的 startup/initialization。
- 请使用 TCP/Direct 连接以获得最低延迟。
- 如果可能,运行 在同一个 Azure 区域内。
- 如果您遵循这些步骤,您可以获得出色的性能,并且您将能够使用 DocumentDB 获得最佳性能数字。
当 运行 从本地计算机上的 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 个小型文档更新,每个更新约 400 个字节,JSON 大小,总共仍需要 12 秒。我的开发环境只有 运行,但我期待更好的性能。
简而言之,这可以通过 DocumentDB 在 ~9 毫秒 内完成。我将介绍所需的更改,why/how 它们会影响下面的结果。
在 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 数据中心得到以下结果 运行,即大约 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 VM 或辅助角色的相同结果也在同一 Azure DC 中 运行 时怎么样?相同的操作完成大约需要 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 客户端的 startup/initialization。
- 请使用 TCP/Direct 连接以获得最低延迟。
- 如果可能,运行 在同一个 Azure 区域内。
- 如果您遵循这些步骤,您可以获得出色的性能,并且您将能够使用 DocumentDB 获得最佳性能数字。