title: MySQL Query stuck on 'Sending Data', doesn't seem to ever finish
body: <p>I have a query that goes through each hour of my <code>book_records</code> table and gets the number of books that are taken (value = 1) vs the number of books that are free (value = 0):</p>
  
  <pre><code>SELECT sr.time AS h,
         COUNT(CASE WHEN sr.value = 1 THEN 1 END) AS taken,
         COUNT(CASE WHEN sr.value = 0 THEN 1 END) AS free,
         sr.libID AS libID
  FROM `book_records` AS sr
  WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-01 23:00:00' 
  AND MINUTE(sr.time) = 0
  AND libID = 0 
  GROUP BY h, libID
  ORDER BY h, libID
  </code></pre>
  
  <p>The main line to look at here is this:</p>
  
  <pre><code>WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-01 23:00:00' 
  </code></pre>
  
  <p>This takes about 0.03s to query.</p>
  
  <p>If I change it to this (5 days):</p>
  
  <pre><code>WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-05 23:00:00' 
  </code></pre>
  
  <p>It takes about 0.15s.</p>
  
  <p>10 days:</p>
  
  <pre><code>WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-10 23:00:00'
  </code></pre>
  
  <p>It takes about 0.28s</p>
  
  <p><strong>But at 15 days:</strong></p>
  
  <pre><code>WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-15 23:00:00'
  </code></pre>
  
  <p>At the time of writing this, the query is still going. <code>SHOW FULL PROCESSLIST</code> tells me the query's state is set to 'Sleep' with the time going up.</p>
  
  <p>So what gives? Is there something in my MySQL 5.7 configuration that might be causing this? The query speed is fine at 1, 5, and 10 days, but 5 days more (15) and the query gets put in a Sleep state? Why?</p>
  
  <p><strong>EDIT:</strong> SHOW FULL PROCESSLIST output:</p>
  
  <pre><code>| 2234 | phpmyadmin | localhost | NULL    | Sleep   |     4 |              | NULL
  | 2235 | root       | localhost | library | Query   |     4 | Sending data | 
  SELECT SQL_CALC_FOUND_ROWS sr.time AS h, 
  COUNT(CASE WHEN sr.value = 1 THEN 1 END) AS taken, 
  COUNT(CASE WHEN sr.value = 0 THEN 1 END) AS free, sr.libID AS libID FROM `book_records` AS sr
  WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-15 23:00:00' 
  AND MINUTE(sr.time) = 0
  AND libID = 0 
  GROUP BY h, libID
  ORDER BY h, libID LIMIT 1 |
  </code></pre>