为索引 Python 追加时间戳字符串



我正在使用Google App Engine,Standard Environment,NDB Datastore,Python 2.7。每个项目限制为 200 个索引。

为了减少索引的数量,我计划这样做:

我有三个字段,report_type,current_center_urlsafe_key模型中的timestamp_entered。我需要查找数据存储中具有特定值current_center_urlsafe_key和report_type的所有条目。我需要根据timestamp_entered(升序和降序(对这些值进行排序

这将消耗单独的复合索引,我想避免它。为了实现此查询,我计划通过组合所有三个值来为每次写入添加一个单独的实体,如下所示:

center_urlsafe_key_report_type_timestamp    = report_type + "***" + current_center_urlsafe_key + str(current_timestamp_ms)

然后我计划做这样的查询:

current_timestamp_ms = int(round(time.time() * 1000))
current_date = date.today()
date_six_months_back = common_increment_dateobj_by_months(self,current_date, -6)
six_month_back_timestamp = (date_six_months_back - date(1970, 1, 1)).total_seconds() * 1000             
center_urlsafe_key_report_type_timestamp    = report_type_selected + "***" + current_center_urlsafe_key + str(six_month_back_timestamp)
download_reports_forward = download_report_request_model.query(ndb.GenericProperty('center_urlsafe_key_report_type_timestamp') >= center_urlsafe_key_report_type_timestamp).order(ndb.GenericProperty('center_urlsafe_key_report_type_timestamp'))
download_reports_backward = download_report_request_model.query(ndb.GenericProperty('center_urlsafe_key_report_type_timestamp') >= center_urlsafe_key_report_type_timestamp).order(ndb.GenericProperty('-center_urlsafe_key_report_type_timestamp'))

我的问题是,如果我将时间戳添加为字符串并添加前缀report_type+"****"+current_center_urlsafe_key,NDB数据存储不等式过滤器会提供所需的结果吗?

策略有问题。您需要同时应用">="和"<="筛选器,以确保不会从其他前缀值提取记录。例如,假设您的数据如下

a-123-20191001
a-123-20190901 
a-123-20190801
b-123-20191001
b-123-20190901
b-123-20190801

现在,如果您执行"键>= a-123-20190801",您将获得自 2019/08/01 以来前缀"a-123"的所有数据,但您最终也会得到以"b-"开头的所有数据,因为"b-*">= "a-123-20190801"。但是,如果您执行"密钥>= a-123-20190801 和密钥 <= a-123-20191001",则您的数据将仅属于该前缀。

最新更新