"SQL Group By" Google Cloud Platform 的数据存储替代方案
"SQL Group By" Alternative for Datastore of Google Cloud Platform
假设以下是我在 Google Cloud Platform 上的数据存储内容:
class ItemRecord {
@Id
private
Long id;
@Index
private String item;
@Index
private String user;
@Index
private int minValue;
@Index
private int maxValue;
}
我想获得 item
组的最小值 minValue
和最大值 maxValue
:
item1 minumum_minValue= 1, maximum_maxValue= 13
item2 minumum_minValue= 3, maximum_maxValue= 10
注意: minValue 和 maxValue 属性是可更新的(实体是用户给定的值,用户可以随时更新)所以请如果您打算建议使用单独的实体来存储最小值 minValue
和最大值 maxValue
值,请考虑更新、删除操作。
我正在寻找类似下面的东西 sql:
SELECT item, MAX(maxValue)
FROM [ItemRecord]
Group by item
和
SELECT item, MIN(minValue)
FROM [ItemRecord]
Group by item
但 Datastore 不支持 "group by" 操作,我找不到任何类似的东西。
那么,我是如何解决这个问题的?
1) 获取不同的项目名称:
private static List<String> getDistinctItemNameList() {
Query query = ofy().load().type(ItemRecord.class)
.project("item").distinct(true);
List<ItemRecord> resultList = query.list();
if(resultList != null && !resultList.isEmpty()) {
List<String> itemNameList = new ArrayList<>(resultList.size());
for (ItemRecord itemRecord : resultList) {
itemNameList.add(itemRecord.getItem());
}
return itemNameList;
}
return null;
}
2) 对于每个 itemName(上面检索到的)查询数据存储的最小值和最大值:
private ItemRecord getMinumumMinValue(String itemName) {
ItemRecord record = ofy()
.load()
.type(ItemRecord.class)
.filter("item", itemName)
.order("minValue")
.first().now();
return record;
}
private ItemRecord getMaximumMaxValue(String itemName) {
ItemRecord record = ofy()
.load()
.type(ItemRecord.class)
.filter("item", itemName)
.order("-maxValue")
.first().now();
return record;
}
成本:(检索不同项目名称的查询)+(不同项目计数 * 查询最小 minValue)+(不同项目计数 * 查询最大 maxValue)
但是这个解决方案非常烦人,因为它有很多读取操作。你有什么建议,更好的解决方案吗?
数据存储是一个很差的分析查询工具。将您的数据子集复制到云 SQL 或其他一些可以轻松 运行 聚合的关系存储中。
假设以下是我在 Google Cloud Platform 上的数据存储内容:
class ItemRecord {
@Id
private
Long id;
@Index
private String item;
@Index
private String user;
@Index
private int minValue;
@Index
private int maxValue;
}
我想获得 item
组的最小值 minValue
和最大值 maxValue
:
item1 minumum_minValue= 1, maximum_maxValue= 13
item2 minumum_minValue= 3, maximum_maxValue= 10
注意: minValue 和 maxValue 属性是可更新的(实体是用户给定的值,用户可以随时更新)所以请如果您打算建议使用单独的实体来存储最小值 minValue
和最大值 maxValue
值,请考虑更新、删除操作。
我正在寻找类似下面的东西 sql:
SELECT item, MAX(maxValue)
FROM [ItemRecord]
Group by item
和
SELECT item, MIN(minValue)
FROM [ItemRecord]
Group by item
但 Datastore 不支持 "group by" 操作,我找不到任何类似的东西。
那么,我是如何解决这个问题的?
1) 获取不同的项目名称:
private static List<String> getDistinctItemNameList() {
Query query = ofy().load().type(ItemRecord.class)
.project("item").distinct(true);
List<ItemRecord> resultList = query.list();
if(resultList != null && !resultList.isEmpty()) {
List<String> itemNameList = new ArrayList<>(resultList.size());
for (ItemRecord itemRecord : resultList) {
itemNameList.add(itemRecord.getItem());
}
return itemNameList;
}
return null;
}
2) 对于每个 itemName(上面检索到的)查询数据存储的最小值和最大值:
private ItemRecord getMinumumMinValue(String itemName) {
ItemRecord record = ofy()
.load()
.type(ItemRecord.class)
.filter("item", itemName)
.order("minValue")
.first().now();
return record;
}
private ItemRecord getMaximumMaxValue(String itemName) {
ItemRecord record = ofy()
.load()
.type(ItemRecord.class)
.filter("item", itemName)
.order("-maxValue")
.first().now();
return record;
}
成本:(检索不同项目名称的查询)+(不同项目计数 * 查询最小 minValue)+(不同项目计数 * 查询最大 maxValue)
但是这个解决方案非常烦人,因为它有很多读取操作。你有什么建议,更好的解决方案吗?
数据存储是一个很差的分析查询工具。将您的数据子集复制到云 SQL 或其他一些可以轻松 运行 聚合的关系存储中。