From e60662fca00feadeb76728f53791b8337bb5c5a2 Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Fri, 23 Aug 2013 14:58:17 -0600 Subject: [PATCH] remove unhelpful gist indexes for api searches postgres is using the indexes to search all objects' titles (in any context), and then filtering the results by context, whereas it's a lot faster to use the existing index to filter by context first and do a linear search through these results. for example, the wiki sidebar has done this for years (searching a course's attachments for those whose mime type matches "image/%") test plan: - api searches should not be painfully slow Change-Id: I6a4cd1f64b20bcccea6fe4d56ec78298d26e7b7d Reviewed-on: https://gerrit.instructure.com/23691 Reviewed-by: Cody Cutrer Tested-by: Jenkins Product-Review: Brian Palmer QA-Review: Brian Palmer --- ...651_add_gist_index_for_wiki_page_search.rb | 27 --------------- ...3170139_add_gist_indexes_for_api_search.rb | 33 ------------------- ..._additional_gist_indexes_for_api_search.rb | 31 ----------------- ...0823204503_remove_unneeded_gist_indexes.rb | 19 +++++++++++ 4 files changed, 19 insertions(+), 91 deletions(-) delete mode 100644 db/migrate/20130709155651_add_gist_index_for_wiki_page_search.rb delete mode 100644 db/migrate/20130723170139_add_gist_indexes_for_api_search.rb delete mode 100644 db/migrate/20130729175222_add_additional_gist_indexes_for_api_search.rb create mode 100644 db/migrate/20130823204503_remove_unneeded_gist_indexes.rb diff --git a/db/migrate/20130709155651_add_gist_index_for_wiki_page_search.rb b/db/migrate/20130709155651_add_gist_index_for_wiki_page_search.rb deleted file mode 100644 index 33b989a8f4f..00000000000 --- a/db/migrate/20130709155651_add_gist_index_for_wiki_page_search.rb +++ /dev/null @@ -1,27 +0,0 @@ -class AddGistIndexForWikiPageSearch < ActiveRecord::Migration - self.transactional = false - tag :predeploy - - def self.up - if is_postgres? - connection.transaction(:requires_new => true) do - begin - execute('create extension if not exists pg_trgm;') - rescue ActiveRecord::StatementInvalid - raise ActiveRecord::Rollback - end - end - - if has_postgres_proc?('show_trgm') - concurrently = " CONCURRENTLY" if connection.open_transactions == 0 - execute("create index#{concurrently} index_trgm_wiki_pages_title on wiki_pages USING gist(lower(title) gist_trgm_ops);") - end - end - end - - def self.down - if is_postgres? - execute('drop index if exists index_trgm_wiki_pages_title;') - end - end -end diff --git a/db/migrate/20130723170139_add_gist_indexes_for_api_search.rb b/db/migrate/20130723170139_add_gist_indexes_for_api_search.rb deleted file mode 100644 index 5cf0da465f0..00000000000 --- a/db/migrate/20130723170139_add_gist_indexes_for_api_search.rb +++ /dev/null @@ -1,33 +0,0 @@ -class AddGistIndexesForApiSearch < ActiveRecord::Migration - self.transactional = false - tag :postdeploy - - def self.up - if is_postgres? - connection.transaction(:requires_new => true) do - begin - execute('create extension if not exists pg_trgm;') - rescue ActiveRecord::StatementInvalid - raise ActiveRecord::Rollback - end - end - - if has_postgres_proc?('show_trgm') - concurrently = " CONCURRENTLY" if connection.open_transactions == 0 - execute("create index#{concurrently} index_trgm_context_external_tools_name on context_external_tools USING gist(lower(name) gist_trgm_ops);") - execute("create index#{concurrently} index_trgm_assignments_title on assignments USING gist(lower(title) gist_trgm_ops);") - execute("create index#{concurrently} index_trgm_quizzes_title on quizzes USING gist(lower(title) gist_trgm_ops);") - execute("create index#{concurrently} index_trgm_discussion_topics_title on discussion_topics USING gist(lower(title) gist_trgm_ops);") - end - end - end - - def self.down - if is_postgres? - execute('drop index if exists index_trgm_context_external_tools_name;') - execute('drop index if exists index_trgm_assignments_title;') - execute('drop index if exists index_trgm_quizzes_title;') - execute('drop index if exists index_trgm_discussion_topics_title;') - end - end -end diff --git a/db/migrate/20130729175222_add_additional_gist_indexes_for_api_search.rb b/db/migrate/20130729175222_add_additional_gist_indexes_for_api_search.rb deleted file mode 100644 index c66811234e3..00000000000 --- a/db/migrate/20130729175222_add_additional_gist_indexes_for_api_search.rb +++ /dev/null @@ -1,31 +0,0 @@ -class AddAdditionalGistIndexesForApiSearch < ActiveRecord::Migration - self.transactional = false - tag :postdeploy - - def self.up - if is_postgres? - connection.transaction(:requires_new => true) do - begin - execute('create extension if not exists pg_trgm;') - rescue ActiveRecord::StatementInvalid - raise ActiveRecord::Rollback - end - end - - if has_postgres_proc?('show_trgm') - concurrently = " CONCURRENTLY" if connection.open_transactions == 0 - execute("create index#{concurrently} index_trgm_attachments_display_name on attachments USING gist(lower(display_name) gist_trgm_ops);") - execute("create index#{concurrently} index_trgm_context_modules_name on context_modules USING gist(lower(name) gist_trgm_ops);") - execute("create index#{concurrently} index_trgm_content_tags_title on content_tags USING gist(lower(title) gist_trgm_ops);") - end - end - end - - def self.down - if is_postgres? - execute('drop index if exists index_trgm_attachments_display_name;') - execute('drop index if exists index_trgm_context_modules_name;') - execute('drop index if exists index_trgm_content_tags_title;') - end - end -end diff --git a/db/migrate/20130823204503_remove_unneeded_gist_indexes.rb b/db/migrate/20130823204503_remove_unneeded_gist_indexes.rb new file mode 100644 index 00000000000..c923eaad334 --- /dev/null +++ b/db/migrate/20130823204503_remove_unneeded_gist_indexes.rb @@ -0,0 +1,19 @@ +class RemoveUnneededGistIndexes < ActiveRecord::Migration + tag :predeploy + + def self.up + if is_postgres? + execute('drop index if exists index_trgm_wiki_pages_title;') + execute('drop index if exists index_trgm_context_external_tools_name;') + execute('drop index if exists index_trgm_assignments_title;') + execute('drop index if exists index_trgm_quizzes_title;') + execute('drop index if exists index_trgm_discussion_topics_title;') + execute('drop index if exists index_trgm_attachments_display_name;') + execute('drop index if exists index_trgm_context_modules_name;') + execute('drop index if exists index_trgm_content_tags_title;') + end + end + + def self.down + end +end