我使用 Apache Geode 作为缓存解决方案。我需要在 2 个不同的区域内存储数据并使用简单的连接查询检索它们。
我已经尝试过复制区域和分区区域,但发现查询需要很长时间才能返回结果。我也在这两个区域添加了索引,这提高了性能,但仍然不够快。有人可以帮助如何提高此查询的性能。
这是我尝试过的
示例 1 - 分区区域
从缓存中检索大约 7300 条记录所需的时间为 36 秒
cache.xml 中的配置
<region name="Department">
<region-attributes>
<partition-attributes redundant-copies="1">
</partition-attributes>
</region-attributes>
<index name="deptIndex" from-clause="/Department" expression="deptId"/>
</region>
<region name="Employee">
<region-attributes>
<partition-attributes redundant-copies="1" colocated-with="Department">
</partition-attributes>
</region-attributes>
<index name="empIndex" from-clause="/Employee" expression="deptId"/>
</region>
查询功能
@Override
public void execute(FunctionContext context) {
// TODO Auto-generated method stub
Cache cache = CacheFactory.getAnyInstance();
QueryService queryService = cache.getQueryService();
ArrayList arguments = (ArrayList)context.getArguments();
String queryStr = (String)arguments.get(0);
Query query = queryService.newQuery(queryStr);
try {
SelectResults result = (SelectResults)query.execute((RegionFunctionContext)context);
ArrayList arrayResult = (ArrayList)result.asList();
context.getResultSender().sendResult(arrayResult);
context.getResultSender().lastResult(null);
} catch (FunctionDomainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TypeMismatchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NameResolutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryInvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
执行函数
Function function = new QueryFunction();
String queryStr = "SELECT * FROM /Department d, /Employee e WHERE d.deptId=e.deptId";
ArrayList argList = new ArrayList();
argList.add(queryStr);
Object result = FunctionService.onRegion(CacheFactory.getAnyInstance().getRegion("Department")).withArgs(argList).execute(function).getResult();
ArrayList resultList = (ArrayList)result;
ArrayList<StructImpl> finalList = (ArrayList)resultList.get(0);
示例 2 - 复制区域
从缓存中检索大约 7300 条记录所需的时间为 29 秒
cache.xml 中的配置
<region name="Department">
<region-attributes refid="REPLICATE">
</region-attributes>
<index name="deptIndex" from-clause="/Department" expression="deptId"/>
</region>
<region name="Employee">
<region-attributes refid="REPLICATE">
</region-attributes>
<index name="empIndex" from-clause="/Employee" expression="deptId"/>
</region>
询问
@Override
public SelectResults fetchJoinedDataForIndex() {
QueryService queryService = getClientcache().getQueryService();
Query query = queryService.newQuery("SELECT * FROM /Department d, /Employee e WHERE d.deptId=e.deptId");
SelectResults result = null;
try {
result = (SelectResults)query.execute();
System.out.println(result.size());
} catch (FunctionDomainException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TypeMismatchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NameResolutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (QueryInvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}