【问题标题】:Most Efficient Way to Related Data Entries最有效的相关数据输入方式
【发布时间】:2012-10-07 06:17:09
【问题描述】:

我正在使用 Python 中的 Google App Engine 构建一个网站,但这个问题应该适用于任何类似的东西,而不仅仅是这个特定的框架。我试图在两种相关数据模型之间做出决定。

它们涉及一个位置数据库类,每个位置实体都有多个与其挂钩的体验数据库实体。

现在我在自己的内存缓存中有 Locations 类,并且我引用了存储在 expRef IntegerProperty 中的每个体验。然后我执行 Experience.get_by_id(expRef) 以获取所有体验并将它们显示在位置页面上。

class Location(ndb.Model):
    #a bunch of other properties
    expRefs = ndb.IntegerProperty(repeated=True)
class Experience(ndb.Model):
            #a bunch of other properties with no ref to Location

#here is the handler for the Location page
location = individual_location_cache(keyid)
exps= []
            if location.expRefs != []:
                for ref in location.expRefs:
                    records.append(Record.get_by_id(ref))

我想知道这是否是最好的选择,或者最好给每个体验一个 Location 属性的引用,然后将所有引用存储在 Memcached 中,并对 memcached 进行两次调用,一次调用 Location,然后调用那里的所有体验。

class Location(ndb.Model):
        #a bunch of other properties but no ref to experiences
class Experience(ndb.Model):
        #a bunch of other properties
        locationRef = ndb.IntegerProperty()

#the Location page handler starts here
location = individual_location_cache(keyid)
exps= exp_location_cache(keyid)

是否有很大的不同或我忘记了任何选项?

【问题讨论】:

  • 用代码定义你的问题总是比用自然语言更容易,你能提供一些你写的代码和你正在考虑做什么的代码吗?
  • 好吧,我觉得应该有点帮助

标签: python database google-app-engine optimization google-cloud-datastore


【解决方案1】:

您存储它的方式看起来还不错 - 不过,您想要优化的一件非常明显的事情是批量获取体验:

exps= []
        if location.expRefs != []:
            for future in [Record.get_by_id_async(ref) for ref in location.expRefs]:
                records.append(future.get_result())

这样,NDB 将尝试通过一个查询获取您的所有体验 - 并自动将它们缓存在 memcache 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2017-10-06
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多