【问题标题】:Azure Cosmos Db, select after row?Azure Cosmos Db,在行后选择?
【发布时间】:2018-12-17 15:49:52
【问题描述】:

我正在尝试在 x 行之后选择一些行,例如:

SELECT * from collection WHERE ROWNUM >= 235 and ROWNUM <= 250

不幸的是,看起来 ROWNUM 没有在 azure cosmos db 中解析。

还有其他方法可以做到这一点吗?我已经研究过使用延续令牌,但如果用户跳到第 50 页,它没有帮助,我是否需要继续使用延续令牌进行查询才能到达第 50 页?

我尝试过使用页面大小选项,但它在一次可以返回多少内容方面存在一些限制。

【问题讨论】:

  • 天蓝色 cosmos db 中似乎没有解析 ROWNUM 是什么意思?有什么错误吗?还有更多细节吗?
  • 我在进行 SQL 查询时收到一条错误提示 rownum 不存在
  • 一般情况下,延续令牌与 maxItemCount 参数结合使用。您的意思是您的延续令牌在 50 页后不起作用?你的页面大小是多少?你有多少项目?
  • 例如我在 Azure 中有 1,000,000 条记录。我想查询第 500,000 到 500,010 行。我不能 SELECT * from collection WHERE ROWNUM >= 500,000 和 ROWNUM
  • 嗨,有进展吗?我的回答对你有帮助吗?

标签: azure azure-cosmosdb


【解决方案1】:

例如,我在 Azure 中有 1,000,000 条记录。我想查询行 500,000 到 500,010。我不能 SELECT * from collection WHERE ROWNUM >= 500,000 和 ROWNUM

如果您没有任何过滤器,则到目前为止,您无法通过查询 sql 在 cosmos db 中直接检索特定范围内的项目。因此,您需要使用分页来定位您想要的项目。据我所知,目前仅基于continuation token 支持pagination

请参考以下功能:

using JayGongDocumentDB.pojo;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JayGongDocumentDB.module
{
    class QuerySample1
    {
        public static async void QueryPageByPage()
        {
            // Number of documents per page
            const int PAGE_SIZE = 2;

            int currentPageNumber = 1;
            int documentNumber = 1;

            // Continuation token for subsequent queries (NULL for the very first request/page)
            string continuationToken = null;

            do
            {
                Console.WriteLine($"----- PAGE {currentPageNumber} -----");

                // Loads ALL documents for the current page
                KeyValuePair<string, IEnumerable<Student>> currentPage = await QueryDocumentsByPage(currentPageNumber, PAGE_SIZE, continuationToken);

                foreach (Student student in currentPage.Value)
                {
                    Console.WriteLine($"[{documentNumber}] {student.Name}");
                    documentNumber++;
                }

                // Ensure the continuation token is kept for the next page query execution
                continuationToken = currentPage.Key;
                currentPageNumber++;
            } while (continuationToken != null);

            Console.WriteLine("\n--- END: Finished Querying ALL Dcuments ---");
        }


        public static async Task<KeyValuePair<string, IEnumerable<Student>>> QueryDocumentsByPage(int pageNumber, int pageSize, string continuationToken)
        {
            DocumentClient documentClient = new DocumentClient(new Uri("https://***.documents.azure.com:443/"), "***");

            var feedOptions = new FeedOptions
            {
                MaxItemCount = pageSize,
                EnableCrossPartitionQuery = true,

                // IMPORTANT: Set the continuation token (NULL for the first ever request/page)
                RequestContinuation = continuationToken
            };

            IQueryable<Student> filter = documentClient.CreateDocumentQuery<Student>("dbs/db/colls/item", feedOptions);
            IDocumentQuery<Student> query = filter.AsDocumentQuery();

            FeedResponse<Student> feedRespose = await query.ExecuteNextAsync<Student>();

            List<Student> documents = new List<Student>();
            foreach (Student t in feedRespose)
            {
                documents.Add(t);
            }

            // IMPORTANT: Ensure the continuation token is kept for the next requests
            return new KeyValuePair<string, IEnumerable<Student>>(feedRespose.ResponseContinuation, documents);
        }
    }
}

输出:

希望对你有所帮助。


更新答案:

到目前为止,cosmos db 中还没有像 ROW_NUMBER() [How do I use ROW_NUMBER()? ] 这样的功能。我也想过skip和top。但是,支持top并且还skip(feedback)。似乎skip已经在处理中,将来会发布。

我认为你可以推送与分页功能相关的反馈。或者暂时将continuation token以上作为解决方法。

【讨论】:

  • 嗯,这仍然意味着我必须浏览所有页面才能到达我想查看的页面。有没有办法可以使用过滤器跳到该页面?像'select * where c.tags.mytag = true' 或者如果我按索引值排序然后跳转到 500,000 到 500,010?不断地向数据库询问条目以达到我想要的记录似乎效率低下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多