Skip to main content
3 of 6
added 172 characters in body
ypercubeᵀᴹ
  • 98.7k
  • 13
  • 215
  • 305

The first index does look like a better fit for the query. Please provide the actual execution plans.

I would try using window functions instead of the four correlated subqueries.

It would also be worth checking and comparing the two execution plans, when asking for all the columns and when asking for only the columns in the index:

SELECT 
    -- m.*,
    movie_title, start_date_time,
    price, quantity,

    SUM(CASE WHEN price = 0 THEN quantity ELSE 0 END)
        OVER
        (PARTITION BY movie_title
         ORDER BY start_date_time
         ROWS BETWEEN UNBOUNDED PRECEDING
                  AND CURRENT ROW
       ) AS [Admissions Free],
    SUM(CASE WHEN price > 0 THEN quantity ELSE 0 END)
        OVER
        (PARTITION BY movie_title
         ORDER BY start_date_time
         ROWS BETWEEN UNBOUNDED PRECEDING
                  AND CURRENT ROW
       ) AS [Admissions Paid],
    SUM(CASE WHEN price > 0 THEN quantity * price ELSE 0 END)
        OVER
        (PARTITION BY movie_title
         ORDER BY start_date_time
         ROWS BETWEEN UNBOUNDED PRECEDING
                  AND CURRENT ROW
        ) AS [Admissions Revenue],
    SUM(quantity)
        OVER
        (PARTITION BY movie_title
         ORDER BY start_date_time
         ROWS BETWEEN UNBOUNDED PRECEDING
                  AND CURRENT ROW
        ) AS [Total Admissions]
FROM
    [movies] AS m ;

This index may be a little better than the first one:

(
    movie_title ASC,
    start_date_time ASC
)
INCLUDE (price, quantity)
ypercubeᵀᴹ
  • 98.7k
  • 13
  • 215
  • 305