tinycodinglm / sys /sql-0003.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 19)
034ba67 verified
|
Raw History Blame
1.87 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

How do I optimize PostgreSQL with indexes?

Assistant
sql
-- B-tree (default, equality + range)
CREATE INDEX idx_users_email ON users(email);

-- Partial index
CREATE INDEX idx_orders_pending ON orders(created_at)
WHERE status = 'pending';

-- Composite index
CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);

-- GIN for full text search
CREATE INDEX idx_articles_search ON articles
USING gin(to_tsvector('english', title || ' ' || body));

-- GIN for JSONB
CREATE INDEX idx_users_metadata ON users USING gin(metadata jsonb_path_ops);

-- GiST for geospatial
CREATE INDEX idx_locations_coords ON locations USING gist(coords);

-- BRIN for large sorted tables
CREATE INDEX idx_logs_time ON logs USING brin(created_at);

-- Covering index (INCLUDE)
CREATE INDEX idx_users_email_cover ON users(email) INCLUDE (name, created_at);

-- Check index usage
SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read
FROM pg_stat_user_indexes
WHERE idx_scan = 0  -- unused indexes
ORDER BY pg_relation_size(indexrelid) DESC;

-- Analyze query plans
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT * FROM users WHERE email = 'test@example.com';