Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/san650/one_plus_n_detector
Elixir library to help you detect 1+n queries in applications using Ecto
https://github.com/san650/one_plus_n_detector
ecto elixir phoenix sql
Last synced: 13 days ago
JSON representation
Elixir library to help you detect 1+n queries in applications using Ecto
- Host: GitHub
- URL: https://github.com/san650/one_plus_n_detector
- Owner: san650
- License: other
- Created: 2018-07-22T23:53:38.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T09:18:29.000Z (almost 2 years ago)
- Last Synced: 2024-03-15T01:23:09.607Z (8 months ago)
- Topics: ecto, elixir, phoenix, sql
- Language: Elixir
- Size: 10.7 KB
- Stars: 25
- Watchers: 2
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# OnePlusNDetector
This library helps detect 1+n SQL queries in your application. This is a common problem when working with ORMs like Ecto.
## What is the 1+n problem?
When you have a parent-child relationship like a `has_many`, `has_one` or `belongs_to` you might load your parent records with one query and then **for each record**, trigger another SQL statment to load the related children.
Let's say you have the following Ecto schemas
```elixir
defmodule User do
use Ecto.Schemaschema "users" do
has_one :details, Details
end
enddefmodule Details do
use Ecto.Schemaschema "users_details" do
belongs_to :user, User
end
end
```You could load all the users with its respective details the following way (**wrong way**)
```elixir
User
|> Repo.all
|> Enum.map(& %{&1 | details: Repo.get(&1.details_id)})
```You can query all users and then for each record that it's returned you can query it's user details. This implementation falls into the 1+N problem because the first query returns N records and for each record you trigger an additional SQL query, having in total 1+N SQL queries issued to your DB.
The number of queries can be reduced to two by just using a [preload](https://hexdocs.pm/ecto/Ecto.Query.html#preload/3) statement.
```elixir
User
|> preload(:details)
|> Repo.all
```This library helps you detect these cases by triggering a warning in your query log.
## Usage
Add an extra logger to Ecto repo in development.
```elixir
config :my_app, MyApp.Repo, loggers: [
{OnePlusNDetector, :analyze, []},
{Ecto.LogEntry, :log, []}
]
```## Documentation
Documentation can be found at [https://hexdocs.pm/one_plus_n_detector](https://hexdocs.pm/one_plus_n_detector).
## Installation
**This library should be used only on development**
```elixir
def deps do
[
{:one_plus_n_detector, "~> 0.1.0", only: :dev}
]
end
```Remember to **not add** `:one_plus_n_detector` to your applications list!
## License
one_plus_n_detector is licensed under the MIT license.
See [LICENSE](./LICENSE) for the full license text.