I'm building my first website using Node.js as the webserver and fetch to request to the server. When the MySQL database is run from localhost, my requests take less than 30ms each and there are no issues with the webpage. However, when the server is hosted somewhere else, I get very long response times ONLY when the resulting data from the query returns '[].' I'm trying to figure out why this would be the case only when requesting from a non-localhost database (or why this is happening in general).
See the difference in response times between these two links:
{ link removed }
{ link removed }
You'll see that the one which returns '[]' takes significantly longer than the other.
Here's the serverside code used:
router.get('/getgames/:matchid/:season', (req, res) => {
let sql = `SELECT * FROM games WHERE match_id='${req.params.matchid}' AND season='${req.params.season}'`;
let query = db.query(sql, (err, result) => {
if (err) throw err;
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(result));
});
});
I don't think it's necessary to include the clientside code because the request takes just as long when it's put into the URL bar.
It's also important to note that the tables being accessed have less than 10 rows, so I don't think indexing/posting my explains would actually fix this problem (or would it??).
Edit: I've added a covering index to the 'games' table, and the query is still just as slow.
Thanks for your help.
For some reason I'm not sure of, node seems to be waiting for keep-alive to be closed before sending that empty array body. Disabling keep-alive mitigates this issue, see https://nodejs.org/api/http.html#http_server_keepalivetimeout (the default timeout of keep-alive is 5s)
I suppose having a longer body overcomes this issue, that's why it's returning immediately for sets with actual sql results.