繁体   English   中英

如何针对此查询优化 Azure Cosmos 索引

[英]How to optimize the Azure Cosmos index for this query

我有一个大约有一个 Cosmos DB。 用于存储分析数据的 10GB 数据。

model如下:

{
    "publisherID": "",
    "managerID": "",
    "mediaID": "",
    "type": "",
    "ip": "",
    "userAgent": "",
    "playerRes": "",
    "title": "",
    "playerName": "",
    "videoTimeCode": 0,
    "geo": {
        "country": "",
        "region": "",
        "city": "",
        "ll": []
    },
    "date": "",
    "uuid": "",
    "id": ""
}

我有时会遇到非常繁重的查询,因为达到了我的 RU 限制而受到限制。 在考虑增加我的 RU 限制之前,我想确保我的查询得到优化。

我所有的查询都遵循以下模式:

SELECT c.id,c.date,c.uuid,c.type FROM c WHERE c.mediaID = "{ID}" AND (c.type = "Load OR c.type = "Progress" OR c.type = "Play") AND (c.date BETWEEN "2021-06-30T22:00:00.000Z" AND "2021-07-31T21:59:59.999Z")

所以在做了一些研究之后,我得出的结论是,我能拥有的最佳指标是:

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/type/?"
        },
        {
            "path": "/mediaID/?"
        },
        {
            "path": "/date/?"
        }
    ],
    "excludedPaths": [
        {
            "path": "/*"
        }
    ]
}

我得到了这个查询的以下统计信息:

Request Charge: 324.51000000000005 RUs
Showing Results: 1 - 100
Retrieved document count: 10024
Retrieved document size: 8597509 bytes
Output document count: 200
Output document size: 28324 bytes
Index hit document count: 199.24
Index lookup time: 2.41 ms
Document load time: 62.93 ms
Query engine execution time: 15.709800000000001 ms
System function execution time: 0 ms
User defined function execution time: 0 ms
Document write time: 0.47000000000000003 ms
Round Trips: 1

让我担心的是 Retrieved 文档计数和 Output 文档计数之间的差异。 我猜这就是为什么我需要 324 RU 才能获得前 100 个结果...

我不确定如何设置索引以优化查询性能(总是相同的模式:WHERE mediaID = {ID} AND type = {TYPE} AND date Between 2 dates)

欢迎任何帮助。

感谢您的反馈,帮助很大!

我添加了如下复合索引:

    "compositeIndexes": [
        [
            {
                "path": "/mediaID",
                "order": "ascending"
            },
            {
                "path": "/type",
                "order": "ascending"
            },
            {
                "path": "/date",
                "order": "ascending"
            }
        ]
    ]

我第一篇文章中初始请求的 RU 现在是 7 RU(之前是 320。)。 谢谢@404

如果你不介意,我想更进一步...... :)

对于另一个请求(相同结构),我知道有很多数据需要检索,我需要 42.02 RU 才能获得前 100 个结果。 是否有意义?

Request Charg 42.02 RUs
Showing Results 1 - 100
Retrieved document count 200
Retrieved document size 164847 bytes
Output document count 200
Output document size 28544 bytes
Index hit document count 200
Index lookup time 6.98 ms
Document load time 1.3399 ms
Query engine execution time 0.6601 ms
System function execution time 0 ms
User defined function execution time 0 ms
Document write time 0.47000000000000003 ms
Round Trips 1

除了增加 RU 限制之外,是否可以在此处进行更多优化?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM