gpt4 book ai didi

java - 在 App Engine 中查询数据存储的最有效方式

转载 作者:搜寻专家 更新时间:2023-10-31 08:03:49 24 4
gpt4 key购买 nike

我有一个数据存储,其中包含大约 150,000 个实体。当我使用过滤器查询商店时,我的查询速度非常慢。我的结构是完全扁平的,即每个实体都是彼此的 sibling 。

1:使用 GQL 代替过滤器是否更好?

2:这不是 Data Store 的最佳用例吗,我应该改用 SQL 数据库吗?

这是我的代码示例:

// Look for a buy opportunity
dateFilter = new FilterPredicate("date", FilterOperator.EQUAL, dt);
scoreFilter = new FilterPredicate("score", FilterOperator.LESS_THAN_OR_EQUAL, 10.0);
safetyFilter = new FilterPredicate("score", FilterOperator.GREATER_THAN_OR_EQUAL, -1.0);
mainFilter = CompositeFilterOperator.and(dateFilter,scoreFilter,safetyFilter);
q = new Query("StockEntity",stockKey).setFilter(mainFilter);
q.addSort("score", Query.SortDirection.ASCENDING);

stocks = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(availableSlots));

更多细节:

  1. 150,000 条左右的记录,分为 500 只股票,因此每只股票大约有 300 条记录,一个日期范围内的每一天。

  2. 像上面的查询,其中传入了一个特定的日期,并根据“分数”有效地过滤了 500 只股票,希望返回的记录数在 10 到 20 之间,取 30 以上秒完成,在我的开发机器上。

还没有尝试将其投入生产,但我想我接下来会尝试——我认为不会有太大的不同。我的开发机器是相当高规范的 iMac。

最佳答案

https://developers.google.com/appengine/docs/java/datastore/queries#Java_Restrictions_on_queries

Inequality filters are limited to at most one property

To avoid having to scan the entire index table, the query mechanism relies on all of a query's potential results being adjacent to one another in the index. To satisfy this constraint, a single query may not use inequality comparisons (LESS_THAN, LESS_THAN_OR_EQUAL, GREATER_THAN, GREATER_THAN_OR_EQUAL, NOT_EQUAL) on more than one property across all of its filters. For example, the following query is valid, because both inequality filters apply to the same property:

简短的回答是,您真的不能完全按照数据存储的方式进行操作。

关于java - 在 App Engine 中查询数据存储的最有效方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17517517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com