获取相关数据项的最有效方法

2024-04-26 07:26:40 发布

您现在位置:Python中文网/ 问答频道 /正文

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

它们涉及一个位置数据库类,每个位置实体都有多个连接到它的经验数据库实体。你知道吗

现在,我在自己的memcache中有Locations类,并且我在expRef IntegerProperty中存储了对每个体验的引用。那我会的体验。按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进行两个调用,一个调用位置,然后一个调用那里的所有体验。你知道吗

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)

有什么大的不同或者我忘记了什么选择吗?你知道吗


Tags: oftherefcachemodellocationpropertiesclass
1条回答
网友
1楼 · 发布于 2024-04-26 07:26:40

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

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中。你知道吗

相关问题 更多 >