All Projects → san650 → one_plus_n_detector

san650 / one_plus_n_detector

Licence: other
Elixir library to help you detect 1+n queries in applications using Ecto

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to one plus n detector

ecto generator
Generate Ecto schemas from existing database in Phoenix - Elixir
Stars: ✭ 20 (+0%)
Mutual labels:  phoenix, ecto
phoenix pagination
Simple pagination for Ecto and Phoenix that uses plain EEx templates.
Stars: ✭ 20 (+0%)
Mutual labels:  phoenix, ecto
Params
Easy parameters validation/casting with Ecto.Schema, akin to Rails' strong parameters.
Stars: ✭ 239 (+1095%)
Mutual labels:  phoenix, ecto
Mipha
Proj Elixir Forum build with phoenix 1.5.
Stars: ✭ 153 (+665%)
Mutual labels:  phoenix, ecto
ecto nested changeset
Helpers for manipulating nested Ecto changesets
Stars: ✭ 23 (+15%)
Mutual labels:  phoenix, ecto
Phoenix Ecto Encryption Example
🔐 A detailed example for how to encrypt data in a Phoenix (Elixir) App before inserting into a database using Ecto Types
Stars: ✭ 166 (+730%)
Mutual labels:  phoenix, ecto
algoliax
Algolia integration to elixir application
Stars: ✭ 38 (+90%)
Mutual labels:  phoenix, ecto
Ecto morph
morph your Ecto capabilities into the s t r a t o s p h e r e !
Stars: ✭ 72 (+260%)
Mutual labels:  phoenix, ecto
contextual
🌈 Generate your Ecto contexts using this macro and eliminate boilerplate
Stars: ✭ 18 (-10%)
Mutual labels:  phoenix, ecto
ecto profiler
Project for Ecto DB profiling
Stars: ✭ 16 (-20%)
Mutual labels:  phoenix, ecto
Phoenix live dashboard
Realtime dashboard with metrics, request logging, plus storage, OS and VM insights
Stars: ✭ 1,657 (+8185%)
Mutual labels:  phoenix, ecto
ex sieve
Implement dynamic filtering and sorting API for Ecto queries
Stars: ✭ 37 (+85%)
Mutual labels:  phoenix, ecto
Filterable
Filtering from incoming params in Elixir/Ecto/Phoenix with easy to use DSL.
Stars: ✭ 83 (+315%)
Mutual labels:  phoenix, ecto
Formex
A better form library for Phoenix
Stars: ✭ 206 (+930%)
Mutual labels:  phoenix, ecto
Polymorphic embed
Polymorphic embeds in Ecto
Stars: ✭ 80 (+300%)
Mutual labels:  phoenix, ecto
query builder
Compose Ecto queries without effort
Stars: ✭ 56 (+180%)
Mutual labels:  phoenix, ecto
Query
Query adds tools to aid the use of Ecto in web settings.
Stars: ✭ 23 (+15%)
Mutual labels:  phoenix, ecto
Wallaby
Concurrent browser tests with elixir
Stars: ✭ 1,143 (+5615%)
Mutual labels:  phoenix, ecto
querie
Compose Ecto query from the client side
Stars: ✭ 20 (+0%)
Mutual labels:  phoenix, ecto
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (+25%)
Mutual labels:  phoenix, ecto

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

defmodule User do
  use Ecto.Schema

  schema "users" do
    has_one :details, Details
  end
end

defmodule Details do
  use Ecto.Schema

  schema "users_details" do
    belongs_to :user, User
  end
end

You could load all the users with its respective details the following way (wrong way)

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 statement.

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.

config :my_app, MyApp.Repo, loggers: [
  {OnePlusNDetector, :analyze, []},
  {Ecto.LogEntry, :log, []}
]

Documentation

Documentation can be found at https://hexdocs.pm/one_plus_n_detector.

Installation

This library should be used only on development

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 for the full license text.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].