扫描存储桶中的某些文档时出现 Couchbase 性能问题 - 获取超时异常

Couchbase performance issues when scanning bucket for certain documents - Getting timeout exception

我们有 Couchbase 服务器版本 Community Edition 5.1.1 build 5723

在我们的 Cars 桶中,我们有 Car Make 和它制造的 Cars

两者之间的联系就是我们在Car文档中另存为Car MakeId(就像MySQL中的外键table).

该存储桶只有 330,000 个文档。

查询花费大量时间 - 非常简单的查询需要几十秒 例如

select * from cars where model="Camry"  <-- we expect to have about 50,000 results for that

我们以两种方式执行查询:

  1. 沙发底座UI
  2. 一个 Spring 启动应用程序,在 7.5 秒后不断出现 TimeOutException

我们认为问题是存储桶缺少索引。

所以我们添加了一个索引:

CREATE INDEX cars_idx ON cars(makeName, modelName, makeId, _class) USING GSI;

我们可以在 运行

时看到该索引
SELECT * FROM system:indexes

我们在这里缺少什么? NoSQL 数据库中此类查询的这些次数是否合理?

尝试

CREATE INDEX model_idx ON cars(model);

您的索引未涵盖模型字段。

你应该有 spring 数据库“_class”的索引 属性

CREATE INDEX `type_idx` ON `cars`(`_class`)

所以,这就是我们解决问题的方法:

  1. 使用 this link 和@paralen 的回答,我们创建了几个索引来加速查询。
  2. 当我们知道返回的结果集会很大时,我们修改了代码以使用分页,并想出了这样的事情:
    do{
       Pageable pageable = PageRequest.of(pageNumber, SLICE_SIZE, Sort.by("id"));
       Slice slice carsRepository.findAllByModelName("Camry", pageable);
       List cars = slice.getContent();
    } while (slice.hasNext());