【问题标题】:Trying to increase the performance of my GAE app试图提高我的 GAE 应用程序的性能
【发布时间】:2013-04-20 21:12:15
【问题描述】:

我正在尝试使用 memcache 来提高性能。
这是我的模型:

class ABC(db.Model):
   some_property = db.StringProperty()
# more properties

class XYZ(db.Model):
   another_property = db.StringProperty()
   abc = db.ReferenceProperty(ABC,collection_name="xyzs")
# more properties

我只有两个 ABC 实体和 800 个 XYZ 实体
因此,该应用程序的功能之一是为所有 XYZ 提供 excel 表。 excel 工作表有两列。
第一列是“another_property”,第二列是“some_property”(来自 ABC 参考)。


xyzs = XYZ.all()
for xyz in xyzs:
   logging.info(xyz.another_property)
   logging.info(xyz.abc.some_property)

使用这种方法,xyz.abc.some_property 每次都会调用数据存储区

看到这一点,我决定使用 memcache 将 abc 引用存储在内存中。
使用 memcache 后,我没有看到响应时间有任何重大变化。

abcId = XYZ.abc.get_value_for_datastore(xyz).id()
#Get ABC reference from memcache if present else bring it from datastore and add it to memcahce.

请问为什么我没有看到任何性能提升?

【问题讨论】:

    标签: google-app-engine python-2.7 google-cloud-datastore appstats


    【解决方案1】:

    我试图为每个 XYZ 实体做一个 get memcache。 我通过批量获取 memcache https://developers.google.com/appengine/docs/python/memcache/clientclass#Client_get_multi 解决了这个问题
    伪代码:

    
    abcIds = [str(XYZ.abc.get_value_for_datastore(xyz).id() for xyz in xyzs]
    abcs = memcache.get_multi(abcIds) #This gives me a dictionary of id as key and ABC reference property as value
    

    这是我的应用改进版本的快照。

    发布它可能对其他人有帮助。
    PS:我感觉这个也可以改进。帮我改进一下答案。

    【讨论】:

      【解决方案2】:

      您可能会注意到,如果您刚开始,请考虑使用 ndb。

      现在回答我的问题,您应该使用更有效的预取引用属性方法,请参阅 nick johnson http://blog.notdot.net/2010/01/ReferenceProperty-prefetching-in-App-Engine 的文章

      它基本上收集了引用属性中的所有键,然后对所有实体进行一次获取。您可能会发现它的性能与 memcache 一样好,而且如果实体从 memcache 中被驱逐(这将发生),您将获得所有数据。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 2011-02-09
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多