It can be related to both. Node.js can handle multiple asynchronous database operations, but slow queries can consume database connections and increase the number of pending requests.
If your application uses a limited database connection pool, new requests may have to wait until existing connections become available.
To troubleshoot this:
- Identify slow queries using database query analysis.
- Add appropriate indexes to frequently searched columns.
- Avoid retrieving unnecessary rows or columns.
- Configure a reasonable database connection pool size.
- Set query timeouts where supported.
- Monitor pending requests and database connection usage.
For example, with MySQL:
EXPLAIN SELECT *
FROM users
WHERE email = 'user@example.com';
The EXPLAIN command helps you understand how MySQL executes a query and whether an appropriate index is being used.