Enforce a limit on the maximum number of SQL queries per request
Currently there is no limit on the number of queries you can run per request. This in turn means you can go completely crazy and run thousands of queries. Lots of queries in turn is either a sign of an N+1 query, or badly written code. There are very few cases where you genuinely need more than e.g. 200 queries (which already is a lot) to do your work.
We need to start with tracking the number of SQL queries per request, perhaps using some kind of Rack middleware. In the test suite we'd then throw errors whenever this amount exceeds 200 (we'll start with this as the threshold). In production we instead log offences, allowing us to see how this behaves vs our test suite.
Plan
Test environment:
- 100 queries max per request
- Exceeding this threshold will result in an exception being raised
Development environment:
- 100 queries max per request
- Exceeding this will result in a message being logged (so we don't block developers from working right away). Eventually this should turn into an error
Production environment:
- 100 queries max per request
- Exceeding this will result in a message being logged in Sentry, but no exception will be triggered
Edited by Yorick Peterse