class AddEnabledFlagIndexToAccounts < ActiveRecord::Migration[6.1]
def change
execute <<~SQL
CREATE INDEX index_accounts_on_beta_enabled
ON accounts ((settings->>'beta'))
WHERE (settings->>'beta') = 'true';
SQL
end
end
If you store flags/settings in JSONB, query performance hinges on indexing. Partial indexes are a great compromise: index only the rows that matter for the hot path (e.g., enabled flags).