Last active
July 23, 2020 09:31
-
-
Save mdesantis/ce8100f617073a09dd0e96f3ce3a7cc8 to your computer and use it in GitHub Desktop.
Rails migration adding PostgreSQL index for LIKE when the pattern is both left-anchored and right-anchored
This file contains hidden or 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
# It supports queries like: | |
# ModelName.where(ModelName.arel_table[:columnname].lower.matches 'searchterm') | |
class PostgresqlLikeIIndex < ActiveRecord::Migration | |
def up | |
enable_extension 'pg_trgm' | |
execute <<~SQL | |
CREATE INDEX index_tablename_on_columnname_lower | |
ON tablename USING gin (lower(columnname) gin_trgm_ops) | |
SQL | |
end | |
def down | |
execute 'DROP INDEX index_tablename_on_columnname_lower' | |
disable_extension 'pg_trgm' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LIKE / ILIKE indexing: http://web.archive.org/web/20200723085756/https://www.cybertec-postgresql.com/en/postgresql-more-performance-for-like-and-ilike-statements/