Slow Loading MongoDB Query

Hi - I'm noticing extremely slow loading MongoDB query in Retool that is instant when I run in MongoDB Compass.

return (async () => {
  if(isLoggedInState) {  
    //console.log("LoggedIn is true");    
    var assignedTerritories = await MDBAssignmentsAggregateAssigned.trigger();
    // If more than 4 territories, disable request button
    if(assignedTerritories && _.keys(assignedTerritories).length > 0) {
      if(assignedTerritories.length > 3) {
        //console.log("assignedTerritories: "+assignedTerritories);
        //console.log("selectedTerritory: "+selectAssignmentCurrent.value);
        if(selectAssignmentCurrent.value === null || selectAssignmentCurrent.value === "") {        
          await selectAssignmentCurrent.setValue(assignedTerritories[0]._id.toString());
          await currentAssignmentIDState.setValue(assignedTerritories[0]._id.toString());
          await currentTerritoryIDState.setValue(assignedTerritories[0].No._id.toString());  
    if(selectAssignmentCurrent.value) {
      await currentAssignmentIDState.setValue(selectAssignmentCurrent.value);       
      if(currentAssignmentIDState.value.length > 1) {
        //console.log("currentAssignmentID: "+currentAssignmentIDState.value);
        var currentAssignment = await MDBAssignmentsFindOne.trigger();        
         if(currentAssignment) {      
          await currentTerritoryIDState.setValue(currentAssignment.No.toString());
          //console.log("currentTerritory: "+currentTerritoryIDState.value);  
          // If territory found, get addresses
          if(currentTerritoryIDState.value.length > 0 ) {
            var Addresses = await MDBTerritoriesAggregateAddresses.trigger();
            if(Addresses) {
            return true;  
        } else {
          utils.showNotification({ title: "There was an error.", description: "We are unable to look up your assigned territories. Please contact support.", notificationType: "error", duration: 6 });
          return false;
      // Else if no selected territory
  } else  {
       utils.showNotification({ title: "Please Request a Territory", description: "You have no territories. Please click \"Request Territory\" to check out a territory. If you have already done so, please wait for your territory to be assigned.", notificationType: "warning", duration: 6 });
      return false;

Hey @finedesignz! Since Retool is handling both the backend requesting, response parsing and frontend rendering, it's not uncommon that it can take longer to load, unfortunately. Based on the query you sent, it looks like there's some JS logic also running that might add to the individual query run. If you hover over the time next to your queries, what do the breakdowns look like?\

There are a few general suggestions we recommend that might have an immediate effect on load times and application speed!

1: Caching query results. This would allow you to store your queries for a set amount of time, preventing your application from requesting the data from scratch every single time.

2. Selective query activation. By specifying exactly when you do or do not want a query to run, you can help your application only load and request data when absolutely needed

3. Smaller queries. This one is an obvious one, but it must be listed as a possible option. Loading large amounts of data or having a handful of queries and requests running at the same time could cause some noticeable load issues.

1 Like