Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the filtering query, and indexes needed #1504

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ defmodule NervesHub.Devices do
|> Repo.paginate(pagination)
end

def filter(product_id, opts) do
pagination = Map.get(opts, :pagination, %{})
sorting = Map.get(opts, :sort, {:asc, :identifier})
filters = Map.get(opts, :filters, %{})

Device
|> where([d], d.product_id == ^product_id)
|> Repo.exclude_deleted()
|> filtering(filters)
|> order_by(^sort_devices(sorting))
|> Repo.paginate(pagination)
end

def get_health_by_org_id_and_product_id(org_id, product_id, opts) do
query =
from(
Expand Down Expand Up @@ -163,7 +176,7 @@ defmodule NervesHub.Devices do
where(
query,
[d],
fragment("array_to_string(?, ',') ILIKE ?", d.tags, ^"%#{tag}%")
fragment("array_to_string(?, ' ', ' ') ILIKE ?", d.tags, ^"%#{tag}%")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does adding a trailing space do here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this isn't using the string_array_to_string function that we're creating in the migration it's probably not going to use that index.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I get it. A third value added 🤦🏼

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this @esvinson

)
end)

Expand Down
4 changes: 2 additions & 2 deletions lib/nerves_hub_web/live/devices/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
end

defp assign_display_devices(
%{assigns: %{org: org, product: product, paginate_opts: paginate_opts}} = socket
%{assigns: %{product: product, paginate_opts: paginate_opts}} = socket
) do
opts = %{
pagination: %{page: paginate_opts.page_number, page_size: paginate_opts.page_size},
Expand All @@ -353,7 +353,7 @@ defmodule NervesHubWeb.Live.Devices.Index do
filters: socket.assigns.current_filters
}

page = Devices.get_devices_by_org_id_and_product_id(org.id, product.id, opts)
page = Devices.filter(product.id, opts)

statuses =
Enum.into(page.entries, %{}, fn device ->
Expand Down
20 changes: 20 additions & 0 deletions priv/repo/migrations/20240910012345_enable_pg_trgm_extension.exs
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"
oestrich marked this conversation as resolved.
Show resolved Hide resolved
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 priv/repo/migrations/20240910054628_add_indexes_to_devices.exs
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
oestrich marked this conversation as resolved.
Show resolved Hide resolved

def change do
create index("devices", [:product_id], concurrently: true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional note, if we create the indexes using create_if_not_exists we can create some of the indexes in advance to help with performance.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea!

create index("devices", [:updates_enabled], concurrently: true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indexes on boolean fields probably won't be used.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My general thought was "index more stuff, see what is used, remove indexes as we see fit"

This was some general advice from some Postgres contributors at Heroku that I was friendly with.

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
Loading