-
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
Conversation
6468871
to
ba3e7b7
Compare
Looks very nice! |
Thats a fair question. I've tested it locally, and the filters work as expected, but the test suite should be expanded. |
Leaving a note post close because I'm a little concerned with adding in 6 indexes on a table that gets hammered with heartbeat updates. Something to look out for once this gets deployed at smartrent @jjcarstens |
Yeah, that's a valid concern. I would imagine it won't have to touch indexes unless the indexed value actually changes? |
@@ -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}%") |
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
|
||
def change do | ||
create index("devices", [:product_id], concurrently: true) | ||
create index("devices", [:updates_enabled], concurrently: true) |
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.
Indexes on boolean fields probably won't be used.
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.
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.
@disable_ddl_transaction true | ||
|
||
def change do | ||
create index("devices", [:product_id], concurrently: true) |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea!
As indexes are separate, this should be fine. It would only be a concern if the indexed data is updated often. |
A few things came up while looking into #1501
This PR simplifies the filter query, adds indexes, including some
pg_trgm
goodness.The
pg_trgm
bits have been inspired by: