Certainly, here is a more comprehensive version of the MySQL script that follows the updated dimensional model which includes additional dimensions and metrics.
CREATE DATABASE IF NOT EXISTS MusicStreamingDB;
USE MusicStreamingDB;
-- Create the dimension tables first since they will be referenced by the fact tables
-- User Dimension
CREATE TABLE User_Dimension (
User_ID INT AUTO_INCREMENT PRIMARY KEY,
User_Name VARCHAR(255) NOT NULL,
Registration_Date DATE,
Location_ID INT,
User_Type ENUM('Free', 'Subscriber') NOT NULL,
INDEX(Location_ID),
FOREIGN KEY (Location_ID) REFERENCES Location_Dimension(Location_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Song Dimension
CREATE TABLE Song_Dimension (
Song_ID INT AUTO_INCREMENT PRIMARY KEY,
Song_Title VARCHAR(255) NOT NULL,
Artist_ID INT,
Album_ID INT,
Genre_ID INT,
Release_Date DATE,
INDEX(Artist_ID),
INDEX(Album_ID),
INDEX(Genre_ID),
FOREIGN KEY (Artist_ID) REFERENCES Artist_Dimension(Artist_ID),
FOREIGN KEY (Album_ID) REFERENCES Album_Dimension(Album_ID),
FOREIGN KEY (Genre_ID) REFERENCES Genre_Dimension(Genre_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Date Dimension
CREATE TABLE Date_Dimension (
Date_ID INT AUTO_INCREMENT PRIMARY KEY,
Date DATE UNIQUE NOT NULL,
Weekday INT,
Month INT,
Quarter INT,
Year YEAR,
WeekOfYear INT
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Location Dimension
CREATE TABLE Location_Dimension (
Location_ID INT AUTO_INCREMENT PRIMARY KEY,
Country VARCHAR(100) NOT NULL,
Region VARCHAR(100),
City VARCHAR(100)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Artist Dimension
CREATE TABLE Artist_Dimension (
Artist_ID INT AUTO_INCREMENT PRIMARY KEY,
Artist_Name VARCHAR(255) NOT NULL,
Label_ID INT
-- More artist-related attributes can be added here
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Album Dimension
CREATE TABLE Album_Dimension (
Album_ID INT AUTO_INCREMENT PRIMARY KEY,
Album_Name VARCHAR(255) NOT NULL,
Artist_ID INT,
Release_Date DATE,
INDEX(Artist_ID),
FOREIGN KEY (Artist_ID) REFERENCES Artist_Dimension(Artist_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Genre Dimension
CREATE TABLE Genre_Dimension (
Genre_ID INT AUTO_INCREMENT PRIMARY KEY,
Genre_Name VARCHAR(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Feature Dimension
CREATE TABLE Feature_Dimension (
Feature_ID INT AUTO_INCREMENT PRIMARY KEY,
Feature_Name VARCHAR(255) NOT NULL,
Feature_Description TEXT,
Release_Date DATE,
Deprecated_Date DATE NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Now we set up the fact tables that reference these dimensions
-- Streams Fact
CREATE TABLE Streams_Fact (
Stream_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
User_ID INT,
Song_ID INT,
Feature_ID INT,
Date_ID INT,
Usage_Timestamp TIMESTAMP,
Duration_Played INT,
Stream_Quality VARCHAR(50),
INDEX(User_ID),
INDEX(Song_ID),
INDEX(Feature_ID),
INDEX(Date_ID),
FOREIGN KEY (User_ID) REFERENCES User_Dimension(User_ID),
FOREIGN KEY (Song_ID) REFERENCES Song_Dimension(Song_ID),
FOREIGN KEY (Feature_ID) REFERENCES Feature_Dimension(Feature_ID),
FOREIGN KEY (Date_ID) REFERENCES Date_Dimension(Date_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Feature Usage Fact
CREATE TABLE Feature_Usage_Fact (
Feature_Usage_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
User_ID INT,
Feature_ID INT,
Date_ID INT,
Usage_Timestamp TIMESTAMP,
Duration_Of_Use INT,
Engagement_Type ENUM('click', 'hover', 'long-press', 'others'),
INDEX(User_ID),
INDEX(Feature_ID),
INDEX(Date_ID),
FOREIGN KEY (User_ID) REFERENCES User_Dimension(User_ID),
FOREIGN KEY (Feature_ID) REFERENCES Feature_Dimension(Feature_ID),
FOREIGN KEY (Date_ID) REFERENCES Date_Dimension(Date_ID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- The script assumes `Artist_Dimension`, `Album_Dimension`, `Genre_Dimension`, and `Location_Dimension` exist.
-- Add additional fields as required by the application's data capture system and business needs.
-- Note: Depending on the complexity of user interactions with the features, the `Feature_Usage_Fact` table can be modified or extended.
-- If necessary, create additional dimension tables for detailed device information, subscription plans, etc.
In the above schema, we have dimension tables for users, songs, dates, locations, artists, albums, genres, and features. We also have fact tables to capture when a song is streamed and when a feature is used.
Please note that in practice, you would need to add more dimensions suited to your business needs, and also consider creating aggregate tables or views for common queries to speed up data retrieval. You should also consider adding capabilities for change data capture if you plan to track slowly changing dimensions.
Keep in mind:
- The use of the
NOT NULL constraint requires that the column cannot be left empty, which ensures data integrity. Adjust accordingly for your requirements.
- The
ENUM type is used to restrict the value to predefined options. Adjust the list within the ENUM as per the feature types and engagement types you expect to track.
- Indexes are created mostly on foreign keys and other columns that are expected to be common in WHERE clauses or joining conditions. You might want to add more indexes based on your query patterns.
- To optimize the model further, factors like partitioning and fine-tuning data types for size and speed might be considered.