Rails N+1 queries auto-detection with zero false positives / false negatives
open sourceApache-2.0self-hostablefree
Prosopite finds N+1 queries in Rails apps by watching the SQL itself rather than association loading: in a scan (a request, a job, a test or any block) it reports queries that run more than once from the same call stack with the same fingerprint. That catches the cases association-tracking tools miss, such as records created in tests, lookups by ID in a loop, first, last and pluck on collections, and records changed with becomes. Reports show the repeated queries, the call stack and a suggested fix, and can raise in tests, log in development or go to structured notifications in production. pg_query makes fingerprints more precise on PostgreSQL but is optional.
Problems it solves
- find N+1 queries in my Rails tests and fail the build when a new one appears
- bullet keeps flagging things that aren't real problems and misses real ones
- a page runs the same query 200 times in a loop and nobody noticed
- detect repeated queries that don't come from associations, like find by id in a loop
- tell me which line to add includes to when there's an N+1
- catch N+1 queries in background jobs, not only in web requests
- monitor N+1 queries in production without slowing everything down
- my tests create records and then load them one by one, and nothing warns me
- a slow Rails page is making hundreds of SQL queries
Features
- detection from SQL fingerprints and call stacks
- scans of requests, jobs, tests or any block
- raises in tests
- fix suggestions with the line to add includes
- loggers and structured notifications
- per-database counting (shards don't mix)
- optional pg_query fingerprints on PostgreSQL
- an install generator
Capabilities
| Has an API | plan not known |
|---|
Alternatives
The same job
These do what Prosopite does.
- Bullet Help to kill N+1 queries and unused eager loading
- Goldiloader Just the right amount of Rails eager loading
