feat: add trigram index to tags (#6615)

Fixes: chatwoot/product#796

This migration adds a trigram index to the tags table on the name column. This is used to speed up the search for tags by name, this is a heavy sub-query when generating reports where labels are involved.

Trigram indexes are used to speed up LIKE queries; they are not used for equality queries. This is because the index is not a btree index, it is a GIN index. This means that the index is not ordered and so cannot be used for equality queries.

Read more: https://www.postgresql.org/docs/current/pgtrgm.html

The index is created concurrently: https://thoughtbot.com/blog/how-to-create-postgres-indexes-concurrently-in
---------

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Shivam Mishra
2023-03-08 18:11:07 +05:30
committed by GitHub
parent d59fd6b747
commit 0fe05dc1b9
2 changed files with 43 additions and 21 deletions

View File

@@ -0,0 +1,21 @@
# This migration adds a trigram index to the tags table on the name column.
# This is used to speed up the search for tags by name, this is a heavy
# sub query when genereating reports where labels are involved.
#
# Trigram indexes are used to speed up LIKE queries, they are not used for
# equality queries. This is because the index is not a btree index, it is a
# GIN index. This means that the index is not ordered, and so cannot be used
# for equality queries.
#
# Read more: https://www.postgresql.org/docs/current/pgtrgm.html
class AddTrgmIndexToTagName < ActiveRecord::Migration[6.1]
# disabling ddl transaction is required for concurrent index creation
# https://thoughtbot.com/blog/how-to-create-postgres-indexes-concurrently-in
disable_ddl_transaction!
def change
add_index :tags, 'LOWER(name) gin_trgm_ops', using: :gin, name: 'tags_name_trgm_idx', algorithm: :concurrently
# resolves to CREATE INDEX tags_name_trgm_idx ON public.tags USING gin (lower((name)::text) gin_trgm_ops);
end
end