【问题标题】:nodejs mongodb and android studio slow fetch speednodejs mongodb 和 android studio 获取速度慢
【发布时间】:2020-08-05 11:20:39
【问题描述】:

我有一个系统,它从我的服务器中提取数据并通过 android studio 将其存储到移动 SQL 数据库中。它有效,但像 30 分钟一样缓慢。我的数据库中有大约 86000 条记录,并希望将它们全部从服务器中提取出来。最好的方法是什么?

目前我从服务器获取计数,然后查询服务器数据库,直到找到每个 ID,然后将结果发送回我的移动应用程序。

app.post("/get_data", function(req, res)
{
    var Id_request = req.body.Id_request;//The requested ID

    var query = {Val_String : Id_request};//Query value
    //console.log(query);
    //Data.find({}, function(err, result) {//Finds all data
    Data.findOne(query, function(err, result) {//Finds all data
    if (err) {
      //res.status(400).send(err);
      console.log("Sending error");
      res.status(200).send();
    } else {
      return res.json(result);
    }
  });
});

我在每个 ID 的拉取请求中使用了一个反函数

private void call_method()
    {
        HashMap<String, String> map = new HashMap<>();
        map.put("Id_request", Integer.toString(data_pointer));//The ID value
        Call<Fetch_result> call = retrofitInterface.executeGet_data(map);//Run the post
        call.enqueue(new Callback<Fetch_result>() {
            //call.enqueue(new Callback<Fetch_result>() {
            @Override
            public void onResponse(Call<Fetch_result> call, Response<Fetch_result> response) {
                if (response.code() == 200)//Successful login
                {
                    D1= response.body().getT1_String();
                    D2= response.body().getT2_String();
                  
                    data_pointer = data_pointer + 1;
                    boolean result = BLE_DB.addData_Downloaded(D1,D2);//Add data
    
                    if(data_pointer<= Total_entries) {//Call method again
                        call_method();//Recursive here
                    }else if (data_pointer > Total_entries){
                        Utils.toast(getApplicationContext(),"All data received");
                    }

                } else if (response.code() == 404) {
                    Utils.toast(getApplicationContext(), "Get data fail");
                }
            }

            @Override
            public void onFailure(Call<Fetch_result> call, Throwable t) {
                Utils.toast(getApplicationContext(), "Get data error");
            }
        });
    }

我怎样才能加快速度或以不同的方式加快速度?

【问题讨论】:

  • 是否必须获取所有数据?没有办法按需获取数据吗?
  • 是的,不幸的是我想拿这批货。因为它是必不可少的数据日志。

标签: java android node.js mongodb


【解决方案1】:
  1. 尝试一次获取尽可能多的数据(限制您执行的查询数量)。很难告诉你怎么做,因为我不知道你的 monogDB 集合。
  2. 尝试使用尽可能少的请求来执行此操作。如果您可以一次返回所有获取的数据,这将为您节省一些时间。
  3. 在处理 86000 个文档时,JSON 可能会非常慢
  4. 考虑为未来的用户缓存数据

现在我怀疑限制你的是你正在对数据库进行 86000 次查询......如果你可以获得整个 mongoDB 集合,它可能会更快一些(查看注释)

注意事项: https://docs.mongodb.com/manual/reference/method/db.collection.find/#db-collection-find(省略查询参数将导致获取整个集合)

【讨论】:

  • 所以有一个包含更多结果的查询,然后一次性发回更多结果。从而限制查询调用?
  • 还可以发回集合中的 X 个文档吗?
  • 如果您在没有任何查询的情况下执行 db.collection.find(),那么您将获得集合中的所有文档
  • 还可以发回集合中的 X 个文档吗? -> 我不明白你的意思
  • 如果我不添加查询你是正确的我得到所有的条目。但我不知道如何一次收到所有这些。所以我想知道我是否可以发送像 5 然后 5 这样的 X
猜你喜欢
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 2022-01-12
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多