Skip to content

Add index for searching environments name without type

Pam Artiaga requested to merge 427537-add-index-for-ltrim-search-function into master

What does this MR do and why?

Adding an index for the change in Fix search within Environments folder (!146154 - merged).

The old query has a custom index introduced here: !102227 (diffs)

So we are adding a custom index for the new query as well since it is recommended by Database Labs.

Before (Old Query)

SELECT "environments".* FROM "environments" 
WHERE "environments"."project_id" = 49114813 
AND (LOWER(ltrim(environments.name, environments.environment_type || '/')) LIKE LOWER('podtato') || '%') LIMIT 5

After (New Query)

SELECT "environments".* FROM "environments" 
WHERE "environments"."project_id" = 49114813 
AND (LOWER(ltrim(ltrim(environments.name, environments.environment_type), '/')) LIKE LOWER('podtato') || '%') LIMIT 5

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

DB migration output

$ bundle exec rails db:migrate
main: == [advisory_lock_connection] object_id: 119420, pg_backend_pid: 14196
main: == 20240229223236 AddIndexEnvironmentsNameWithoutType: migrating ==============
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- view_exists?(:postgres_partitions)
main:    -> 0.0147s
main: -- index_exists?(:environments, "project_id, lower(ltrim(ltrim(name, environment_type), '/')) varchar_pattern_ops, state", {:name=>"index_environments_name_without_type", :algorithm=>:concurrently})
main:    -> 0.0043s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0002s
main: -- add_index(:environments, "project_id, lower(ltrim(ltrim(name, environment_type), '/')) varchar_pattern_ops, state", {:name=>"index_environments_name_without_type", :algorithm=>:concurrently})
main:    -> 0.0027s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: == 20240229223236 AddIndexEnvironmentsNameWithoutType: migrated (0.0336s) =====

main: == [advisory_lock_connection] object_id: 119420, pg_backend_pid: 14196

How to set up and validate locally

Run the migration:

bundle exec rails db:migrate

Related to #427537 (closed)

Edited by Pam Artiaga

Merge request reports