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 <cody@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Brian Palmer <brianp@instructure.com>
QA-Review: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Jeremy Stanley 2013-08-23 14:58:17 -06:00 committed by Brian Palmer
parent 1d704e4893
commit e60662fca0
4 changed files with 19 additions and 91 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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