N
N
nurzhannogerbek2018-11-26 07:43:56
MySQL
nurzhannogerbek, 2018-11-26 07:43:56

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 |

Partitioning was added to the YEAR , MONTH , DAY columns , and indexing was added to the DATE_TIME_KEY column .
Ran the following two queries which returns a pool of data for one day. Both queries return data in about the same time. Plus minus 1.5 seconds.
First request :
SELECT * FROM big_table
WHERE YEAR=2018 AND MONTH=9 AND DAY=1

Second request :
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'

I have two questions.
First question :
What is faster in theory, partitioning or indexing?
Second question :
Since the data pool for one day in my cases turned out to be not indicative, I decided to take the data pool for a month and measure the speed of data return. I don't know how to get a pool of data for a certain period using the YEAR, MONTH, DAY columns. Please help me make a request.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
V Sh., 2018-11-26
@JuniorNoobie

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.

S
Stalker_RED, 2018-11-26
@Stalker_RED

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 question

Ask a Question

731 491 924 answers to any question