From: lain Date: Sat, 19 Jan 2019 23:31:17 +0000 (+0100) Subject: Make use of the indices. X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=b108aeee082949e2e534f8bc406fdacb8924803d;p=akkoma Make use of the indices. Indices in postgresql rely on operators, so they won't be used if you use only functions. --- diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index eb4218ebe..87815e11c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -734,7 +734,16 @@ defmodule Pleroma.User do ^processed_query ) }, - where: not is_nil(u.nickname) + where: + fragment( + """ + (setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') || + setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')) @@ to_tsquery('simple', ?) + """, + u.nickname, + u.name, + ^processed_query + ) ) end @@ -750,7 +759,7 @@ defmodule Pleroma.User do u.name ) }, - where: not is_nil(u.nickname) + where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^query) ) end diff --git a/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs b/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs index 4f7712eb0..b4e8c984c 100644 --- a/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs +++ b/priv/repo/migrations/20190118074940_fix_user_trigram_index.exs @@ -2,12 +2,21 @@ defmodule Pleroma.Repo.Migrations.FixUserTrigramIndex do use Ecto.Migration def up do - drop_if_exists index(:users, [], name: :users_trigram_index) - create index(:users, ["(trim(nickname || ' ' || name)) gist_trgm_ops"], name: :users_trigram_index, using: :gist) + drop_if_exists(index(:users, [], name: :users_trigram_index)) + + create( + index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"], + name: :users_trigram_index, + using: :gist + ) + ) end def down do - drop_if_exists index(:users, [], name: :users_trigram_index) - create index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist) + drop_if_exists(index(:users, [], name: :users_trigram_index)) + + create( + index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist) + ) end end