简体   繁体   English

从Web服务和mysql查询多个数据的最佳方法?

[英]Best way to query multiple data from webservice and mysql?

I have an android app which uses java rest api, which gets the data from mysql database. 我有一个使用java rest api的android应用,该应用从mysql数据库获取数据。

After I open the app, I download a list of some items. 打开应用程序后,我将下载一些项目的列表。 Then, I make another 5 requests to rest api to get other connected resources, so: 然后,我再次发出5个请求以请求休息api以获取其他已连接的资源,因此:

First I download a list of shops. 首先,我下载商店清单。 Then, having these shops ids, I make 5 http requests to rest api and it fetches (from mysql) lists of photos (not actual images, just urls and ids), ratings, opening hours and 2 more things 然后,有了这些商店ID,我发出了5个http请求来请求api,它(从mysql中)获取了照片列表(不是实际图像,只是URL和ID),评级,开放时间和另外2件事

This makes 6 api (and db) calls and it takes a long time. 这将进行6次api(和db)调用,并且需要很长时间。 Is there a better way to do it? 有更好的方法吗?

You might rewrite the multiple queries into a single JOIN and fetch them in a single network round trip. 您可以将多个查询重写为单个JOIN,并在单个网络往返中获取它们。

Be sure you understand where the time is being spent and why. 确保您了解花费的时间以及原因。 Your queries might be slow if you have WHERE clauses that don't use indexes. 如果您的WHERE子句不使用索引,则查询可能会很慢。 Run EXPLAIN PLAN and see if there's a TABLE SCAN being executed. 运行EXPLAIN PLAN,查看是否正在执行TABLE SCAN。 If you see one, you can immediately improve performance by adding appropriate indexes on the columns in the WHERE clauses. 如果看到一个,则可以通过在WHERE子句中的列上添加适当的索引来立即提高性能。

Why don't yout retrieve all those data in the first call? 为什么不在第一次通话中检索所有这些数据? the response of restapi can be json or xml. restapi的响应可以是json或xml。

REQUEST: GET /shop/list

RESPONSE XML:
<shops>
  <shop name="shop1" img="../../img1.jpg" ></shop>
  <shop name="shop2" img="../../img2.jpg" ></shop>
  <shop name="shop3" img="../../img3.jpg" ></shop>
</shops>

RESPONSE JSON:
[{name:"shop1",img:"../../img1.jpg"},
{name:"shop2",img:"../../img2.jpg"},
{name:"shop3",img:"../../img3.jpg"}]

You could handle these responses in your Java/Android application 您可以在Java / Android应用程序中处理这些响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM