-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
By using the btree_gin extension and merging the indexes on the various columns into a single index combined with state, debate_state and parliament_id for archived petitions we can achieve a significant performance gain of more than 10x when searching arbitrary text. This also adds in the petition id to the index so people can now search by that on the front end - it will even return cases where people refer to the petition by id in other petition text.
- Loading branch information
Showing
18 changed files
with
345 additions
and
68 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
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
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
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
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
99 changes: 99 additions & 0 deletions
99
db/migrate/20210422053757_optimize_free_text_search_indexes.rb
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,99 @@ | ||
class OptimizeFreeTextSearchIndexes < ActiveRecord::Migration[6.1] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
execute "CREATE EXTENSION IF NOT EXISTS btree_gin" | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_petitions_for_search | ||
ON petitions USING gin(( | ||
to_tsvector('english', id::text) || | ||
to_tsvector('english', action::text) || | ||
to_tsvector('english', background::text) || | ||
to_tsvector('english', COALESCE(additional_details, '')::text)), | ||
state, debate_state | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_archived_petitions_for_search | ||
ON archived_petitions USING gin(( | ||
to_tsvector('english', id::text) || | ||
to_tsvector('english', action::text) || | ||
to_tsvector('english', background::text) || | ||
to_tsvector('english', COALESCE(additional_details, '')::text)), | ||
state, parliament_id, debate_state | ||
); | ||
SQL | ||
|
||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_petitions_on_action" | ||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_petitions_on_background" | ||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_petitions_on_additional_details" | ||
execute "ANALYZE petitions" | ||
|
||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_archived_petitions_on_action" | ||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_archived_petitions_on_background" | ||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_archived_petitions_on_additional_details" | ||
execute "ANALYZE archived_petitions" | ||
end | ||
|
||
def down | ||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_petitions_on_action | ||
ON petitions USING gin( | ||
to_tsvector('english', action) | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_petitions_on_background | ||
ON petitions USING gin( | ||
to_tsvector('english', background) | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_petitions_on_additional_details | ||
ON petitions USING gin( | ||
to_tsvector('english', additional_details) | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_archived_petitions_on_action | ||
ON archived_petitions USING gin( | ||
to_tsvector('english', action) | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_archived_petitions_on_background | ||
ON archived_petitions USING gin( | ||
to_tsvector('english', background) | ||
); | ||
SQL | ||
|
||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_archived_petitions_on_additional_details | ||
ON archived_petitions USING gin( | ||
to_tsvector('english', additional_details) | ||
); | ||
SQL | ||
|
||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_petitions_for_search" | ||
execute "ANALYZE petitions" | ||
|
||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_archived_petitions_for_search" | ||
execute "ANALYZE archived_petitions" | ||
|
||
execute "DROP EXTENSION IF EXISTS btree_gin" | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
db/migrate/20210425143147_optimize_search_indexes_for_departments.rb
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,21 @@ | ||
class OptimizeSearchIndexesForDepartments < ActiveRecord::Migration[6.1] | ||
disable_ddl_transaction! | ||
|
||
def up | ||
execute <<~SQL | ||
CREATE INDEX CONCURRENTLY IF NOT EXISTS | ||
index_departments_for_search | ||
ON departments USING gin(( | ||
to_tsvector('english', name::text) || | ||
to_tsvector('english', COALESCE(acronym, '')::text) | ||
)); | ||
SQL | ||
|
||
execute "ANALYZE departments" | ||
end | ||
|
||
def down | ||
execute "DROP INDEX CONCURRENTLY IF EXISTS index_departments_for_search" | ||
execute "ANALYZE departments" | ||
end | ||
end |
Oops, something went wrong.