【发布时间】:2015-08-06 07:59:38
【问题描述】:
DynamoDBMapper 提供了从表中读取一项的不同方法:
- 查询
- 加载
是否有推荐,使用哪一个?在快速测试中,以下两个代码 sn-ps 为具有主键=哈希和范围键=日期的表返回相同的“MyEntry”项,而查询方法大约快 10%。
加载
public MyEntry getEntryForDay(final Integer hash, final LocalDate date) {
return mapper.load(MyEntry.class, hash, date);
}
查询
public MyEntry getEntryForDay(final Integer hash, final LocalDate date) {
final MyEntry hashKeyValues = new MyEntry ();
hashKeyValues.setHash(hash);
final Condition rangeKeyCondition = new Condition()//
.withComparisonOperator(ComparisonOperator.EQ.toString())//
.withAttributeValueList(new AttributeValue().withS(new LocalDateMarshaller().marshall(date)));
final DynamoDBQueryExpression<MyEntry> queryExpression = new DynamoDBQueryExpression<MyEntry>()//
.withHashKeyValues(hashKeyValues)//
.withRangeKeyCondition("date", rangeKeyCondition)//
.withLimit(1);
final List<MyEntry> storedEntries = mapper
.query(MyEntry.class, queryExpression);
if (storedEntries.size() == 0) {
return null;
}
return storedEntries.get(0);
}
【问题讨论】:
-
“大约快 10%”。你是如何对这些进行基准测试的?你的尺寸是多少?
-
我没有正确地进行基准测试,只是做了一个时间测量。使用 mapper.load 插入样本表需要 90 毫秒,使用 mapper.query 插入同一张表需要 110 毫秒。
标签: amazon-dynamodb