Spaces:
Sleeping
Sleeping
File size: 933 Bytes
9540270 | 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 | # Database Query Optimization
## Performance Tuning Guide
### Using EXPLAIN ANALYZE
Run EXPLAIN ANALYZE before your query to see the execution plan:
```sql
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';
```
### Indexing Strategies
1. **Create indexes** on columns used in WHERE clauses:
```sql
CREATE INDEX idx_users_email ON users(email);
```
2. **Composite indexes** for multi-column queries:
```sql
CREATE INDEX idx_orders_user_status ON orders(user_id, status);
```
### Connection Pooling
Use connection pooling to reduce overhead:
```python
from psycopg2.pool import ThreadedConnectionPool
pool = ThreadedConnectionPool(minconn=5, maxconn=20,
host="localhost", database="mydb")
```
### Best Practices
- Avoid SELECT * - specify only needed columns
- Use LIMIT for large result sets
- Implement pagination for user-facing queries
- Cache frequently accessed data
|