From 8a70629e1fa7ed07fd53bed250e7f627bf937b38 Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Thu, 2 Nov 2017 08:47:05 +0100 Subject: [PATCH 1/2] Fix syntax error --- priv/repo/seeds.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index b89e2d7..da430b7 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -9,7 +9,7 @@ for i <- 1..1_000_000 do selected_roles = Enum.take_random(roles, 2) settings = if rem(i, 250_000) == 0 do %{ - roles: ["admin", Enum.random(selected_roles)] + roles: ["admin", Enum.random(selected_roles)], loyalties: :rand.uniform(1_000), providers: selected_providers, personal_info: "Gigi" From 2c5575decaa13961c5e7cc89d2308b1b562c1adc Mon Sep 17 00:00:00 2001 From: Mario Uher Date: Thu, 2 Nov 2017 08:48:06 +0100 Subject: [PATCH 2/2] Format code with mix format --- config/config.exs | 5 +- config/test.exs | 5 +- lib/application.ex | 2 +- lib/models/user.ex | 10 ++-- .../20171029173051_create_users.exs | 20 ++++--- priv/repo/search.exs | 52 +++++++++---------- priv/repo/seeds.exs | 52 ++++++++++++------- 7 files changed, 81 insertions(+), 65 deletions(-) diff --git a/config/config.exs b/config/config.exs index 6c733e3..02b7f03 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1,6 +1,5 @@ use Mix.Config -config :ecto_jsonb_example, - ecto_repos: [EctoJsonbExample.Repo] +config :ecto_jsonb_example, ecto_repos: [EctoJsonbExample.Repo] -import_config "#{Mix.env}.exs" +import_config "#{Mix.env()}.exs" diff --git a/config/test.exs b/config/test.exs index 2e06044..15737d8 100644 --- a/config/test.exs +++ b/config/test.exs @@ -11,5 +11,6 @@ config :ecto_jsonb_example, EctoJsonbExample.Repo, hostname: "localhost", pool: Ecto.Adapters.SQL.Sandbox, ownership_timeout: 300_000 - # loggers: [{Ecto.LogEntry, :log, [:info]}], - # log_level: :debug + +# loggers: [{Ecto.LogEntry, :log, [:info]}], +# log_level: :debug diff --git a/lib/application.ex b/lib/application.ex index f385ddc..10f2c3c 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -7,7 +7,7 @@ defmodule EctoJsonbExample.Application do import Supervisor.Spec, warn: false children = [ - supervisor(EctoJsonbExample.Repo, []) + supervisor(EctoJsonbExample.Repo, []) ] opts = [strategy: :one_for_one, name: EctoJsonbExample.Supervisor] diff --git a/lib/models/user.ex b/lib/models/user.ex index 19ffd73..7c694ae 100644 --- a/lib/models/user.ex +++ b/lib/models/user.ex @@ -2,10 +2,10 @@ defmodule EctoJsonbExample.User do use Ecto.Schema schema "users" do - field :name, :string - field :settings, :map - field :settings_index, :map - field :acls, {:array, :integer} - field :acls_index, {:array, :integer} + field(:name, :string) + field(:settings, :map) + field(:settings_index, :map) + field(:acls, {:array, :integer}) + field(:acls_index, {:array, :integer}) end end diff --git a/priv/repo/migrations/20171029173051_create_users.exs b/priv/repo/migrations/20171029173051_create_users.exs index 07199c6..3ea937e 100644 --- a/priv/repo/migrations/20171029173051_create_users.exs +++ b/priv/repo/migrations/20171029173051_create_users.exs @@ -3,15 +3,21 @@ defmodule EctoJsonbExample.Repo.Migrations.CreateUsers do def up do create table("users") do - add :name, :string, null: false - add :settings, :jsonb, null: false, default: "{}" - add :settings_index, :jsonb, null: false, default: "{}" - add :acls, :jsonb, null: false, default: "[]" - add :acls_index, :jsonb, null: false, default: "[]" + add(:name, :string, null: false) + add(:settings, :jsonb, null: false, default: "{}") + add(:settings_index, :jsonb, null: false, default: "{}") + add(:acls, :jsonb, null: false, default: "[]") + add(:acls_index, :jsonb, null: false, default: "[]") end - execute("CREATE INDEX users_settings_index_providers ON users USING GIN ((settings_index->'providers'))") - execute("CREATE INDEX users_settings_index_roles ON users USING GIN ((settings_index->'roles'))") + execute( + "CREATE INDEX users_settings_index_providers ON users USING GIN ((settings_index->'providers'))" + ) + + execute( + "CREATE INDEX users_settings_index_roles ON users USING GIN ((settings_index->'roles'))" + ) + execute("CREATE INDEX users_acls_index ON users USING GIN ((acls_index))") end end diff --git a/priv/repo/search.exs b/priv/repo/search.exs index 891acf7..b61f116 100644 --- a/priv/repo/search.exs +++ b/priv/repo/search.exs @@ -2,50 +2,48 @@ alias EctoJsonbExample.Repo alias EctoJsonbExample.User import Ecto.Query, only: [from: 2] -IO.puts "Select users with admin role" -query = from u in User, - where: fragment("(settings_index->'roles')::jsonb \\? ?", "admin") +IO.puts("Select users with admin role") +query = from(u in User, where: fragment("(settings_index->'roles')::jsonb \\? ?", "admin")) result = Repo.all(query) -IO.inspect result +IO.inspect(result) -IO.puts "Select users by acls" -query = from u in User, - where: fragment("acls_index @> ?", ^[101]) +IO.puts("Select users by acls") +query = from(u in User, where: fragment("acls_index @> ?", ^[101])) roles_result = Repo.all(query) -IO.inspect roles_result +IO.inspect(roles_result) -IO.puts "Select users by loyalty" -query = from u in User, - where: fragment("(settings_index)::jsonb @> ?::jsonb", ^%{loyalties: 101}) +IO.puts("Select users by loyalty") + +query = + from(u in User, where: fragment("(settings_index)::jsonb @> ?::jsonb", ^%{loyalties: 101})) result = Repo.all(query) -IO.inspect result +IO.inspect(result) -IO.puts "Select users - all with key personal_info" -query = from u in User, - where: fragment("(settings_index)::jsonb \\? ?", "personal_info") +IO.puts("Select users - all with key personal_info") +query = from(u in User, where: fragment("(settings_index)::jsonb \\? ?", "personal_info")) result = Repo.all(query) -IO.inspect result +IO.inspect(result) + +IO.puts("Roles search benchmark with index vs without index") -IO.puts "Roles search benchmark with index vs without index" index_role_query = fn -> - query = from u in User, - where: fragment("(settings_index->'roles')::jsonb \\? ?", "admin") + query = from(u in User, where: fragment("(settings_index->'roles')::jsonb \\? ?", "admin")) Repo.all(query) end no_index_role_query = fn -> - query = from u in User, - where: fragment("(settings->'roles')::jsonb \\? ?", "admin") + query = from(u in User, where: fragment("(settings->'roles')::jsonb \\? ?", "admin")) Repo.all(query) end -Benchee.run(%{ - "with index" => index_role_query, - "without index" => no_index_role_query -}, time: 10) - - +Benchee.run( + %{ + "with index" => index_role_query, + "without index" => no_index_role_query + }, + time: 10 +) diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index da430b7..22c77d3 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -2,36 +2,48 @@ alias EctoJsonbExample.Repo alias EctoJsonbExample.User roles = ["user", "driver", "manager", "support", "technician", "guest"] -providers = ["facebook", "google", "twitter", "github", "foursquare", "dropbox", "linkedin", "vimeo"] + +providers = [ + "facebook", + "google", + "twitter", + "github", + "foursquare", + "dropbox", + "linkedin", + "vimeo" +] for i <- 1..1_000_000 do selected_providers = Enum.take_random(providers, 2) selected_roles = Enum.take_random(roles, 2) - settings = if rem(i, 250_000) == 0 do - %{ - roles: ["admin", Enum.random(selected_roles)], - loyalties: :rand.uniform(1_000), - providers: selected_providers, - personal_info: "Gigi" - } - else - %{ - roles: selected_roles, - loyalties: :rand.uniform(1_000), - providers: selected_providers - } - end + + settings = + if rem(i, 250_000) == 0 do + %{ + roles: ["admin", Enum.random(selected_roles)], + loyalties: :rand.uniform(1000), + providers: selected_providers, + personal_info: "Gigi" + } + else + %{ + roles: selected_roles, + loyalties: :rand.uniform(1000), + providers: selected_providers + } + end acls = for _ <- 1..4, do: :rand.uniform(100) acls = if rem(i, 250_000) == 0, do: acls ++ [101], else: acls - - Repo.insert! %User{ - name: Faker.Name.name, + Repo.insert!(%User{ + name: Faker.Name.name(), settings: settings, settings_index: settings, acls: acls, acls_index: acls - } - IO.puts "inserting user #{i}/1_000_000" + }) + + IO.puts("inserting user #{i}/1_000_000") end