Mongo查询时间在多个环境中发行(Spring Boot)

发布于 2025-02-01 14:52:41 字数 1806 浏览 1 评论 0原文

我从事一个具有两个环境的项目,A& B,每个都有自己的数据库。我的Spring Boot(1.5.3)应用程序使用MongoDB,其中我正在根据枚举enumtype type> type1 和type2 type2 。

现在,在Env。 A,当尝试获取enumType = type1的所有条目时,我可以在数据库中看到的所有条目约为2000,这需要很长时间。获取条目,其中enumType = type2需要正常的时间。

在Env。 b,当获取type1条目时(约2m)时,这几乎不需要发生。获取type2条目(450个条目)需要荒谬的时间。

在两种情况下,我都添加了一个type_1索引(通过enumtype上升),但是仍然存在这些奇怪的时间,以至于我尝试获取type2 Env B中的条目,我的应用程序崩溃。由于搜索逻辑在两个环境中都是相同的(在两种情况下都完全相同的应用程序运行),因此我不确定要解决此问题的其他操作。

代码很简单:

EntityController:

@GetMapping
public Page<Entity> getAll(@RequestParam EnumType type, OffsetPageRequest page) {
    return entityService.getAll(type, page);
}

EntityService:

public Page<Entity> getAll(EnumType type, OffsetPageRequest page) {
    return entityRepository.findByCriteria(type, page);
}

customizedentityRepository:

Page<Entity> findByCriteria(EnumType type, Pageable page);

userProfilerePositoryImpl:

@Override
public Page<Entity> findByCriteria(EnumType type, Pageable page) {
    Query query = new Query().with(page);
    List<Criteria> andCriteriaList = new ArrayList<>();
    andCriteriaList.add(Criteria.where("type").is(type));
    query.addCriteria(new Criteria().andOperator(andCriteriaList.toArray(new Criteria[0])));
    return PageableExecutionUtils.getPage(mongoTemplate.find(query, Entity.class), page, () -> mongoTemplate.count(query, Entity.class));
}

enumtype:

public enum EnumType {
    TYPE1,TYPE2;

    private EnumType() {
    }
}

两个应用程序都有相同的配置文件,唯一的区别是相应的数据库URL。

I work on a project with two environments, A & B, each with its own database. My spring boot (1.5.3) application uses mongodb, where I am querying a collection based on an enum EnumType with values TYPE1 and TYPE2.

Now, in env. A, when trying to fetch all entries where EnumType = TYPE1, which I can see in my database are about 2000, this takes a long time. Fetching entries where EnumType = TYPE2 takes a normal amount of time.

In env. B, when fetching the TYPE1 entries, which are far more (approx 2M), this takes very little to happen. Fetching the TYPE2 entries (450 entries) takes a ridiculous amount of time.

I have added a type_1 index in both cases (get by EnumType ascending), but these odd times remain, to the point where if I try to fetch the TYPE2 entries in env B, my application crashes. Since the search logic is the same in both environments (the exact same application runs in both cases), I'm not sure what else to do in order to fix this issue.

The code is simple:

EntityController:

@GetMapping
public Page<Entity> getAll(@RequestParam EnumType type, OffsetPageRequest page) {
    return entityService.getAll(type, page);
}

EntityService:

public Page<Entity> getAll(EnumType type, OffsetPageRequest page) {
    return entityRepository.findByCriteria(type, page);
}

CustomizedEntityRepository:

Page<Entity> findByCriteria(EnumType type, Pageable page);

UserProfileRepositoryImpl:

@Override
public Page<Entity> findByCriteria(EnumType type, Pageable page) {
    Query query = new Query().with(page);
    List<Criteria> andCriteriaList = new ArrayList<>();
    andCriteriaList.add(Criteria.where("type").is(type));
    query.addCriteria(new Criteria().andOperator(andCriteriaList.toArray(new Criteria[0])));
    return PageableExecutionUtils.getPage(mongoTemplate.find(query, Entity.class), page, () -> mongoTemplate.count(query, Entity.class));
}

EnumType:

public enum EnumType {
    TYPE1,TYPE2;

    private EnumType() {
    }
}

Both applications have the same configuration files, with the only differences being the corresponding database URLs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文