Is there a way to specify a projection for ndb.get_multi()?

余生颓废 提交于 2021-02-07 18:42:49

问题


Using NDB, it is possible to specify a projection for a query, allowing to limit the number of properties that are retrieved for the entities that match the query.

However, I couldn't find anything in the documentation about how to specify a projection when using ndb.get_multi(), which always fetches complete entities.

Is there a way to fetch only certain properties when using ndb.get_multi()?


回答1:


No, the projection feature only works for queries. There would be no advantage (in terms of fewer I/O operations) to projecting get() operations.




回答2:


I have ended up in a situation where using projection with ndb.get_multi() did result in performance improvements. I have a large entity class with ~25 properties, some of which are even repeated. I need to get_by_id about 10 of these objects for serving each web page in my app, but I only need 4 properties per object. If I implement this using plain ndb.get_multi(), then around half the frontend CPU time ends up being spent in an NDB API method named Model._from_pb, i.e. deserializing the objects that were read from the database. This looks like something that might be improved with projection, and indeed, in my case using projection the below way have cut the overall response time by half:

Product.query(Product.key.IN(keys_to_get)).fetch(projection=list_of_columns)

Where Product is the name of my entity class. Of course in this case a new index is needed for the query.



来源:https://stackoverflow.com/questions/11936904/is-there-a-way-to-specify-a-projection-for-ndb-get-multi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!