0

I have a POST API which accepts JSON in following format and inserts data in 5 tables.

But since I have to write 5 different queries to insert data, it takes longer time for API to give the response back.

{
  "table1": [
    {
      "field1": "value1",
      "field2": "value2",
      "field3": "value3"
    },
    {
      "field4": "value4",
      "field5": "value5"
    }
  ],
  "table2": [
    {
      "field1": "value1",
      "field2": "value2",
      "field3": "value3"
    }
  ],
  "table3": [
    {
      "field1": "value1",
      "field2": "value2",
      "field3": "value3",
      "field4": "value4"
    },
    {
      "field5": "value5"
    }
  ],
  "table4": [
    {
      "field1": "value1",
      "field2": "value2",
      "field3": "value3",
      "field4": "value4",
      "field5": "value5"
    }
  ]
}

I wanted to know if theres any tool or technique available out there by which I can execute the flow as following,

  1. Entire JSON would be inserted in a single table
  2. As soon as the it is inserted server will free the front end by giving the response.
  3. Once the client is given the response and is freed, then server will one by one insert the data in respective table.

such a flow will, make the API faster. Thank you for your suggestions.

shellbot
  • 43
  • 7
  • It doesn't seem like 5 queries would take very long. But one solution might be to issue an AJAX call and output a response for each table or row that's inserted; then you could even show a progress bar. – showdev Feb 09 '19 at 11:06
  • 1
    Possible duplicate of [PHP with AJAX - Continue Processing After Sending Response to the User](https://stackoverflow.com/questions/46200957/php-with-ajax-continue-processing-after-sending-response-to-the-user) – showdev Feb 09 '19 at 11:12
  • @showdev queries are getting slow since each table contain minimum 15-20 fields and there are more than 50 tables in JSON, in addition to that many of them are dates in string format and I have to convert string to date while inserting in table. – shellbot Feb 09 '19 at 11:14
  • 2
    You could look into queues. So your API endpoint would just receive the data and add it to the queue. After adding it to the queue it would return a response. Another process will handle the queue and do the DB inserts later on. – Luka Peharda Feb 09 '19 at 11:14
  • ... another process like a [cron job](https://en.wikipedia.org/wiki/Cron), for instance. – showdev Feb 09 '19 at 11:27

0 Answers0