Skip to content
This repository was archived by the owner on Mar 18, 2026. It is now read-only.

Commit d3bc675

Browse files
Merge pull request #433 from aibtcdev/indexes
add indexes
2 parents a5b0dde + 51f8d54 commit d3bc675

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
-- Add missing indexes on foreign key columns for improved query performance
2+
-- This migration creates indexes on foreign key columns that don't already have them
3+
-- Created: 2025-09-06
4+
5+
-- HOLDERS TABLE
6+
-- Add indexes on foreign key columns that are missing
7+
CREATE INDEX IF NOT EXISTS idx_holders_dao_id ON public.holders(dao_id);
8+
CREATE INDEX IF NOT EXISTS idx_holders_token_id ON public.holders(token_id);
9+
CREATE INDEX IF NOT EXISTS idx_holders_agent_id ON public.holders(agent_id);
10+
11+
-- KEYS TABLE
12+
-- Add index on profile_id foreign key
13+
CREATE INDEX IF NOT EXISTS idx_keys_profile_id ON public.keys(profile_id);
14+
15+
-- TELEGRAM_USERS TABLE
16+
-- Add index on profile_id foreign key
17+
CREATE INDEX IF NOT EXISTS idx_telegram_users_profile_id ON public.telegram_users(profile_id);
18+
19+
-- X_CREDS TABLE
20+
-- Add index on dao_id foreign key
21+
CREATE INDEX IF NOT EXISTS idx_x_creds_dao_id ON public.x_creds(dao_id);
22+
23+
-- X_TWEETS TABLE
24+
-- Add index on author_id foreign key
25+
CREATE INDEX IF NOT EXISTS idx_x_tweets_author_id ON public.x_tweets(author_id);
26+
27+
-- WALLETS TABLE
28+
-- Add indexes on foreign key columns
29+
CREATE INDEX IF NOT EXISTS idx_wallets_agent_id ON public.wallets(agent_id);
30+
CREATE INDEX IF NOT EXISTS idx_wallets_secret_id ON public.wallets(secret_id);
31+
32+
-- QUEUE TABLE
33+
-- Add index on dao_id foreign key (wallet_id already indexed)
34+
CREATE INDEX IF NOT EXISTS idx_queue_dao_id ON public.queue(dao_id);
35+
36+
-- VETOS TABLE
37+
-- Add index on profile_id foreign key (other FK indexes already exist)
38+
CREATE INDEX IF NOT EXISTS idx_vetos_profile_id ON public.vetos(profile_id);
39+
40+
-- FEEDBACK TABLE
41+
-- Add index on dao_id foreign key (profile_id and proposal_id already indexed)
42+
CREATE INDEX IF NOT EXISTS idx_feedback_dao_id ON public.feedback(dao_id);
43+
44+
-- COMPOSITE INDEXES FOR COMMON QUERY PATTERNS
45+
-- These indexes support common WHERE clauses that filter by multiple foreign keys
46+
47+
-- Holders by DAO and agent (useful for DAO member queries)
48+
CREATE INDEX IF NOT EXISTS idx_holders_dao_agent ON public.holders(dao_id, agent_id);
49+
50+
-- Holders by DAO and token (useful for token distribution queries)
51+
CREATE INDEX IF NOT EXISTS idx_holders_dao_token ON public.holders(dao_id, token_id);
52+
53+
-- Votes by DAO and agent (useful for agent voting history)
54+
CREATE INDEX IF NOT EXISTS idx_votes_dao_agent ON public.votes(dao_id, agent_id);
55+
56+
-- Votes by proposal and agent (useful for checking if agent voted on proposal)
57+
CREATE INDEX IF NOT EXISTS idx_votes_proposal_agent ON public.votes(proposal_id, agent_id);
58+
59+
-- Prompts by DAO and agent (useful for agent prompt queries)
60+
CREATE INDEX IF NOT EXISTS idx_prompts_dao_agent ON public.prompts(dao_id, agent_id);
61+
62+
-- Queue items by DAO and processing status (useful for job processing)
63+
CREATE INDEX IF NOT EXISTS idx_queue_dao_processed ON public.queue(dao_id, is_processed);
64+
65+
-- Wallets by profile and agent (useful for user wallet management)
66+
CREATE INDEX IF NOT EXISTS idx_wallets_profile_agent ON public.wallets(profile_id, agent_id);
67+
68+
-- PERFORMANCE INDEXES FOR COMMON FILTERS
69+
-- These indexes support common filtering patterns beyond just foreign keys
70+
71+
-- Queue items by processing status and creation time (for job queues)
72+
CREATE INDEX IF NOT EXISTS idx_queue_processed_created ON public.queue(is_processed, created_at);
73+
74+
-- Proposals by status and DAO (for filtering active proposals)
75+
CREATE INDEX IF NOT EXISTS idx_proposals_status_dao ON public.proposals(status, dao_id);
76+
77+
-- Votes by answer and proposal (for counting yes/no votes)
78+
CREATE INDEX IF NOT EXISTS idx_votes_answer_proposal ON public.votes(answer, proposal_id);
79+
80+
-- X_tweets by worthy flag and creation time (for filtering worthy tweets)
81+
CREATE INDEX IF NOT EXISTS idx_x_tweets_worthy_created ON public.x_tweets(is_worthy, created_at);
82+
83+
-- Agents by archived status and profile (for filtering active agents)
84+
CREATE INDEX IF NOT EXISTS idx_agents_archived_profile ON public.agents(is_archived, profile_id);
85+
86+
-- COMMENTS FOR DOCUMENTATION
87+
COMMENT ON INDEX idx_holders_dao_id IS 'Index on dao_id foreign key for efficient DAO member queries';
88+
COMMENT ON INDEX idx_holders_token_id IS 'Index on token_id foreign key for efficient token holder queries';
89+
COMMENT ON INDEX idx_holders_agent_id IS 'Index on agent_id foreign key for efficient agent holding queries';
90+
COMMENT ON INDEX idx_keys_profile_id IS 'Index on profile_id foreign key for efficient API key lookups';
91+
COMMENT ON INDEX idx_telegram_users_profile_id IS 'Index on profile_id foreign key for efficient Telegram user lookups';
92+
COMMENT ON INDEX idx_x_creds_dao_id IS 'Index on dao_id foreign key for efficient X credentials lookups';
93+
COMMENT ON INDEX idx_x_tweets_author_id IS 'Index on author_id foreign key for efficient tweet author queries';
94+
COMMENT ON INDEX idx_wallets_agent_id IS 'Index on agent_id foreign key for efficient agent wallet queries';
95+
COMMENT ON INDEX idx_wallets_secret_id IS 'Index on secret_id foreign key for efficient wallet secret lookups';
96+
COMMENT ON INDEX idx_queue_dao_id IS 'Index on dao_id foreign key for efficient DAO job queue queries';
97+
COMMENT ON INDEX idx_vetos_profile_id IS 'Index on profile_id foreign key for efficient user veto queries';
98+
COMMENT ON INDEX idx_feedback_dao_id IS 'Index on dao_id foreign key for efficient DAO feedback queries';
99+
100+
-- Composite index comments
101+
COMMENT ON INDEX idx_holders_dao_agent IS 'Composite index for queries filtering holders by both DAO and agent';
102+
COMMENT ON INDEX idx_holders_dao_token IS 'Composite index for queries filtering holders by both DAO and token';
103+
COMMENT ON INDEX idx_votes_dao_agent IS 'Composite index for agent voting history within specific DAOs';
104+
COMMENT ON INDEX idx_votes_proposal_agent IS 'Composite index for checking if specific agent voted on specific proposal';
105+
COMMENT ON INDEX idx_prompts_dao_agent IS 'Composite index for agent prompts within specific DAOs';
106+
COMMENT ON INDEX idx_queue_dao_processed IS 'Composite index for DAO job queue processing status';
107+
COMMENT ON INDEX idx_wallets_profile_agent IS 'Composite index for user wallet management queries';
108+
109+
-- Performance index comments
110+
COMMENT ON INDEX idx_queue_processed_created IS 'Index for efficient job queue processing by status and time';
111+
COMMENT ON INDEX idx_proposals_status_dao IS 'Index for filtering proposals by status within specific DAOs';
112+
COMMENT ON INDEX idx_votes_answer_proposal IS 'Index for counting yes/no votes on proposals';
113+
COMMENT ON INDEX idx_x_tweets_worthy_created IS 'Index for filtering worthy tweets by creation time';
114+
COMMENT ON INDEX idx_agents_archived_profile IS 'Index for filtering active agents by profile';

0 commit comments

Comments
 (0)