【问题标题】:Mongodb Spring Data: Slow query on indexed field?Mongodb Spring Data:索引字段查询慢?
【发布时间】:2016-07-10 05:41:42
【问题描述】:

我试图测量执行这个简单查询需要多长时间:

companyRepository.findOne(companyId); // took 300ms

这是我使用的存储库类:

package fn.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import fn.model.Company;

@RepositoryRestResource(exported = false)
public interface CompanyRepository extends MongoRepository<Company, String>, QueryDslPredicateExecutor<Company> {
}

索引查找需要这么长时间是否正常?

【问题讨论】:

    标签: java mongodb spring-data-mongodb querydsl spring-mongodb


    【解决方案1】:

    这是正常。插入/更新到具有索引的数据库应该比简单地写入堆结构花费更多的时间,因为数据库必须维护顺序和平衡树(MongoDB 使用 B-Tree 数据结构来实现索引,就像任何其他数据库系统一样)。 关注:https://docs.mongodb.org/manual/core/indexes-introduction/#single-field

    减少操作时间的最佳方法是在文档的单个字段上创建索引,您可能想要对其进行排序/搜索,而不是在许多非必需字段上建立索引

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      • 1970-01-01
      • 2019-06-18
      • 2018-07-13
      • 2019-02-10
      • 2015-05-19
      • 1970-01-01
      相关资源
      最近更新 更多