Azure DocumentDB - 可以通过自定义索引改进插入请求费用吗?

时间:2016-04-02 21:37:43

标签: azure azure-cosmosdb

我正在将具有以下结构的文档插入/插入到DocumentDB中:

enter image description here

使用默认索引时,保留此格式文档的请求费用为10.67 RU。这似乎比我预期的要高,所以我希望优化。

阅读本文中的性能提示后:

https://azure.microsoft.com/en-us/blog/performance-tips-for-azure-documentdb-part-2/

我将Collection Indexing Policy的索引模式更改为Lazy。我预计请求费用会明显减少,但只会降至9.9 RU。

然后我再次修改了索引策略,将一堆属性添加到Excluded Paths。这是自定义索引策略:

enter image description here

添加排除路径对upup的RU没有影响 - 它保持在9.9。

我做错了吗?是否有可能使此文档结构的upserts消耗更少的RUs?

修改

这是一个帮助器类,用于设置和缓存与DocumentDB的连接:

public class Documents
{
    public static Documents Instance = new Documents();

    public IReliableReadWriteDocumentClient Client { get; private set; }

    private Documents()
    {
        var endpointUrl = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.EndpointUrl"];
        var authKey = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.AuthorizationKey"];
        var min = TimeSpan.FromMilliseconds(1000);
        var max = TimeSpan.FromMilliseconds(5000);
        var delta = TimeSpan.FromMilliseconds(1000);
        var connectionPolicy = new ConnectionPolicy()
        {
            ConnectionMode = ConnectionMode.Direct,
            ConnectionProtocol = Protocol.Tcp
        };
        var client = new DocumentClient(new Uri(endpointUrl), authKey, connectionPolicy).AsReliable(new ExponentialBackoff(3, min, max, delta));
        Task result = client.OpenAsync();
        result.Wait();
        Client = client;
    }
}

以下是我的Web API类中的Put处理程序的代码:

public class InstallationController : ApiController
{
    private IReliableReadWriteDocumentClient docdb;
    private string docdbUri;

    public InstallationController()
    {
        docdb = Documents.Instance.Client;
        var databaseId = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.DatabaseId"];
        var collectionName = ConfigurationManager.AppSettings["Microsoft.Azure.DocumentDb.CollectionName"];
        docdbUri = "dbs/" + databaseId + "/colls/" + collectionName;
    }

    // PUT api/installation/<installationId>
    // This creates or updates an installation
    public async Task<IHttpActionResult> Put(string id, DeviceInstallation deviceUpdate)
    {
        string message;
        var telemetryClient = new TelemetryClient();

        if (id != deviceUpdate.Id)
        {
            return BadRequest();
        }

        // Code to check for existing record with same APNS Token omitted for clarity

        var upsertResponse = await docdb.UpsertDocumentAsync(docdbUri, deviceUpdate);
        requestCharge = upsertResponse.RequestCharge;
        message = string.Format("Request charge for installation upsert: {0}", requestCharge);
        telemetryClient.TrackTrace(message);

        // Code to save installation to notification hub omitted for clarity

        return Ok();
    }
}

1 个答案:

答案 0 :(得分:1)

由于IO和复制的成本,更换1kB以下的文档会消耗~10 RU而不进行任何索引。 IndexDB属性在DocumentDB中是轻量级的(每个属性/术语只有RU的一小部分),因此从Consistent到Lazy的转换在这种情况下不会产生很大的不同。如果您的文档中包含数千个属性,那将会有所不同。

希望这有帮助。