title: The query takes a long time result in a 504 timeout error?
body: <p>everyone. I am currently maintaining an old system using struts2 and hibernate. There is a function which is formatting data in Excel then download it according to the query parameters. Usually, 3000 records was added per day, and usually we just need to check today's data, which is down about 3000 records in Excel format.</p>
  
  <p>It works fine. Then I got a new demand, for every record, I need to get two position information about the record. Well, the position information doesn't exist in our database. I need to request two interfaces to get them. I simply do this in a for loop, that is when I got the data returned from database, then loop through the array list, for every record, request for the two position information it needed. Until I got all the message, then I format them in Excel, and then respond to front end.</p>
  
  <p>The problem is, the server responds too slow. When it about 200、300 records, it works fine. But when it comes to 3000 records, I got a 504 timeout error.</p>
  
  <p>I don't think 3000 records is large, but I think for every record sending a request then parse the respond message is time consuming. I think I'm doing it the wrong way, but I have little experience dealing with this situation. So, can I get some suggestion? Thanks in advance.</p>
  
  <p>Edit: I record time when downloading <strong>200</strong> records, with and without external requests, and the pseudo code here. I thought the external requests is the main reason.
  <em>with external requests</em>
  <a href="https://i.stack.imgur.com/GHoVo.png" rel="nofollow noreferrer">with external requests</a>
  <a href="https://i.stack.imgur.com/DdRmk.png" rel="nofollow noreferrer">with external requests</a>
  <em>without external requests</em>
  <a href="https://i.stack.imgur.com/puyfr.png" rel="nofollow noreferrer">without external requests</a>
  <a href="https://i.stack.imgur.com/SoUUO.png" rel="nofollow noreferrer">without external requests</a></p>
  
  <pre><code>    public byte[] methodName() {
  
    // 13 title
    String[] title = { "title1", "title2", "title3", ... , "title13" };
    // result is the records returned from db select
    String[][] data = new String[result.size()*20][title.length];
  
    int row = 0, col = 0;
  
    SomeEntity someEntity = null;
  
    System.out.println("with external requests");
    System.out.println("before loop--&gt;" + System.currentTimeMillis() + " - " + new Date().toString());
  
    for (Object object : result) {
      someEntity = (SomeEntity) object;
      col = 0;
      data[row][col++] = someEntity.getTitle1();
      data[row][col++] = someEntity.getTitle2();
      // add other data
      ...
      // get location, two similar requests
      data[row][col++] = getXXXLocation(someEntity.getLongitude(), someEntity.getLatitude());
      data[row][col++] = getXXXLocation(someEntity.getMctId(), someEntity.getTerId());
      row++;
    }
    // then generate the ExcelModel
    System.out.println("after loop--&gt;" + System.currentTimeMillis() + " - " + new Date().toString());
    ExcelModel excelModel = new ExcelModel("filename");
    excelModel.addColumnsTitle(title);
    System.out.println("before generate excel--&gt;" + System.currentTimeMillis() + " - " + new Date().toString());
    byte[] aws = excelModel.generateExcelFile(data);
    System.out.println("after generate excel--&gt;" + System.currentTimeMillis() + " - " + new Date().toString());
  
    return aws;
  }
  </code></pre>