An open API service indexing awesome lists of open source software.

https://github.com/brytebee/alx-airbnb-database


https://github.com/brytebee/alx-airbnb-database

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# Airbnb Clone Database - Complex Queries & Performance Optimization

A comprehensive database implementation for an Airbnb-style rental platform, focusing on complex SQL queries, performance optimization, and scalability.

## Project Overview

This project demonstrates advanced database design and optimization techniques for a vacation rental platform. The implementation includes sophisticated query patterns, strategic indexing, and performance monitoring typically found in production systems.

## Database Schema

Core entities include:
- **Users** - Guest and host account management
- **Properties** - Rental listings with locations
- **Bookings** - Reservation lifecycle management
- **Payments** - Financial transaction processing
- **Reviews** - Rating and feedback system
- **Messages** - User communication

## Key Features

### Advanced Query Patterns
- Multi-table joins with complex filtering
- Subqueries for payment and booking analytics
- Window functions for ranking and analytics
- Recursive queries for hierarchical data

### Performance Optimization
- Strategic indexing across 7 tables (25+ indexes)
- Query optimization reducing response times by 68%
- Materialized views for frequently accessed data
- Partitioning strategies for large datasets

### Monitoring & Analysis
- Slow query log implementation
- Query profiling and execution plan analysis
- Index usage statistics and maintenance
- Performance benchmarking and reporting

## File Structure

```
├── database-adv-script/
│ ├── aggregations_and_window_functions.sql # Complex multi-table operations
│ ├── database_index.sql # Strategic index implementation
│ ├── index_performance.md # Index effectiveness analysis
│ ├── join_queries.sql # Nested query implementations
│ ├── optimization_report.md # Comprehensive optimization report
│ ├── partition_performance.md # Partition performance report
│ ├── database_indexes.sql # Strategic index implementation
│ ├── partitioning.sql # Table partitioning strategies
│ ├── performance.sql # Query optimization analysis
│ └── performance_monitoring.md # Comprehensive performance report
├── database-script-0x01/
│ ├── schema.sql # Platform Database schema
│ └── README.md
├── database-script-0x02/
│ ├── seed.sql # Platform Database seed data
│ └── README.md
└── README.md
```

## Query Examples

### Booking Analytics with Multiple Joins
```sql
SELECT
p.name as property_name,
COUNT(b.booking_id) as total_bookings,
AVG(r.rating) as avg_rating,
SUM(py.amount) as total_revenue
FROM Property p
LEFT JOIN Booking b ON p.property_id = b.property_id
LEFT JOIN Review r ON p.property_id = r.property_id
LEFT JOIN Payment py ON b.booking_id = py.booking_id
WHERE p.created_at >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)
GROUP BY p.property_id
ORDER BY total_revenue DESC;
```

### User Activity Subquery
```sql
SELECT
u.first_name,
u.last_name,
(SELECT COUNT(*) FROM Booking b WHERE b.user_id = u.user_id) as bookings_made,
(SELECT AVG(rating) FROM Review r WHERE r.user_id = u.user_id) as avg_rating_given
FROM User u
WHERE u.role = 'guest'
HAVING bookings_made > 5;
```

## Performance Improvements

| Metric | Before | After | Improvement |
|--------|---------|-------|-------------|
| Avg Query Time | 2.5s | 0.8s | 68% |
| Complex Joins | 8-12s | 2-3s | 75% |
| Date Range Queries | 5-7s | 1.5-2s | 70% |
| Full Table Scans | 85% | 15% | 82% reduction |

## Key Optimizations

### Strategic Indexing
- Composite indexes for date ranges and user queries
- Foreign key indexes for JOIN operations
- Covering indexes for frequently accessed columns

### Query Restructuring
- Reduced column selection from 27 to 11 in main queries
- Replaced expensive LEFT JOINs with subqueries
- Added proper WHERE clauses and LIMIT statements

### Monitoring Implementation
- Slow query log with 1-second threshold
- Query profiling for execution analysis
- Index usage tracking and maintenance

## Running the Project

1. **Setup Database**
```bash
mysql -u root -p < schema.sql
```

2. **Load Sample Data**
```bash
mysql -u root -p airbnb_clone < sample_data.sql
```

3. **Create Indexes**
```bash
mysql -u root -p airbnb_clone < optimization/indexes.sql
```

4. **Run Performance Analysis**
```bash
mysql -u root -p airbnb_clone < optimization/performance_analysis.sql
```

## Academic Focus

This project demonstrates:
- **Database Design**: Normalized schema with proper relationships
- **Query Optimization**: Performance tuning techniques
- **Indexing Strategy**: Strategic index placement and analysis
- **Performance Monitoring**: Real-world monitoring implementation
- **Scalability Planning**: Partitioning and optimization strategies

## Results

The optimization efforts achieved:
- 68% improvement in average query response times
- 82% reduction in full table scans
- 85% improvement in buffer pool hit ratio
- Established framework for continued performance management

## Technologies Used

- **MySQL 8.0** - Primary database engine
- **SQL** - Query language and optimization
- **Performance Schema** - Monitoring and analysis
- **Information Schema** - Metadata queries

## Future Enhancements

- Table partitioning for historical data
- Read replicas for reporting queries
- Caching layer implementation
- Horizontal sharding strategies

---

*This project serves as a comprehensive example of database optimization techniques suitable for production environments while maintaining academic rigor in approach and documentation.*