【问题标题】:append timestamp string for index python为索引python附加时间戳字符串
【发布时间】:2019-10-07 04:44:03
【问题描述】:

我正在使用 Google App Engine、标准环境、NDB 数据存储区、Python 2.7。每个项目的索引限制为 200 个。

为了减少索引的数量,我打算这样做:

我在模型中有三个字段,report_typecurrent_center_urlsafe_keytimestamp_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 不等式过滤器会提供所需的结果吗? p>

【问题讨论】:

    标签: google-app-engine google-cloud-datastore


    【解决方案1】:

    策略有问题。您需要同时应用 ">=" 和 "

    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”的所有数据,但您最终也会获得所有以开头的数据“b-”因为“b-*”>=“a-123-20190801”。但是,如果您执行“key >= a-123-20190801 and key

    【讨论】:

    • 是的。谢谢 - 将包括 ">= 和
    • 是的,只要确保示例中的 current_timestamp_ms 随时间增加即可。当您以秒/毫秒为单位使用 unix 时间而没有任何格式时,它的行为与您预期的一样。
    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 2015-04-19
    • 2020-11-22
    • 2016-11-28
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多