Why is this column order in my non-clustered index better for my query?Does the order of columns in a...

Was there a pre-determined arrangement for the division of Germany in case it surrendered before any Soviet forces entered its territory?

Players preemptively rolling, even though their rolls are useless or are checking the wrong skills

Can me and my friend spend the summer in Canada (6 weeks) at 16 years old without an adult?

Why do neural networks need so many examples to perform?

Is the symmetric product of an abelian variety a CY variety?

Why did Luke use his left hand to shoot?

How can I differentiate duration vs starting time

If I tried and failed to start my own business, how do I apply for a job without job experience?

Was Claire Dearing blamed for any of Jurassic World's failings?

Is there a non trivial covering of the Klein bottle by the Klein bottle

Isn't a semicolon (';') needed after a function declaration in C++?

Is the percentage symbol a constant?

RS485 using USART or UART port on STM32

Buying a "Used" Router

What is a good way to explain how a character can produce flames from their body?

How can I deduce the power of a capacitor from its datasheet?

Word for something that's always reliable, but never the best?

How can I handle players killing my NPC outside of combat?

How to fly a direct entry holding pattern when approaching from an awkward angle?

Why are mages sometimes played bot instead of traditional ADCs?

Lubuntu 18.10 File Manager: How to view directory tree structure?

Plausible reason for gold-digging ant

Modern Algebraic Geometry and Analytic Number Theory

Rigorous justification for non-relativistic QM perturbation theory assumptions?



Why is this column order in my non-clustered index better for my query?


Does the order of columns in a non-clustered ColumnStore index matter?Why isn't SQL Server using my non-clustered index and doing a clustered index scan?Aligned index column orderWhy SQL Server uses Non-Clustered but not Clustered Index?Does Clustered Index on GUID create more fragmentation than Non Clustered Index?What happens when you delete a Clustered index and Non Clustered Index once createdGetting Max for partition column in clustered columnstore indexUnderstanding non clustered index locking during an updateForcing Flow Distinctcreate a visible uniquifier column for a nonunique clustered index













0















I am working on a query for a table that contains movie tickets. The database holds 380k rows. A rows represents a showing of the movie (which cinema, when, how many tickets and at what price among other things).



I need to compute a few totals for every row: Admissions Paid, Admissions Revenue, Admissions Free and Total Admissions.



For a given row Admissions Paid is the sum of all tickets for that movie up until that point where price>0. The other 3 columns are computed similarly.



I wrote a query and created an index:



 SELECT [ID]
,[cinema_name]
,[movie_title]
,[price]
,[quantity]
,[start_date_time]
,* --I need all the columns for reporting
,(select SUM(quantity)
from [movies] i
where i.movie_title=o.movie_title
and i.start_date_time<=o.start_date_time
and price=0) as [Admissions Free]
,(select SUM(quantity)
from [movies] i
where i.movie_title=o.movie_title
and i.start_date_time<=o.start_date_time
and price>0) as [Admissions Paid]
,(select SUM(quantity*price)
from [movies] i
where i.movie_title=o.movie_title
and i.start_date_time<=o.start_date_time
and price>0) as [Admissions Revenue]
,(select SUM(quantity)
from [movies] i
where i.movie_title=o.movie_title
and i.start_date_time<=o.start_date_time) as [Total Admissions]
FROM [movies]


I created the following index which brought the query time down to 5 minutes:



CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
(
[movie_title] ASC,
[start_date_time] ASC,
[price] DESC
)
INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


But this index brought the query time down to 1:30:



CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
(
[start_date_time] ASC,
[movie_title] ASC,
[price] DESC
)
INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO


So my question is: why? From my understanding it makes more sense to first gather all the movie titles and then look at the start times because there are more start times then there are movies. Distinct movies: 51, distinct start_date_times: 8786



Doesn't the underling B-Tree not cut off more branches if it eliminates the unnecessary start_date_times first?









share







New contributor




dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    I am working on a query for a table that contains movie tickets. The database holds 380k rows. A rows represents a showing of the movie (which cinema, when, how many tickets and at what price among other things).



    I need to compute a few totals for every row: Admissions Paid, Admissions Revenue, Admissions Free and Total Admissions.



    For a given row Admissions Paid is the sum of all tickets for that movie up until that point where price>0. The other 3 columns are computed similarly.



    I wrote a query and created an index:



     SELECT [ID]
    ,[cinema_name]
    ,[movie_title]
    ,[price]
    ,[quantity]
    ,[start_date_time]
    ,* --I need all the columns for reporting
    ,(select SUM(quantity)
    from [movies] i
    where i.movie_title=o.movie_title
    and i.start_date_time<=o.start_date_time
    and price=0) as [Admissions Free]
    ,(select SUM(quantity)
    from [movies] i
    where i.movie_title=o.movie_title
    and i.start_date_time<=o.start_date_time
    and price>0) as [Admissions Paid]
    ,(select SUM(quantity*price)
    from [movies] i
    where i.movie_title=o.movie_title
    and i.start_date_time<=o.start_date_time
    and price>0) as [Admissions Revenue]
    ,(select SUM(quantity)
    from [movies] i
    where i.movie_title=o.movie_title
    and i.start_date_time<=o.start_date_time) as [Total Admissions]
    FROM [movies]


    I created the following index which brought the query time down to 5 minutes:



    CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
    (
    [movie_title] ASC,
    [start_date_time] ASC,
    [price] DESC
    )
    INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO


    But this index brought the query time down to 1:30:



    CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
    (
    [start_date_time] ASC,
    [movie_title] ASC,
    [price] DESC
    )
    INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    GO


    So my question is: why? From my understanding it makes more sense to first gather all the movie titles and then look at the start times because there are more start times then there are movies. Distinct movies: 51, distinct start_date_times: 8786



    Doesn't the underling B-Tree not cut off more branches if it eliminates the unnecessary start_date_times first?









    share







    New contributor




    dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I am working on a query for a table that contains movie tickets. The database holds 380k rows. A rows represents a showing of the movie (which cinema, when, how many tickets and at what price among other things).



      I need to compute a few totals for every row: Admissions Paid, Admissions Revenue, Admissions Free and Total Admissions.



      For a given row Admissions Paid is the sum of all tickets for that movie up until that point where price>0. The other 3 columns are computed similarly.



      I wrote a query and created an index:



       SELECT [ID]
      ,[cinema_name]
      ,[movie_title]
      ,[price]
      ,[quantity]
      ,[start_date_time]
      ,* --I need all the columns for reporting
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price=0) as [Admissions Free]
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price>0) as [Admissions Paid]
      ,(select SUM(quantity*price)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price>0) as [Admissions Revenue]
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time) as [Total Admissions]
      FROM [movies]


      I created the following index which brought the query time down to 5 minutes:



      CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
      (
      [movie_title] ASC,
      [start_date_time] ASC,
      [price] DESC
      )
      INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
      GO


      But this index brought the query time down to 1:30:



      CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
      (
      [start_date_time] ASC,
      [movie_title] ASC,
      [price] DESC
      )
      INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
      GO


      So my question is: why? From my understanding it makes more sense to first gather all the movie titles and then look at the start times because there are more start times then there are movies. Distinct movies: 51, distinct start_date_times: 8786



      Doesn't the underling B-Tree not cut off more branches if it eliminates the unnecessary start_date_times first?









      share







      New contributor




      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I am working on a query for a table that contains movie tickets. The database holds 380k rows. A rows represents a showing of the movie (which cinema, when, how many tickets and at what price among other things).



      I need to compute a few totals for every row: Admissions Paid, Admissions Revenue, Admissions Free and Total Admissions.



      For a given row Admissions Paid is the sum of all tickets for that movie up until that point where price>0. The other 3 columns are computed similarly.



      I wrote a query and created an index:



       SELECT [ID]
      ,[cinema_name]
      ,[movie_title]
      ,[price]
      ,[quantity]
      ,[start_date_time]
      ,* --I need all the columns for reporting
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price=0) as [Admissions Free]
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price>0) as [Admissions Paid]
      ,(select SUM(quantity*price)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time
      and price>0) as [Admissions Revenue]
      ,(select SUM(quantity)
      from [movies] i
      where i.movie_title=o.movie_title
      and i.start_date_time<=o.start_date_time) as [Total Admissions]
      FROM [movies]


      I created the following index which brought the query time down to 5 minutes:



      CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
      (
      [movie_title] ASC,
      [start_date_time] ASC,
      [price] DESC
      )
      INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
      GO


      But this index brought the query time down to 1:30:



      CREATE NONCLUSTERED INDEX [startdatetime_movietitle_price] ON [dbo].[movies]
      (
      [start_date_time] ASC,
      [movie_title] ASC,
      [price] DESC
      )
      INCLUDE ( [quantity]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
      GO


      So my question is: why? From my understanding it makes more sense to first gather all the movie titles and then look at the start times because there are more start times then there are movies. Distinct movies: 51, distinct start_date_times: 8786



      Doesn't the underling B-Tree not cut off more branches if it eliminates the unnecessary start_date_times first?







      sql-server-2014





      share







      New contributor




      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 mins ago









      dakesdakes

      1




      1




      New contributor




      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      dakes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          dakes is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f230639%2fwhy-is-this-column-order-in-my-non-clustered-index-better-for-my-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          dakes is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          dakes is a new contributor. Be nice, and check out our Code of Conduct.













          dakes is a new contributor. Be nice, and check out our Code of Conduct.












          dakes is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f230639%2fwhy-is-this-column-order-in-my-non-clustered-index-better-for-my-query%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

          Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

          How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...