title: Slow Count query
body: <p>I had created a query to find how many transactions  are made by each ticket in a branch queues. </p>
  
  <p>So I had the <code>Tickets_table</code> that has the fields <code>Branch</code>, <code>number_of_ticket</code>, <code>issue_date</code> and <code>Ticket_call_time</code>, <code>Ticket_End_time</code> (which is the time the ticket stopped being served)</p>
  
  <p>The <code>Transaction_table</code> has the transactions that are made in each Branch each day. The fields of <code>Transaction_Table</code> are <code>Branch</code>, <code>Date</code>, and <code>Transaction_time</code>.  In order to find which transactions are made by each ticket I created a query that connects the two tables on branch and date and on <code>Ticket_table.Ticket_call_time&lt;=Transaction_table.Transaction_time</code> and <code>Ticket_table.Ticket_End_time&gt;=Transaction_table.End_time</code> as below:</p>
  
  <pre><code>SELECT 
      Transaction_table.Branch, 
      Tickets_table.Ticket, 
      Tickets_table.Date, 
      Transactions_Table.Transaction_Time, 
      Ticket_table.Ticket_call_time, 
      Ticket_table.Ticket_End_time, 
      QTicket_table.Ticket_Call_time, 
      Transaction_table.Transaction_code, 
      [Branch] &amp; [Date] &amp; [Ticket] AS branchdateticket
  FROM 
      Transactions_Table 
      INNER JOIN 
      Tickets_Table 
          ON (Transaction_table.Branch = Ticket_table.Branch) AND 
              Transaction_table.Date = Ticket_table.Date AND 
              Ticket_table.Ticket_call_time&lt;=Transaction_table.Transaction_time AND 
              Ticket_table.Ticket_End_time&gt;=Transaction_table.End_time);
  </code></pre>
  
  <p>This query works fine.</p>
  
  <p>I have a table that for the combination of each ticket, branch, and date has the transactions made, but has millions of rows.  I have tried to make another query to count the number of transactions for the last column <code>branchdateticket</code>, which is the classic:</p>
  
  <pre><code>SELECT 
      branchdateticket, 
      count(Transaction_code)
  FROM 
      the_query_above
  GROUP BY 
      branchdateticket
  </code></pre>
  
  <p>This query takes about one day to run.</p>
  
  <p>Can you please helm me make it faster?</p>