为索引附加时间戳字符串 python
append timestamp string for index python
我正在使用 Google App Engine,标准环境,NDB 数据存储,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 Datastore 不平等过滤器是否会提供所需的结果?
策略有问题。您需要同时应用“>=”和“<=”过滤器,以确保您不会从其他前缀值中获取记录。举个例子,假设你的数据如下
a-123-20191001
a-123-20190901
a-123-20190801
b-123-20191001
b-123-20190901
b-123-20190801
现在,如果您这样做 "key >= a-123-20190801",您将获得自 2019 年 8 月 1 日以来前缀 "a-123" 的所有数据,但您最终也会得到以 [=17] 开头的所有数据=] 因为 "b-*" >= "a-123-20190801"。但是如果你这样做 "key >= a-123-20190801 and key <= a-123-20191001" 那么你的数据将只属于那个前缀。
我正在使用 Google App Engine,标准环境,NDB 数据存储,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 Datastore 不平等过滤器是否会提供所需的结果?
策略有问题。您需要同时应用“>=”和“<=”过滤器,以确保您不会从其他前缀值中获取记录。举个例子,假设你的数据如下
a-123-20191001
a-123-20190901
a-123-20190801
b-123-20191001
b-123-20190901
b-123-20190801
现在,如果您这样做 "key >= a-123-20190801",您将获得自 2019 年 8 月 1 日以来前缀 "a-123" 的所有数据,但您最终也会得到以 [=17] 开头的所有数据=] 因为 "b-*" >= "a-123-20190801"。但是如果你这样做 "key >= a-123-20190801 and key <= a-123-20191001" 那么你的数据将只属于那个前缀。