-
Notifications
You must be signed in to change notification settings - Fork 70
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea! |
||
create index("devices", [:updates_enabled], concurrently: true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indexes on boolean fields probably won't be used. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 🤦🏼
There was a problem hiding this comment.
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