Mongodb Morphia:查询一个mongo集合,该集合对另一个集合有@reference,这需要时间
我有两个mongo系列:Mongodb Morphia:查询一个mongo集合,该集合对另一个集合有@reference,这需要时间,mongodb,morphia,Mongodb,Morphia,我有两个mongo系列: public class TransactionDetails { private String TYPE="NA"; private Long TransactionDateInLong; @Reference List<AccountingTransaction> accountingTransactionList; } 我试图使用morphia框架查询“TransactionDetails”集合以获取引用的“Accou
public class TransactionDetails {
private String TYPE="NA";
private Long TransactionDateInLong;
@Reference
List<AccountingTransaction> accountingTransactionList;
}
我试图使用morphia框架查询“TransactionDetails”集合以获取引用的“AccountingTransaction”
以下是morphia查询的方法:
public List<TransactionDetails> getTransactions(Long fromDateInLong, Long endDateInLong) {
Query<TransactionDetails> query = createQuery();
query.and(
query.criteria("TransactionDateInLong").lessThanOrEq(endDateInLong),
query.criteria("TransactionDateInLong").greaterThanOrEq(fromDateInLong)
);
query.and(
query.criteria("TYPE").equal("Income")
);
return query.retrievedFields(true, "accountingTransactionList").asList();
}
随着索引的创建,查询需要花费大量时间
将异常获取为“在服务器上找不到游标”
有什么改进吗?我遗漏了什么吗?如果您引用了大量的
会计交易
这将是一个非常昂贵的查询@Reference
只是一个驱动程序功能,因此在后台会发生什么:
- 您可以查询
,将其传输到应用程序并对其进行解析交易详细信息
- 当驱动程序在
中找到一个元素时,它将对每个元素运行一个查询以获取该元素(每个元素在应用程序和MongoDB之间进行另一次往返)@Reference
- 它将在您的应用程序中结合
和TransactionDetails
AccountingTransaction
TransactionDetails
,并且每个查询都有10个AccountingTransaction
,那么您将总共运行1+100*10个查询
我认为它在幕后做了完全相同的事情,但我发现这样的查询更具可读性(默认情况下,所有内容都是和连接的:
return createQuery<TransactionDetails>()
.field("TransactionDateInLong").lessThanOrEq(endDateInLong)
.field("TransactionDateInLong").greaterThanOrEq(fromDateInLong)
.field("TYPE").equal("Income")
.asList();
返回createQuery()
.field(“TransactionDateInLong”).lessThanOrEq(endDateInLong)
.field(“TransactionDateInLong”).greaterThanOrEq(fromDateInLong)
.字段(“类型”).相等(“收入”)
.asList();
理解。鉴于TransactionDetails有几个AccountingTransaction(比如最多10个),我觉得最好使用@Embedded符号嵌入AccountingTransaction。这样我可以保存网络调用。
db.getCollection('TransactionDetails').createIndex({"TransactionDateInLong" : 1.0});
db.getCollection('TransactionDetails').createIndex({"TYPE" : "text"});
return createQuery<TransactionDetails>()
.field("TransactionDateInLong").lessThanOrEq(endDateInLong)
.field("TransactionDateInLong").greaterThanOrEq(fromDateInLong)
.field("TYPE").equal("Income")
.asList();