3

我使用 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;
}
4

1 回答 1

0

你能描述一下你的域对象吗?员工和部门区域中的键和值是什么?你在使用 PDX 吗?

一种简单的方法可能是将其deptId作为部门区域的键。然后在您的函数中,您可以迭代该Employee区域并对该区域执行get(deptId)a Department。为了进一步减少延迟,您可以将大量结果发送回客户端,同时您的服务器继续运行该功能。由于您提到结果中有超过 7000 个条目,因此您可以一次从服务器批量处理 500 个。像这样的东西:

@Override
public void execute(FunctionContext context) {
  RegionFunctionContext rfc = (RegionFunctionContext) context;
  Region<EmpId, PDXInstance> employee = PartitionRegionHelper.getLocalPrimaryData(rfc.getDataSet());
  Region<DeptId, PDXInstance> department = PartitionRegionHelper.getLocalPrimaryData(rfc.getDataSet());
  int count = 0;
  Map<PdxInstance, PdxInstance> results = new HashMap<>();
  for (Region.Entry<EmpId, PDXInstance> e : employee.entrySet()) {
    PdxInstance dept = department.get(e.getValue().get("deptId"));
    results.put(e.getValue(), dept);
    if (count == 500) {
      context.getResultSender().sendResult(results);
      results.clear();
      count = 0;
    }
  }
  context.getResultSender().lastResult(results);
}

然后在客户端上,您可以使用自定义结果收集器,当结果从服务器到达时,该收集器将能够逐块处理结果。

于 2016-06-28T23:18:20.857 回答