--ratings-table-idx
CREATE INDEX idx_ratings_user_id_updated_at
    ON ratings (user_id, updated_at);

--clients-table-idx
CREATE INDEX idx_clients_project_id_name_crm_client_id
    ON clients (project_id, name, crm_client_id);

CREATE INDEX idx_clients_project_id_lower_name_crm_client_id
    ON clients (project_id, LOWER(name), crm_client_id);

--client_details-table-idx
CREATE INDEX idx_client_details_client_id_inn
    ON client_details (client_id, inn);

CREATE INDEX idx_client_details_client_id_lower_ext_ref
    ON client_details (client_id, LOWER(ext_ref));

CREATE INDEX idx_client_details_client_id_lower_card_ref
    ON client_details (client_id, LOWER(card_ref));

CREATE INDEX idx_client_details_client_id_lower_inn
    ON client_details (client_id, LOWER(inn));

CREATE INDEX idx_client_details_client_id_lower_email
    ON client_details (client_id, LOWER(email));

CREATE INDEX idx_client_details_client_id_lower_cellphone
    ON client_details (client_id, LOWER(cellphone));

--tickets-table-idx
CREATE INDEX idx_tickets_optimized
    ON tickets (id, client_id)
    WHERE COALESCE(system_ticket, 0) = 0 AND COALESCE(deleted, 0) = 0;

--ratings-table-idx
CREATE INDEX idx_rating_id ON chat_messages (rating_id);
