Repository navigation
Expand file tree
/
Copy pathdatabase_optimization.sql
More file actions
48 lines (42 loc) · 1.89 KB
/
Copy pathdatabase_optimization.sql
File metadata and controls
48 lines (42 loc) · 1.89 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
-- KOFA DATABASE OPTIMIZATION SCRIPT
-- Run this in Azure SQL to significantly improve API performance
-- These indexes will speed up common queries
-- =====================================================
-- STEP 1: Add indexes for Products table
-- =====================================================
CREATE INDEX IX_products_user_id ON products(user_id);
CREATE INDEX IX_products_category ON products(category);
CREATE INDEX IX_products_name ON products(name);
-- =====================================================
-- STEP 2: Add indexes for Orders table
-- =====================================================
CREATE INDEX IX_orders_user_id ON orders(user_id);
CREATE INDEX IX_orders_status ON orders(status);
CREATE INDEX IX_orders_created_at ON orders(created_at DESC);
CREATE INDEX IX_orders_user_status ON orders(user_id, status);
-- =====================================================
-- STEP 3: Add indexes for Expenses table
-- =====================================================
CREATE INDEX IX_expenses_user_id ON expenses(user_id);
CREATE INDEX IX_expenses_category ON expenses(category);
CREATE INDEX IX_expenses_date ON expenses(date DESC);
-- =====================================================
-- STEP 4: Add indexes for Users table
-- =====================================================
CREATE INDEX IX_users_email ON users(email);
CREATE INDEX IX_users_phone ON users(phone);
-- =====================================================
-- VERIFICATION: Check indexes were created
-- =====================================================
SELECT
t.name AS TableName,
i.name AS IndexName,
i.type_desc AS IndexType
FROM sys.indexes i
INNER JOIN sys.tables t ON i.object_id = t.object_id
WHERE i.name LIKE 'IX_%'
ORDER BY t.name, i.name;
-- EXPECTED IMPROVEMENT:
-- - Dashboard API: 10+ seconds → < 1 second
-- - Products list: 2+ seconds → < 300ms
-- - Orders list: 2+ seconds → < 300ms