高效的查询方式 [英] Efficient way to query

查看:74
本文介绍了高效的查询方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有一个类,可以保存用户上传的图片.该类中的每个对象都有一个city属性,该属性保存拍摄照片的城市的名称,以及like属性,该属性跟踪喜欢的次数.

My app has a class that saves picture that users upload. Each object in the class has a city property that holds the name of the city that the picture was taken at, and a like property that tracks the number of likes.

我希望能够发送一个查询,该查询返回每个城市一张图片,并且每张图片在其所属城市中的喜欢人数排名最高.我怎样才能做到这一点?

I want to be able to send a query that returns one picture per city and each picture should have the highest ranking of likes in the city it belongs to. How can I do that?

我首先想到的一种方法是通过获取最喜欢的城市图片并将其保存在数组中,然后对其他城市进行相同的操作来进行多个查询. 但是,每个国家/地区都有一个以上的城市,因此效率不高.

One way which I first thought about is doing multiple queries by fetching the most liked picture of a city and save it in an array, and then do the same to other cities. However, each country has more than one city, thus it's not that efficient.

解析不支持数据库中使用的普通操作.此外,我尝试使用复合查询.不幸的是,我无法对子查询设置限制或排序.有什么好的解决方案吗?

Parse doesn't support the ordinary operations used in databases. Besides, I tried to use a compound query. Unfortunately, I can't set limit or ordering on the subqueries. Any good solution for this?

推荐答案

使用group by很容易.不幸的是,Parse不支持选择不同"或分组依据"功能.

It would be easy using group by. Unfortunately, Parse does not support "select distinct" or "group by" features.

如您所建议,您需要为每个国家/地区获取所有城市,并为每个城市获取评价最高的照片.

As you've suggested you need to fetch for each country all the cities, and for each one get the top most rated photo.

但是,由于解析对请求执行的持续时间有严格的限制(事件监听器为3秒,自定义函数为7秒),因此建议您在后台作业中执行此操作,并将其保存在新表中每个城市评价最高的照片.这样,您可以轻松地从客户端查询数据库.后台作业最多可以执行15分钟,然后解析将其删除,因此您可以进行此类查询而不会超时.

BUT, since Parse has strict restrictions on the duration time execution of a request ( 3 sec for an event listener, 7 sec for a custom function ), I suggest you to do this in a background job, saving in a new table the top rated photo for each city. In this way you can easily query the db from client. The Background jobs can be executed up to 15 minuted before parse drop them, so you could make that kind of queries without timeouts.

希望有帮助

这篇关于高效的查询方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆