-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathreindex_tables.sql
More file actions
199 lines (153 loc) · 7.5 KB
/
reindex_tables.sql
File metadata and controls
199 lines (153 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
-- Reindex All Tables for scira
-- This file drops and recreates all indexes for optimal performance
-- Safe to run - uses IF EXISTS and CONCURRENTLY where possible
-- =============================================================================
-- STEP 1: DROP ALL EXISTING INDEXES
-- =============================================================================
-- Message Usage Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_message_usage_user_date;
DROP INDEX CONCURRENTLY IF EXISTS idx_message_usage_today;
-- Extreme Search Usage Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_extreme_search_usage_user_date;
DROP INDEX CONCURRENTLY IF EXISTS idx_extreme_usage_month;
-- Subscription Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_subscription_user_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_subscription_status;
DROP INDEX CONCURRENTLY IF EXISTS idx_subscription_customer;
-- User Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_user_email;
DROP INDEX CONCURRENTLY IF EXISTS idx_user_id;
-- Chat Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_chat_user_created;
-- Message Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_message_chat_created;
DROP INDEX CONCURRENTLY IF EXISTS idx_message_role_created;
-- Session Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_session_token;
DROP INDEX CONCURRENTLY IF EXISTS idx_session_user_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_session_token_expires;
DROP INDEX CONCURRENTLY IF EXISTS idx_session_active;
-- Payment Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_payment_user_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_payment_status_created;
DROP INDEX CONCURRENTLY IF EXISTS idx_payment_subscription;
-- Lookout Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_lookout_user_status;
DROP INDEX CONCURRENTLY IF EXISTS idx_lookout_next_run;
DROP INDEX CONCURRENTLY IF EXISTS idx_lookout_last_run;
-- Custom Instructions Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_custom_instructions_user;
-- Account Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_account_user_id;
DROP INDEX CONCURRENTLY IF EXISTS idx_account_provider;
-- Verification Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_verification_identifier;
-- Stream Indexes
DROP INDEX CONCURRENTLY IF EXISTS idx_stream_chat_created;
-- =============================================================================
-- STEP 2: RECREATE ALL INDEXES
-- =============================================================================
-- 1. MESSAGE USAGE - Most critical (user_id + date range queries)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_message_usage_user_date
ON message_usage(user_id, date);
-- 2. EXTREME SEARCH USAGE - Critical (user_id + date range queries)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_extreme_search_usage_user_date
ON extreme_search_usage(user_id, date);
-- 3. SUBSCRIPTION - Critical (userId lookups for Pro checks)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_subscription_user_id
ON subscription("userId");
-- 4. USER EMAIL - Critical (auth session lookups)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_email
ON "user"(email);
-- 5. CHAT USER QUERIES - Important (chat history)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_chat_user_created
ON chat("userId", created_at DESC);
-- 6. MESSAGE CHAT QUERIES - Important (message loading)
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_message_chat_created
ON message(chat_id, created_at ASC);
-- 7. SESSION TOKEN - Auth performance
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_session_token
ON session(token);
-- 8. SESSION USER - Auth performance
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_session_user_id
ON session(user_id);
-- 9. SESSION TOKEN LOOKUP - Critical for auth performance
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_session_token_expires
ON session(token, expires_at);
-- 10. SESSION ACTIVE LOOKUP - Fast active session checks
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_session_active
ON session(token, expires_at, user_id);
-- 11. USER ID LOOKUP - Speed up user table access
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_user_id
ON "user"(id);
-- 12. MESSAGE USAGE TODAY - Optimize daily usage lookups
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_message_usage_today
ON message_usage(user_id, date DESC, message_count);
-- 13. EXTREME SEARCH USAGE MONTH - Optimize monthly usage lookups
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_extreme_usage_month
ON extreme_search_usage(user_id, date DESC, search_count);
-- 14. PAYMENT USER LOOKUP - Payment history queries
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_payment_user_id
ON payment(user_id);
-- 15. PAYMENT STATUS - Payment status filtering
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_payment_status_created
ON payment(status, created_at DESC);
-- 16. LOOKOUT USER QUERIES - User's scheduled searches
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_lookout_user_status
ON lookout(user_id, status);
-- 17. LOOKOUT SCHEDULING - Next run scheduling
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_lookout_next_run
ON lookout(next_run_at, status);
-- 18. CUSTOM INSTRUCTIONS USER - User's custom instructions
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_custom_instructions_user
ON custom_instructions(user_id);
-- 19. ACCOUNT USER LOOKUP - Account linking
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_account_user_id
ON account(user_id);
-- 20. ACCOUNT PROVIDER LOOKUP - Provider account lookups
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_account_provider
ON account(provider_id, account_id);
-- 21. VERIFICATION IDENTIFIER - Email verification lookups
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_verification_identifier
ON verification(identifier, expires_at);
-- 22. STREAM CHAT LOOKUP - Stream queries by chat
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_stream_chat_created
ON stream("chatId", "createdAt" DESC);
-- 23. SUBSCRIPTION STATUS - Active subscription checks
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_subscription_status
ON subscription(status, "currentPeriodEnd");
-- 24. SUBSCRIPTION CUSTOMER - Customer subscription lookups
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_subscription_customer
ON subscription("customerId");
-- 25. LOOKOUT LAST RUN - Recently run lookouts
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_lookout_last_run
ON lookout(last_run_at DESC) WHERE last_run_at IS NOT NULL;
-- 26. MESSAGE ROLE FILTER - Filter messages by role
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_message_role_created
ON message(role, created_at DESC);
-- 27. PAYMENT SUBSCRIPTION LINK - Link payments to subscriptions
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_payment_subscription
ON payment(subscription_id) WHERE subscription_id IS NOT NULL;
-- =============================================================================
-- STEP 3: REINDEX PRIMARY TABLES
-- =============================================================================
-- Reindex main tables to rebuild primary key indexes and optimize storage
REINDEX TABLE CONCURRENTLY "user";
REINDEX TABLE CONCURRENTLY session;
REINDEX TABLE CONCURRENTLY account;
REINDEX TABLE CONCURRENTLY verification;
REINDEX TABLE CONCURRENTLY chat;
REINDEX TABLE CONCURRENTLY message;
REINDEX TABLE CONCURRENTLY stream;
REINDEX TABLE CONCURRENTLY subscription;
REINDEX TABLE CONCURRENTLY payment;
REINDEX TABLE CONCURRENTLY extreme_search_usage;
REINDEX TABLE CONCURRENTLY message_usage;
REINDEX TABLE CONCURRENTLY custom_instructions;
REINDEX TABLE CONCURRENTLY lookout;
-- =============================================================================
-- COMPLETED
-- =============================================================================
-- All indexes have been dropped and recreated
-- All tables have been reindexed
-- Your database is now optimized!