text: -- Before materialized views, the query run time was 5 min 5 secs, after materialized views 1 min 5 secs
  CREATE MATERIALIZED VIEW AGE_GAP AS
  SELECT
    GET_AGE_GAP (M.YEAR) AS GAP,
    TITLE,
    MID
  FROM
    movies AS M;
  
  CREATE MATERIALIZED VIEW RATING_GAP AS
  SELECT
    GET_RATING_GAP (M.RATING) AS GAP,
    TITLE,
    MID
  FROM
    movies AS M;
  
  CREATE MATERIALIZED VIEW SMITH_ACTORS_COUNT AS
  SELECT
    COUNT(DISTINCT A.NAME)
  FROM
    ACTORS AS A
    JOIN MOVIES AS M ON M.MID = A.MID
      AND M.TITLE = 'Mr. & Mrs. Smith';
  
  CREATE MATERIALIZED VIEW SMITH_ACTORS AS
  SELECT
    M1.MID,
    COUNT(*) AS NUM_COMMON_ACTORS
  FROM
    ACTORS AS A1,
    ACTORS AS A2,
    MOVIES AS M1,
    MOVIES AS M2
  WHERE
    A1.MID = M1.MID
    AND A2.MID = M2.MID
    AND M2.MID = (
      SELECT
        M.MID
      FROM
        MOVIES AS M
      WHERE
        M.TITLE = 'Mr. & Mrs. Smith'
      LIMIT 1)
  AND A2.NAME = A1.NAME
  GROUP BY
    M1.MID
  ORDER BY
    M1.MID;
  
  CREATE MATERIALIZED VIEW COMMON_ACTOR_FRACTION AS
  SELECT
    GET_COMMON_ACTOR_FRACTION (M.MID) AS FRACTION,
    TITLE,
    MID
  FROM
    movies AS M;
  
  -- FRACTION OF COMMON GENRES
  CREATE MATERIALIZED VIEW SMITH_GENRES AS SELECT DISTINCT
    G.GENRE
  FROM
    GENRES AS G
    JOIN MOVIES AS M ON M.MID = G.MID
      AND M.TITLE = 'Mr. & Mrs. Smith';
  
  CREATE MATERIALIZED VIEW SMITH_GENRES_COUNT AS
  SELECT
    COUNT(DISTINCT G.GENRE)
  FROM
    GENRES AS G
    JOIN MOVIES AS M ON M.MID = G.MID
      AND M.TITLE = 'Mr. & Mrs. Smith';
  
  CREATE MATERIALIZED VIEW COMMON_GENRE_FRACTION AS
  SELECT
    GET_COMMON_GENRE_FRACTION (M.MID) AS FRACTION,
    TITLE,
    MID
  FROM
    MOVIES AS M;
  
  SELECT
    FRACTION,
    TITLE,
    MID
  FROM
    COMMON_GENRE_FRACTION
  ORDER BY
    FRACTION;
  
  -- FRACTION OF COMMON TAGS
  CREATE MATERIALIZED VIEW SMITH_TAGS AS SELECT DISTINCT
    TAG
  FROM
    TAG_NAMES
    JOIN MOVIES ON MOVIES.TITLE = 'Mr. & Mrs. Smith'
    JOIN TAGS ON TAGS.MID = MOVIES.MID
      AND TAGS.TID = TAG_NAMES.TID;
  
  CREATE MATERIALIZED VIEW SMITH_TAGS_COUNT AS
  SELECT
    COUNT(DISTINCT TAG)
  FROM
    TAG_NAMES
    JOIN MOVIES ON MOVIES.TITLE = 'Mr. & Mrs. Smith'
    JOIN TAGS ON TAGS.MID = MOVIES.MID
      AND TAGS.TID = TAG_NAMES.TID;
  
  CREATE MATERIALIZED VIEW COMMON_TAG_FRACTION AS
  SELECT
    GET_COMMON_TAG_FRACTION (M.MID) AS FRACTION,
    TITLE,
    MID
  FROM
    MOVIES AS M;
  
  CREATE MATERIALIZED VIEW JOINED_FRACTIONS AS
  SELECT
    CGF.MID,
    m.TITLE,
    m.RATING,
    RECOMMENDATION (CAF.FRACTION, CTF.FRACTION, CGF.FRACTION, AG.GAP, RG.GAP) AS SIMILARITY
  FROM
    COMMON_GENRE_FRACTION CGF
    JOIN COMMON_ACTOR_FRACTION CAF ON CAF.MID = CGF.MID
    JOIN AGE_GAP AG ON AG.MID = CGF.MID
    JOIN RATING_GAP RG ON RG.MID = CGF.MID
    JOIN COMMON_TAG_FRACTION CTF ON CTF.MID = CGF.MID
    JOIN MOVIES m ON m.MID = CGF.MID
  WHERE
    m.TITLE <> 'Mr. & Mrs. Smith'
  ORDER BY
    SIMILARITY DESC nulls LAST
  LIMIT 10;
  
  SELECT
    mid,
    title,
    rating,
    similarity
  FROM
    JOINED_FRACTIONS;
  
  