【问题标题】:How to improve speed of query for Firestore + Flutter?如何提高 Firestore + Flutter 的查询速度?
【发布时间】:2019-12-21 14:50:52
【问题描述】:

我的 Flutter + Firestore 应用程序存在性能问题:大型数据库查询执行时间(大约 5 秒)。我有一个小的数据库大小,我认为如果它增加,查询执行速度会更大。如何提高应用程序性能?

import 'package:carstat/models/entry.dart';
import 'package:carstat/models/operation.dart';
import 'package:carstat/services/data_service.dart';

class DashboardService {
  DataService dataService = DataService();

  getMarkers(List<Entry> entries, String carId) async {
    var _markers = [];

    for (int i = 0; i < entries.length; i++) {
      List<Operation> _operations = [];
      _operations =
          await dataService.getEntryOperations(entries[i].entryId, carId);
      _markers.add({'entry': entries[i], 'operations': _operations});

    }
    return _markers;
  }
}

以我的数据结构为例:

        .document(docId)
        .collection('cars')
        .document(carId)
        .collection('entries')
        .document(entryId)
        .collection('operations')
        .document();

数据服务代码:

  getEntryOperations(String entryId, String carId) async {
    List<Operation> _operations = [];
    Future<QuerySnapshot> _userDoc =
        fs.where('userId', isEqualTo: _userId).getDocuments();
    await _userDoc.then((res) {
      docId = res.documents[0].documentID;
    });
    Future<QuerySnapshot> _entryOperations = fs
        .document(docId)
        .collection('cars')
        .document(carId)
        .collection('entries')
        .document(entryId)
        .collection('operations')
        .getDocuments();

    await _entryOperations.then((val) {
      for (int i = 0; i < val.documents.length; i++) {
        var _operation = Operation();

        _operation.entryId = entryId;
        _operation.operationNote = val.documents[i].data['operationNote'];
        _operation.operationDate =
            val.documents[i].data['operationDate'].toDate();
        _operation.operationMileage = val.documents[i].data['operationMileage'];
        _operation.operationPartName =
            val.documents[i].data['operationPartName'];
        _operation.operationPrice = val.documents[i].data['operationPrice'];
        _operation.partPrice = val.documents[i]['partPrice'];
        _operation.operationId = val.documents[i]['operationId'];
        _operations.add(_operation);
      }
    });
    return _operations;
  }

【问题讨论】:

  • 您的代码中没有 Firestore 查询。你能显示实际的查询吗?

标签: flutter dart google-cloud-firestore


【解决方案1】:

您显示的查询是无条件地获取特定子集合中的所有文档。当然,随着收藏的增长,这将需要更多时间。无需传递任何秘密技巧或特殊标志来加快此查询的发生速度。

事实上,除了限制集合的大小,或者限制查询中的文档数量之外,您根本没有什么可以做的。您可能还想重新考虑您的数据库结构,以减少要获取的文档数量。

【讨论】:

    【解决方案2】:

    我的答案,要快得多

    class DashboardService {
      DataService dataService = DataService();
    
      getMarkers(List<Entry> entries, String carId) async {
        var _marker = []; // коллекция списков операторов для каждого регламента ТО
    
        final opsForEntries = await Future.wait(
          entries.map((value) {
            return dataService.getEntryOperations(value.entryId, carId);
          })
        );
    
         for(int i = 0; i < entries.length; i++) {
           _marker.add(
             {
               'entry': entries[i],
               'operations': opsForEntries[i]
             }
           );
         }
    
        return _marker;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2019-10-09
      • 1970-01-01
      • 2021-11-17
      • 2012-09-01
      • 1970-01-01
      相关资源
      最近更新 更多