-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the filtering query, and indexes needed
The `pg_trgm` bits have been inspired by: - https://stackoverflow.com/questions/32951793/how-to-index-a-varchar-array-in-postgresql-using-array-to-string/33016333#33016333 - https://stackoverflow.com/questions/39480580/how-to-index-a-string-array-column-for-pg-trgm-term-any-array-column-que
- Loading branch information
Showing
4 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
priv/repo/migrations/20240910012345_enable_pg_trgm_extension.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
defmodule NervesHub.Repo.Migrations.EnablePgTrgmExtension do | ||
use Ecto.Migration | ||
|
||
def change do | ||
execute "CREATE EXTENSION pg_trgm", "DROP EXTENSION pg_trgm" | ||
execute(&add_function/0, &drop_function/0) | ||
end | ||
|
||
defp add_function do | ||
repo().query!(""" | ||
create function string_array_to_string(text[], text, text) returns text as $$ | ||
select array_to_string($1, $2, $3) | ||
$$ language sql cost 1 immutable; | ||
""", [], [log: :info]) | ||
end | ||
|
||
defp drop_function do | ||
repo().query!("drop function string_array_to_string(text[], text, text);", [], [log: :info]) | ||
end | ||
end |
13 changes: 13 additions & 0 deletions
13
priv/repo/migrations/20240910054628_add_indexes_to_devices.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule NervesHub.Repo.Migrations.AddIndexesToDevices do | ||
use Ecto.Migration | ||
@disable_ddl_transaction true | ||
|
||
def change do | ||
create index("devices", [:product_id], concurrently: true) | ||
create index("devices", [:updates_enabled], concurrently: true) | ||
create index("devices", ["(firmware_metadata->'version')"], name: :devices_firmware_index, using: "GIN", concurrently: true) | ||
create index("devices", ["(firmware_metadata->'platform')"], name: :devices_platform_index, using: "GIN", concurrently: true) | ||
create index("devices", ["string_array_to_string(tags, ' ', ' ') gin_trgm_ops"], name: :devices_tags_index, using: "GIN", concurrently: true) | ||
create index("devices", ["identifier gin_trgm_ops"], name: :devices_identifier_trgm_index, using: "GIN", concurrently: true) | ||
end | ||
end |