feat(perf): contacts query performance (#7175)
* feat: use more indexable where condition * feat: add index concurrently * chore: update schema * refactor: update index name
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
# index_contacts_on_account_id (account_id)
|
# index_contacts_on_account_id (account_id)
|
||||||
# index_contacts_on_lower_email_account_id (lower((email)::text), account_id)
|
# index_contacts_on_lower_email_account_id (lower((email)::text), account_id)
|
||||||
# index_contacts_on_name_email_phone_number_identifier (name,email,phone_number,identifier) USING gin
|
# index_contacts_on_name_email_phone_number_identifier (name,email,phone_number,identifier) USING gin
|
||||||
|
# index_contacts_on_identifiable_fields (account_id,email,phone_number,identifier) WHERE (((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text)) # rubocop:disable Layout/LineLength
|
||||||
# index_contacts_on_phone_number_and_account_id (phone_number,account_id)
|
# index_contacts_on_phone_number_and_account_id (phone_number,account_id)
|
||||||
# uniq_email_per_account_contact (email,account_id) UNIQUE
|
# uniq_email_per_account_contact (email,account_id) UNIQUE
|
||||||
# uniq_identifier_per_account_contact (identifier,account_id) UNIQUE
|
# uniq_identifier_per_account_contact (identifier,account_id) UNIQUE
|
||||||
@@ -136,7 +137,7 @@ class Contact < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.resolved_contacts
|
def self.resolved_contacts
|
||||||
where("COALESCE(NULLIF(contacts.email,''),NULLIF(contacts.phone_number,''),NULLIF(contacts.identifier,'')) IS NOT NULL")
|
where("contacts.email <> '' OR contacts.phone_number <> '' OR contacts.identifier <> ''")
|
||||||
end
|
end
|
||||||
|
|
||||||
def discard_invalid_attrs
|
def discard_invalid_attrs
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
class AddPartialIndexForResolvedContacts < ActiveRecord::Migration[7.0]
|
||||||
|
disable_ddl_transaction!
|
||||||
|
|
||||||
|
def change
|
||||||
|
add_index :contacts, [:account_id, :email, :phone_number, :identifier], where: "(email <> '' OR phone_number <> '' OR identifier <> '')",
|
||||||
|
name: 'index_contacts_on_nonempty_fields', algorithm: :concurrently
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.0].define(version: 2023_05_15_051424) do
|
ActiveRecord::Schema[7.0].define(version: 2023_05_23_104139) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pg_stat_statements"
|
enable_extension "pg_stat_statements"
|
||||||
enable_extension "pg_trgm"
|
enable_extension "pg_trgm"
|
||||||
@@ -405,6 +405,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_05_15_051424) do
|
|||||||
t.jsonb "custom_attributes", default: {}
|
t.jsonb "custom_attributes", default: {}
|
||||||
t.datetime "last_activity_at", precision: nil
|
t.datetime "last_activity_at", precision: nil
|
||||||
t.index "lower((email)::text), account_id", name: "index_contacts_on_lower_email_account_id"
|
t.index "lower((email)::text), account_id", name: "index_contacts_on_lower_email_account_id"
|
||||||
|
t.index ["account_id", "email", "phone_number", "identifier"], name: "index_contacts_on_nonempty_fields", where: "(((email)::text <> ''::text) OR ((phone_number)::text <> ''::text) OR ((identifier)::text <> ''::text))"
|
||||||
t.index ["account_id"], name: "index_contacts_on_account_id"
|
t.index ["account_id"], name: "index_contacts_on_account_id"
|
||||||
t.index ["email", "account_id"], name: "uniq_email_per_account_contact", unique: true
|
t.index ["email", "account_id"], name: "uniq_email_per_account_contact", unique: true
|
||||||
t.index ["identifier", "account_id"], name: "uniq_identifier_per_account_contact", unique: true
|
t.index ["identifier", "account_id"], name: "uniq_identifier_per_account_contact", unique: true
|
||||||
|
|||||||
Reference in New Issue
Block a user