Answer the question
In order to leave comments, you need to log in
How to get a pool of data for a period by YEAR, MONTH, DAY columns?
Hello comrades! I need your advice.
The MySQL database has a table with a similar structure (see below). This table stores about 20 million data, and the table weighs about 3.5 GB. The task is to display data for a certain period as quickly as possible.
| DATE_TIME_KEY | YEAR | MONTH | DAY | VALUE |
|--------------------------------------------------|
| 2018-11-09 00:00:00 | 2018 | 11 | 9 | 1056 |
| 2018-11-09 01:00:00 | 2018 | 11 | 9 | 8756 |
| 2018-11-10 00:00:00 | 2018 | 11 | 10 | 6582 |
| 2018-11-10 01:00:00 | 2018 | 11 | 10 | 2382 |
|**************************************************|
| 2018-12-09 00:00:00 | 2018 | 12 | 9 | 6892 |
| 2018-12-09 00:00:00 | 2018 | 12 | 9 | 8592 |
| 2018-12-10 00:00:00 | 2018 | 12 | 10 | 9587 |
| 2018-12-10 00:00:00 | 2018 | 12 | 10 | 6587 |
|**************************************************|
| 2019-03-05 00:00:00 | 2019 | 3 | 5 | 5693 |
| 2019-03-05 00:00:00 | 2019 | 3 | 5 | 7593 |
| 2019-03-06 00:00:00 | 2019 | 3 | 6 | 5698 |
| 2019-03-06 00:00:00 | 2019 | 3 | 6 | 8598 |
SELECT * FROM big_table
WHERE YEAR=2018 AND MONTH=9 AND DAY=1
SELECT * FROM big_table
FORCE INDEX (date_time_index)
WHERE DATE_TIME_KEY BETWEEN '2018-09-01 00:00:00' AND '2018-09-01 23:59:59'
Answer the question
In order to leave comments, you need to log in
Index lookup should be faster. Look at the query execution plan and evaluate by cost. They can be executed equally for you, because both are cached.
The second query is probably faster because it selects by field with an index.
Probably if to create indexes on year and month - it will be faster. Probably composite on these two fields will be even faster.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question