{"id":15654978,"url":"https://github.com/san650/one_plus_n_detector","last_synced_at":"2025-05-01T00:55:56.780Z","repository":{"id":62430162,"uuid":"141939983","full_name":"san650/one_plus_n_detector","owner":"san650","description":"Elixir library to help you detect 1+n queries in applications using Ecto","archived":false,"fork":false,"pushed_at":"2022-12-09T09:18:29.000Z","size":11,"stargazers_count":28,"open_issues_count":1,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-01T00:55:42.299Z","etag":null,"topics":["ecto","elixir","phoenix","sql"],"latest_commit_sha":null,"homepage":null,"language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/san650.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-22T23:53:38.000Z","updated_at":"2024-09-12T03:04:22.000Z","dependencies_parsed_at":"2023-01-25T21:00:46.179Z","dependency_job_id":null,"html_url":"https://github.com/san650/one_plus_n_detector","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/san650%2Fone_plus_n_detector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/san650%2Fone_plus_n_detector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/san650%2Fone_plus_n_detector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/san650%2Fone_plus_n_detector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/san650","download_url":"https://codeload.github.com/san650/one_plus_n_detector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251806212,"owners_count":21646843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecto","elixir","phoenix","sql"],"created_at":"2024-10-03T12:55:18.163Z","updated_at":"2025-05-01T00:55:56.756Z","avatar_url":"https://github.com/san650.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# OnePlusNDetector\n\nThis library helps detect 1+n SQL queries in your application. This is a common problem when working with ORMs like Ecto.\n\n## What is the 1+n problem?\n\nWhen 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.\n\nLet's say you have the following Ecto schemas\n\n```elixir\ndefmodule User do\n  use Ecto.Schema\n\n  schema \"users\" do\n    has_one :details, Details\n  end\nend\n\ndefmodule Details do\n  use Ecto.Schema\n\n  schema \"users_details\" do\n    belongs_to :user, User\n  end\nend\n```\n\nYou could load all the users with its respective details the following way (**wrong way**)\n\n```elixir\nUser\n|\u003e Repo.all\n|\u003e Enum.map(\u0026 %{\u00261 | details: Repo.get(\u00261.details_id)})\n```\n\nYou 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.\n\nThe number of queries can be reduced to two by just using a [preload](https://hexdocs.pm/ecto/Ecto.Query.html#preload/3) statement.\n\n```elixir\nUser\n|\u003e preload(:details)\n|\u003e Repo.all\n```\n\nThis library helps you detect these cases by triggering a warning in your query log.\n\n## Usage\n\nAdd an extra logger to Ecto repo in development.\n\n```elixir\nconfig :my_app, MyApp.Repo, loggers: [\n  {OnePlusNDetector, :analyze, []},\n  {Ecto.LogEntry, :log, []}\n]\n```\n\n## Documentation\n\nDocumentation can be found at [https://hexdocs.pm/one_plus_n_detector](https://hexdocs.pm/one_plus_n_detector).\n\n## Installation\n\n**This library should be used only on development**\n\n```elixir\ndef deps do\n  [\n    {:one_plus_n_detector, \"~\u003e 0.1.0\", only: :dev}\n  ]\nend\n```\n\nRemember to **not add** `:one_plus_n_detector` to your applications list!\n\n## License\n\none_plus_n_detector is licensed under the MIT license.\n\nSee [LICENSE](./LICENSE) for the full license text.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsan650%2Fone_plus_n_detector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsan650%2Fone_plus_n_detector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsan650%2Fone_plus_n_detector/lists"}