I have a query that goes through each hour of my book_records
table and gets the number of books that are taken (value = 1) vs the number of books that are free (value = 0):
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
The main line to look at here is this:
WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-01 23:00:00'
This takes about 0.03s to query.
If I change it to this (5 days):
WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-05 23:00:00'
It takes about 0.15s.
10 days:
WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-10 23:00:00'
It takes about 0.28s
But at 15 days:
WHERE sr.time BETWEEN '2017-01-01 00:00:00' AND '2017-01-15 23:00:00'
At the time of writing this, the query is still going. SHOW FULL PROCESSLIST
tells me the query's state is set to 'Sleep' with the time going up.
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?
EDIT: SHOW FULL PROCESSLIST output:
| 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 |