This project involves the creation of a movie-centered database management system (DBMS) using MySQL and Python. The system fetches data from The Movie Database (TMDB) API and stores it in a well-structured MySQL database. Additionally, the project includes five distinct database queries to analyze the stored data, focusing on specific aspects of the movie industry. While the project emphasizes backend development, a frontend design is documented to illustrate the intended application interface.
-
Database Schema: The database includes five tables:
movies,genres,persons,movie_genres, andmovie_cast. These tables store detailed information about movies, their genres, and cast/crew members. -
Data Population:
- The system uses the TMDB API to retrieve movie data, genres, and credits.
- Over 5,000 records are populated across the tables.
-
Queries:
- Two full-text search queries.
- Three complex queries involving nested subqueries, aggregations, and the
EXISTSclause.
-
Database Optimizations:
- Proper indexing and foreign key constraints for efficient query execution.
- Python 3.11.4
- MySQL server (hosted on
mysqlsrv1.cs.tau.ac.il) - Required Python libraries (see
requirements.txt)
project-root/
│
├── src/
│ ├── create_db_script.py # Script to create the database schema
│ ├── api_data_retrieve.py # Script to fetch and insert data from TMDB API
│ ├── queries_db_script.py # Contains the SQL query functions
│ └── queries_execution.py # Demonstrates query executions
│
├── documentation/
│ ├── user_manual.pdf # Application functionality and design
│ ├── system_docs.pdf # Database schema and design rationale
│ └── mysql_and_user_password.txt # MySQL credentials
│
├── requirements.txt # Required Python packages
└── name_and_id.txt # Team member names and IDs
The database consists of the following tables:
-
movies:
- Stores basic movie details.
- Indexed by
movie_idfor fast lookups.
-
genres:
- Stores genre information.
- Linked to movies via the
movie_genrestable.
-
persons:
- Stores details of cast and crew members.
-
movie_genres:
- Links movies to their respective genres.
-
movie_cast:
- Links movies to cast and crew, specifying roles and character names.
- Query 1: Find the top 5 movies mentioning 'gangster' in their overview with the highest average rating, including genres
- Query 2: Find the top 5 most popular movies mentioning 'Action' in their overview, along with their genres and the number of actors in each movie
- Query 3: Find movies with the most diverse cast (actors from different genres)
- Query 4: Find the highest-rated movie in each genre, along with its rating, genre name, and popularity.
- Query 5: Find top 5 directors by average vote_average of the movies they directed
This project uses a set of carefully selected indexes to optimize query performance. Below is a summary of the indexes added to each table, their purpose, and justification:
| Table | Index Name | Columns | Purpose | Justification |
|---|---|---|---|---|
movies |
idx_overview |
overview |
Full-text search for queries like MATCH ... AGAINST on movie descriptions. |
Improves performance for text searches involving keywords (e.g., "gangster"). |
idx_popularity |
popularity |
Filtering and sorting movies by popularity. | Optimizes queries that rank movies by popularity, often used in user-facing applications. | |
idx_vote_average |
vote_average |
Filtering and sorting movies by average vote. | Improves performance for queries ranking movies by ratings. | |
idx_movies_vote_movie |
vote_average, movie_id |
Efficient sorting and grouping for queries involving ratings and movie ID. | Supports queries like finding the top-rated movies or aggregating results by movie ID. | |
movie_genres |
idx_movie_genres_genre_movie |
genre_id, movie_id |
Efficient joins between movies and genres. | Essential for queries combining movie and genre information (e.g., highest-rated movies per genre). |
movie_cast |
idx_cast_movie |
movie_id, person_id |
Optimizes joins with persons and grouping for queries about actors and directors. |
Critical for queries fetching details about cast members or filtering by cast roles. |
idx_cast_role |
person_id, role |
Efficient filtering for queries involving roles (e.g., actors or directors). | Improves performance of role-specific queries like finding directors or actors for a movie. | |
genres |
idx_genre_id |
genre_id |
Efficient joins with movie_genres for genre-specific queries. |
Ensures fast joins between genres and movie genres for filtering or aggregating by genre. |
persons |
idx_person_id |
person_id |
Optimizes joins with movie_cast for queries involving directors and cast members. |
Supports fast lookups of directors or actors and their associated movies. |
- Read Optimization: These indexes are designed to enhance the performance of read-heavy operations such as
SELECT,JOIN,WHERE, andGROUP BY. - Write Performance Impact: While indexes slightly slow down write operations (
INSERT,UPDATE,DELETE), the tradeoff is acceptable given the read-heavy nature of the workload. - Regular Monitoring: Query performance should be monitored over time using tools like
EXPLAINandinformation_schema.statisticsto ensure the indexes remain effective as the database grows.
- The
idx_overviewindex is a full-text index and is crucial for queries that involve searching for specific text in the movie descriptions. - Composite indexes (e.g.,
idx_movie_genres_genre_movieandidx_movies_vote_movie) are used for optimizing queries that involve multiple columns for filtering, grouping, or sorting.
For any new queries, revisit this table to evaluate if additional indexes are needed.
pip install -r requirements.txtRun the following script to create the database schema:
python src/create_db_script.pyFetch and insert data from TMDB API:
python src/api_data_retrieve.pyRun example queries to interact with the database:
python src/queries_execution.pyDetailed descriptions of the database schema, design rationale, and optimizations are available in system_docs.pdf.
The scripts include robust error handling to manage API failures, database connection issues, and data insertion conflicts.
This project is developed for educational purposes and follows the academic guidelines set by the institution.
- Shai Goldbourt(Shaigoldbourt22)
- Dor Liberman (dorlib)
If you have any questions or feedback, I would be glad if you will contact us via mail.
This project was created for educational purposes, for personal and open-source use.
If you like my content or find my code useful, give it a ⭐